-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreminder.py
More file actions
executable file
·216 lines (194 loc) · 7.23 KB
/
reminder.py
File metadata and controls
executable file
·216 lines (194 loc) · 7.23 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python3
VERSION = "v0.14.1"
import argparse
import os
import subprocess
import datetime
import platform
from pathlib import Path
import json
fullpath = os.path.abspath(__file__)
name_len = len(os.path.basename(__file__))
fullpath = fullpath[:-name_len]
workmode_path = f"{fullpath}/workmode.txt"
addcron_path = f"{fullpath}/scripts/addcron.sh"
sound_path = f"{fullpath}/sound.wav"
applescript_path = f"{fullpath}/scripts/popup.scpt"
uninstall_path = f"{fullpath}/scripts/uninstall.sh"
message_path = f"{fullpath}/message.txt"
productivity_log_path = f"{fullpath}/productivity.log"
settings_path = f"{fullpath}/settings.json"
settings = json.loads(Path(settings_path).read_text())
DEFAULT_TIME=settings["DEFAULT_TIME"]
DEFAULT_MESSAGE=settings["DEFAULT_MESSAGE"]
parser = argparse.ArgumentParser(prog="work",epilog=f"take-a-break {VERSION}")
parser.add_argument("action",help="action to execute")
parser.add_argument("-t","--time",default=DEFAULT_TIME,help="(optional) reminder time interval")
parser.add_argument("-m","--message",default="",help="(optional) change reminder message. Usage: work message -m {message}")
args = parser.parse_args()
def read_work_mode():
if os.path.exists(workmode_path):
with open(workmode_path,"r") as file_read:
mode = file_read.readlines()[0].split()
file_read.close()
return mode[0]
else:
file_write = open(workmode_path, 'a')
file_write.write("unset")
file_write.close()
return "unset"
def read_work_delay():
if os.path.exists(workmode_path):
with open(workmode_path,"r") as file_read:
mode = file_read.readlines()[0].split()
file_read.close()
if(mode[0]=="set"):
return mode[1]
return "-1"
else:
file_write = open(workmode_path, 'a')
file_write.write(f"set {DEFAULT_TIME}")
file_write.close()
return str(DEFAULT_TIME)
def write_work_mode(mode,time=DEFAULT_TIME):
change_mode(mode,time)
subprocess.call(['sh',addcron_path,fullpath,mode,time])
if(mode == "set"):
print(f"set work mode with interval {time} minutes")
# important: call write_work_mode("unset", time) with time!=DEFAULT_TIME to hide unset output
elif(mode == "unset" and time==DEFAULT_TIME):
print(f"unset work mode")
def change_message(mode,message=DEFAULT_MESSAGE):
path = Path(message_path)
if(mode == "set"):
path.write_text(message)
elif(mode == "get"):
current_message = path.read_text().strip('\n')
print(f"Current message is: {current_message}")
def change_mode(mode,time=DEFAULT_TIME):
path = Path(workmode_path)
path.write_text(f"{mode} {time}")
def check_next():
current_crontab = subprocess.check_output(['crontab','-l'])
current_crontab = current_crontab.decode('utf-8')
correct_entry = ""
for line in current_crontab.splitlines():
if "reminder.py" in line:
correct_entry = line
break
if(correct_entry!=""):
current_time = datetime.datetime.now()
current_mins = current_time.minute
current_hour = current_time.hour
correct_entry = correct_entry.split()
reminder_mins = int(correct_entry[0])
reminder_hour = int(correct_entry[1])
if(reminder_hour>=current_hour):
hours_to_next=reminder_hour-current_hour
else:
hours_to_next=24-current_hour+reminder_hour
if(reminder_mins>=current_mins):
mins_to_next=reminder_mins-current_mins
else:
mins_to_next=60-current_mins+reminder_mins
hours_to_next-=1
return 60*hours_to_next + mins_to_next
return -1
if(args.action == "get"):
current_delay = read_work_delay()
current_mode = read_work_mode()
next = check_next()
if((next == -1 and current_mode == "set") or (int(current_delay) > next)):
current_mode = "unset"
write_work_mode("unset","-1")
current_mode = "unset"
print(f"current mode is {current_mode}")
elif(args.action == "set"):
if read_work_mode() == "unset":
if(args.time != "-1"):
time = args.time
try:
time = int(time)
except ValueError:
print("invalid time argument - must be numeric")
exit()
write_work_mode("set",str(time))
if(args.message != ""):
change_message("set",args.message)
print(f"set message to: {args.message}")
else:
write_work_mode("set",DEFAULT_TIME)
else:
print("work mode already set")
elif(args.action == "unset"):
if read_work_mode() == "set":
write_work_mode("unset",DEFAULT_TIME)
else:
print("work mode already unset")
elif(args.action == "next"):
next = check_next()
if(next!=-1 and int(next) <= int(read_work_delay())):
print(f"next reminder is in {next} minutes")
elif(int(next) > int(read_work_delay())):
write_work_mode("unset","-1")
print("enable work mode to check next reminder")
else:
print("enable work mode to check next reminder")
elif(args.action == "message"):
message=args.message
action="set"
if(message == "default"):
message=DEFAULT_MESSAGE
elif(message == ""):
action="get"
change_message(action,message)
if(action=="set"):
print(f"Set message to: \"{message}\"")
elif(args.action == "log"):
try:
print("opening productivity.log")
subprocess.run(["open",productivity_log_path])
except:
print("could not open productivity.log")
print("attempting to list productivity.log here:")
try:
subprocess.run(["cat",productivity_log_path])
except:
print("could not list productivity.log")
elif(args.action == "settings"):
try:
subprocess.run(["cat",settings_path])
except:
print("could not print settings.json")
elif(args.action == "reminder"):
if read_work_mode() == "set":
if(not Path(productivity_log_path).exists()):
Path(productivity_log_path).touch()
os_type = platform.system()
if os_type == "Darwin":
process = subprocess.Popen(['afplay',sound_path])
subprocess.run(["osascript", applescript_path,fullpath,read_work_delay()])
elif os_type == "Linux":
process = subprocess.Popen(['aplay',sound_path])
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.title("Break Reminder")
messagebox.showinfo("Reminder", "Reminder to take a break!")
root.mainloop()
elif os_type == "Windows":
import winsound
winsound.PlaySound(sound_path, winsound.SND_ASYNC)
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.title("Break Reminder")
messagebox.showinfo("Reminder", "Reminder to take a break!")
root.mainloop()
else:
print("warning : your operating system is not yet supported for the sound feature")
elif(args.action=="uninstall"):
subprocess.call([uninstall_path])
else:
print("unknown command, please try again")
exit()