forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheggtimer
More file actions
executable file
·136 lines (102 loc) · 3.84 KB
/
eggtimer
File metadata and controls
executable file
·136 lines (102 loc) · 3.84 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
from __future__ import print_function
import sys, os
import time
import signal
wakeuptime = None
message = "Wake up!"
# Tkinter changed capitalization from Python 2 to Python 3.
# Python 2: import Tkinter as tkinter
# Python 3: import tkinter
# The error trying to import a nonexistent module also changed:
# Python2 raises ImportError, while Python3 raises ModuleNotFoundError
# which is a class of ImportError. Use ImportError because both
# Python versions understand it.
try:
import tkinter
# This may throw ImportError in Python 2 or ModuleNotFoundError in Py 3.
except ImportError:
# In case it's Python 2, try importing it the Python 2 way:
try:
import Tkinter as tkinter
except ImportError:
print("Sorry, ImportError, eggtimer needs Tkinter")
sys.exit(1)
# Callback for key events:
def keyEvent(event):
# print(event.char, event.keysym, event.keycode)
if event.char == 'q' or event.keysym == 'Return':
sys.exit(0)
def Usage():
print("Usage:", sys.argv[0], "minutes message")
print("With no arguments, will show any eggtimers currently running")
sys.exit(0)
def showAlert(message:str):
# This is supposed to show a dialog, but tkMessageBox doesn't exist:
# tkMessageBox.showwarning("hello", message)
# Try to beep a bit, even though that doesn't work on some distros:
print("")
# print message
root = tkinter.Tk()
button = tkinter.Button(root, text=message,
bg="red", activebackground="red",
fg="white", activeforeground="white",
font=("Sans", 40, "bold"),
command=quit)
# Make sure the window is at least as big as the screen:
button.pack(ipadx=root.winfo_screenwidth()/2,
ipady=root.winfo_screenheight()/2)
# Apparently we can't bind key events to a button, only to the root:
root.bind("<Key>", keyEvent)
root.mainloop()
# main: read the runtime arguments.
if __name__ == '__main__':
if len(sys.argv) == 1:
# Find all running eggtimer processes and send them a SIGUSR1
foundone = False
slashproc = "/proc"
for pid in os.listdir(slashproc):
try:
# Make sure the directory name is an integer PID
pidint = int(pid)
# Skip the PID of this process
if pidint == os.getpid():
continue
# Look for other eggtimer processes
with open(os.path.join(slashproc, pid, "cmdline")) as fp:
cmdline = fp.read().split('\0')
if "python" not in cmdline[0]:
continue
if not (cmdline[1].endswith("eggtimer") or
cmdline[1].endswith("eggtimer.py")):
continue
foundone = True
os.kill(pidint, signal.SIGUSR1)
except:
pass
if not foundone:
Usage()
sys.exit(0)
try:
sleeptime = float(sys.argv[1]) * 60
except ValueError:
Usage()
if len(sys.argv) > 2:
message = ' '.join(sys.argv[2:])
print("Sleeping for", sleeptime, "seconds with message:", message)
# Return control to the shell before sleeping:
rc = os.fork()
if rc:
sys.exit(0)
# Set up a signal handler so users can query for time left
def signal_handler(signal, frame):
timeleft = int(wakeuptime - time.time())
print("In", timeleft, "seconds:", message, "(PID %d)" % os.getpid())
time.sleep(timeleft)
showAlert(message)
# Trap SIGUSR1
signal.signal(signal.SIGUSR1, signal_handler)
wakeuptime = time.time() + sleeptime
time.sleep(sleeptime)
print("woke up")
showAlert(message)