-
Notifications
You must be signed in to change notification settings - Fork 12
/
timebox.py
158 lines (134 loc) · 5.9 KB
/
timebox.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
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
import rumps
import time
import subprocess
import shlex
import csv
import sqlite3
import os
try:
from urllib import urlretrieve
except ImportError:
from urllib.request import urlretrieve
rumps.debug_mode(True)
SEC_TO_MIN = 60
def timez():
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime())
def get_things_min(index=0, complete_task=False):
try:
conn = sqlite3.connect(os.path.expanduser('~/Library/Containers/com.culturedcode.ThingsMac/Data/Library/Application Support/Cultured Code/Things/Things.sqlite3'))
sql = ("SELECT\n"
" TAG.title,\n"
" TASK.title,\n"
" \"things:///show?id=\" || TASK.uuid\n"
" FROM TMTask as TASK\n"
" LEFT JOIN TMTaskTag TAGS ON TAGS.tasks = TASK.uuid\n"
" LEFT JOIN TMTag TAG ON TAGS.tags = TAG.uuid\n"
" LEFT OUTER JOIN TMTask PROJECT ON TASK.project = PROJECT.uuid\n"
" LEFT OUTER JOIN TMArea AREA ON TASK.area = AREA.uuid\n"
" LEFT OUTER JOIN TMTask HEADING ON TASK.actionGroup = HEADING.uuid\n"
" WHERE TASK.trashed = 0 AND TASK.status = 0 AND TASK.type = 0 AND TAG.title IS NOT NULL\n"
" AND TASK.start = 1\n"
" AND TASK.startdate is NOT NULL\n"
" ORDER BY TASK.todayIndex\n"
" LIMIT 100")
tasks = []
for row in conn.execute(sql):
tasks.append([*row])
conn.close()
task = tasks[index]
if not complete_task:
if task[0]:
return int(task[0].replace("min", ""))
else:
if task[2]:
# print("open the following url: ", task[2])
subprocess.call(shlex.split('open '+task[2]))
except:
return 60
class TimerApp(object):
def __init__(self, timer_interval=1):
self.timer = rumps.Timer(self.on_tick, 1)
self.timer.stop() # timer running when initialized
self.timer.count = 0
self.app = rumps.App("Timebox", "🥊")
self.start_pause_button = rumps.MenuItem(title='Start Timer',
callback=lambda _: self.start_timer(_, self.interval))
self.stop_button = rumps.MenuItem(title='Stop Timer',
callback=None)
self.buttons = {}
self.buttons_callback = {}
for i in [5, 10, 15, 20, 25]:
title = str(i) + ' Minutes'
callback = lambda _, j=i: self.set_mins(_, j)
self.buttons["btn_" + str(i)] = rumps.MenuItem(title=title, callback=callback)
self.buttons_callback[title] = callback
self.interval = get_things_min()*SEC_TO_MIN
self.button_things = rumps.MenuItem(title="Things Interval ("+str(round(self.interval/SEC_TO_MIN))+"min)", callback=lambda _: self.set_things_mins(_))
self.button_things.state = True
self.app.menu = [
self.start_pause_button,
None,
self.button_things,
None,
*self.buttons.values(),
None,
self.stop_button]
def run(self):
self.app.run()
def set_things_mins(self, sender):
pass_interval = get_things_min()
print("pass_interval is now", pass_interval)
self.button_things.title = "Things Interval (" + str(round(pass_interval)) + "min)"
self.set_mins(sender, pass_interval)
def set_mins(self, sender, interval):
for btn in [self.button_things, *self.buttons.values()]:
if sender.title == btn.title:
self.interval = interval*SEC_TO_MIN
btn.state = True
elif sender.title != btn.title:
btn.state = False
def start_timer(self, sender, interval):
for btn in [self.button_things, *self.buttons.values()]:
btn.set_callback(None)
if sender.title.lower().startswith(("start", "continue")):
if sender.title == 'Start Timer':
# reset timer & set stop time
self.timer.count = 0
self.timer.end = interval
# change title of MenuItem from 'Start timer' to 'Pause timer'
sender.title = 'Pause Timer'
# lift off! start the timer
self.timer.start()
else: # 'Pause Timer'
sender.title = 'Continue Timer'
self.timer.stop()
def on_tick(self, sender):
time_left = sender.end - sender.count
mins = time_left // 60 if time_left >= 0 else time_left // 60 + 1
secs = time_left % 60 if time_left >= 0 else (-1 * time_left) % 60
if mins == 0 and time_left < 0:
rumps.notification(title='Timebox',
subtitle='Time is up! Take a break :)',
message='')
self.stop_timer(sender)
self.stop_button.set_callback(None)
else:
self.stop_button.set_callback(self.stop_timer)
self.app.title = '{:2d}:{:02d}'.format(mins, secs)
sender.count += 1
def stop_timer(self, sender=None):
self.timer.stop()
self.timer.count = 0
self.app.title = "🥊"
self.stop_button.set_callback(None)
for key, btn in self.buttons.items():
btn.set_callback(self.buttons_callback[btn.title])
self.button_things.set_callback(lambda _: self.set_things_mins(_))
if self.button_things.state:
self.interval = get_things_min(1)*SEC_TO_MIN
self.button_things.title = "Things Interval ("+str(round(self.interval/SEC_TO_MIN))+"min)"
get_things_min(0, True)
self.start_pause_button.title = 'Start Timer'
if __name__ == '__main__':
app = TimerApp(timer_interval=1)
app.run()