-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShowWords.py
executable file
·69 lines (61 loc) · 2.52 KB
/
ShowWords.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
import os, sys, time, re, subprocess, ReadWordList, LookUpWord, PanelNotification
timeStringFormat = "%A, %d %B %Y, %I:%M:%S %p"
#newWordsRegExPattern = re.compile(r'\(\*\)') # set this to None if you do not want to filter only words marked by some symbol
newWordsRegExPattern = None
def loopThroughGREWords(wordsFileOrDir, tempOutFile):
print "%s - Started logging...\n" % time.strftime(timeStringFormat)
noteStartTime = 0
while(True):
word = ReadWordList.pickWordAtRandom(wordsFileOrDir, newWordsRegExPattern)
definition = LookUpWord.getDefinition(word, tempOutFile)
while(time.time()-noteStartTime < 11):
time.sleep(1)
noteStartTime = time.time()
if newWordsRegExPattern is not None:
word = '%s %s %s' % ('*', word, '*')
PanelNotification.showPanelNotification(word, definition)
def getPIDFromFile(pidFile):
if os.path.exists(pidFile) and os.path.isfile(pidFile):
with open(pidFile, 'rb') as pidFileHandle:
pid = pidFileHandle.read()
return pid
else:
return -1
def processWithPIDHasName(pid, processName):
subprocessHandle = subprocess.Popen(['ps', '-p', pid, '-O', 'c'], stdout=subprocess.PIPE)
output = subprocessHandle.stdout.read()
pos = output.find(processName)
if pos >= 0:
return True
else:
return False
def deleteFiles(filesList):
for filePath in filesList:
if os.path.exists(filePath) and os.path.isfile(filePath):
os.remove(filePath)
return
def writePIDToFile(pidFile):
with open(pidFile,'wb') as pidFileHandle:
pidFileHandle.write("%s" % os.getpid())
return
def redirectStdOutAndStdErrToFile(logFile):
logFileHandle = open(logFile, 'wb', 0) # 0 is for unbuffered
sys.stdout = logFileHandle
sys.stderr = logFileHandle
return
if __name__ == '__main__':
srcDir = os.path.split(PanelNotification.__file__)[0]
pidFile = os.path.join(srcDir, 'Word-Hoard.pid')
logFile = os.path.join(srcDir, 'Word-Hoard.log')
tempOutFile = os.path.join(srcDir, 'Word-Hoard-Definition.txt')
pid = getPIDFromFile(pidFile)
if pid > 0 and processWithPIDHasName(pid, 'ShowWords.py'):
subprocess.Popen(['kill', pid])
deleteFiles([pidFile, logFile])
sys.exit(1)
else:
writePIDToFile(pidFile)
redirectStdOutAndStdErrToFile(logFile)
loopThroughGREWords('/home/kaushikk/TempFolder', tempOutFile)
# Yet to handle closing logFileHandle gracefully