-
Notifications
You must be signed in to change notification settings - Fork 59
/
pomodoro
executable file
·199 lines (170 loc) · 5.95 KB
/
pomodoro
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
#!/usr/bin/env python
# Adapted from : http://code.activestate.com/recipes/577358-pomodoro-timer/
from __future__ import print_function
from clize import run
from sigtools import modifiers
import sys
import time
import subprocess
from datetime import datetime
import os
import configparser
try:
from PyQt5.QtWidgets import QMessageBox
from PyQt5.Qt import QApplication
except ImportError:
has_qt = False
else:
has_qt = True
display = print
script_dir = os.path.dirname(__file__)
ALARM_FILE_DIRS = [
'.',
script_dir,
sys.prefix,
os.path.join(script_dir, "..", ".."),
]
ALARM_FILENAME = 'clock.mp3'
for alarm_file_dir in ALARM_FILE_DIRS:
ALARM_FILE = os.path.join(alarm_file_dir, ALARM_FILENAME)
if os.path.exists(ALARM_FILE):
break
ALARM_CMD_FFPLAY = ["ffplay", "-nodisp", "-autoexit"]
ALARM_CMD_MPG123 = ["mpg123"]
ALARM_CMDS = (ALARM_CMD_MPG123, ALARM_CMD_FFPLAY)
DATA_FILENAME = os.path.expanduser("~/.pomodoro")
CONFIG_FILENAME = os.path.expanduser("~/.pomodoro.conf")
DEV_NULL = open(os.devnull, "w")
# Parsing config file :
config = configparser.ConfigParser()
config.read(CONFIG_FILENAME)
if 'DEFAULTS' not in config : #if the config doesn't exist or is of the wrong format
#writes the default config to the file
config['DEFAULTS'] = {'work':'25', 'rest':'5', 'long':'15', 'cycles':'4', 'start':'0', 'repeat':'0', 'alarm':'True', 'notif':'False', 'timer':'False'}
with open(CONFIG_FILENAME, 'w') as configfile:
config.write(configfile)
config.read(CONFIG_FILENAME)
work_default = config['DEFAULTS'].getint('work', fallback='25')
rest_default = config['DEFAULTS'].getint('rest', fallback='5')
long_default = config['DEFAULTS'].getint('long', fallback='15')
cycles_default = config['DEFAULTS'].getint('cycles', fallback='4')
start_default = config['DEFAULTS'].getint('start', fallback='0')
repeat_default = config['DEFAULTS'].getint('repeat', fallback='0')
alarm_default = config['DEFAULTS'].getboolean('alarm', fallback='True')
notif_default = config['DEFAULTS'].getboolean('notif', fallback='False')
timer_default = config['DEFAULTS'].getboolean('timer', fallback='True')
def notify(title, content, more=''):
if not has_qt:
return
app = QApplication(sys.argv)
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setText(content)
msg.setWindowTitle(title)
msg.setDetailedText(more)
msg.show()
app.exec_()
def tick(duration, timer):
try:
if timer:
cli_timer(duration)
else:
time.sleep(duration)
except KeyboardInterrupt:
display("Interrupting")
interrupt = True
else:
interrupt = False
return interrupt
def play_alarm(filename):
for alarm_cmd in ALARM_CMDS:
cmd = alarm_cmd + [filename]
try:
p = subprocess.Popen(cmd, stdout=DEV_NULL, stderr=subprocess.PIPE)
p.wait()
except OSError:
# error, try the next alarm cmd
continue
else:
# successful
return
def write_pomo(start, stop, tag):
duration = (stop - start).total_seconds() / 60.
line = "{0},{1},{2},{3}\n".format(tag, start, stop, duration)
if not os.path.exists(DATA_FILENAME):
fd = open(DATA_FILENAME, "w")
fd.write("work,start,end,duration\n")
else:
fd = open(DATA_FILENAME, "a")
fd.write(line)
fd.close()
def cli_timer(duration):
for remaining in range(duration, -1, -1):
sys.stdout.write("\r")
hours, rem = divmod(remaining, 3600)
mins, secs = divmod(rem, 60)
sys.stdout.write("Time remaining: {:0>2}:{:0>2}:{:0>2}".format(hours, mins, secs))
sys.stdout.flush()
time.sleep(1)
print('\n')
@modifiers.kwoargs('long', 'cycles', 'start', 'repeat', 'alarm', 'notif', 'timer')
def main(work=work_default, rest=rest_default, long=long_default, cycles=cycles_default, start=start_default, repeat=repeat_default, alarm=alarm_default, notif=notif_default, timer=timer_default):
"""
work : int
nb of minuntes of work
rest : int
nb of minutes of rest
long : int
nb of minutes of long rest after completion of a set
cycles : int
nb of cycles in a set
start : int
nb of cycles already completed in the set
repeat : int
nb of cycles work-rest to do (use 0 to continue until user interruption)
alarm : bool
whether to play an alarm each time a pomodoro is finished or started
notif : bool
whether to send a message box each time a pomodoro is finished or
started
timer : bool
whether to have cli timer visible
"""
cycle = start
while repeat <= 0 or cycle != repeat:
# If repeat is >0, continue until cycle == repeat. If repeat <=0, continue until user interrupts.
display("Work now, cycle {}/{}".format((cycle % cycles) + 1, cycles))
start = datetime.now()
interrupted = tick(int(work) * 60, timer)
if interrupted:
break
stop = datetime.now()
write_pomo(start, stop, "work")
if alarm:
play_alarm(ALARM_FILE)
if (cycle + 1) % cycles == 0:
# If set complete, long rest
rest_time = int(long) * 60
if notif:
notify('pomodoro', 'Finished set, long rest now.')
display("Long rest now")
else:
# If partial set, normal (short) rest
rest_time = int(rest) * 60
if notif:
notify('pomodoro', 'Finished pomo, rest now.')
display("Rest now")
start = datetime.now()
interrupted = tick(rest_time, timer)
if interrupted:
break
stop = datetime.now()
write_pomo(start, stop, "rest")
if alarm:
play_alarm(ALARM_FILE)
if notif:
notify('pomodoro', 'Finished rest, work now.')
display("Cycle complete")
cycle += 1
if __name__ == "__main__":
run(main)