-
Notifications
You must be signed in to change notification settings - Fork 1
/
pymato.py
404 lines (350 loc) · 11.3 KB
/
pymato.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Pymato version 0.1.9rc2
# 0.1.9 - UNRELEASED
- Added statuses in the pomodorouboros style - achieved, focused, distracted,
and never evaluated.
"""
import re
import cmd
import os
import sys
import time
from collections import namedtuple
from datetime import datetime, timedelta
from pathlib import Path
import pytz
from tzlocal import get_localzone
__version__ = '0.1.9rc2'
POM_MINS = 25
STATUS_MAP = {
'achieved': '+',
'focused': '=',
'distracted': '-',
'never evaluated': '?',
None: '?',
}
STATUS_SYMBOLS = {
'+': 'achieved',
'=': 'focused',
'-': 'distracted',
'?': 'never evaluated',
}
# Yoinked from pomodorouboros
POINTS = {
'+': (1.25, 0.0),
'=': (1.0, 0.0),
'-': (0.25, 1.0),
'?': (0.1, 1.0),
}
LOG_PATTERN = re.compile(r'(?P<status>[=+-?]?)\s*(?P<start>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{4}) (?P<end>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{4})(?P<title>.*)$')
def hms(seconds):
'''
Take a number of seconds and return hours, minutes, and seconds
'''
seconds = int(seconds)
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
return h, m, s
def pretty_hms(h=0, m=0, s=0):
'''
Return the provided h, m, and s in the format HhMmSs. If any of the
values are 0, then they will be blank spaces unless *all* values are zero,
in which case, return ' 0s'.
>>> pretty_hms()
' 0s'
>>> pretty_hms(0,0,0)
' 0s'
>>> pretty_hms(1,0,0)
' 1h '
>>> pretty_hms(1,2,5)
' 1h 2m 5s'
>>> pretty_hms(1,2,50)
' 1h 2m50s'
>>> pretty_hms(99,23,50)
'99h23m50s'
'''
hrs = f'{h:>2}h' if h else ' '*3
mins = f'{m:>2}m' if m else ' '*3
secs = f'{s:>2}s' if s else ' '*3
elapsed_time = f'{hrs}{mins}{secs}'
return elapsed_time if elapsed_time.strip() else ' 0s'
def parse_logs(logfile):
logfile.seek(0)
log = []
date_pat = '%Y-%m-%d %H:%M:%S%z'
for line in logfile:
m = re.search(LOG_PATTERN, line)
# TODO: How should I handle failed parsing log lines? -W. Werner, 2021-10-30
if m['end'].strip() == '':
end_timestamp = None
else:
end_timestamp = datetime.strptime(
m['end'],
date_pat,
)
log.append(LogEntry(
start=datetime.strptime(m['start'], date_pat),
end=end_timestamp,
title=m['title'].strip(),
status=STATUS_SYMBOLS.get(m['status'], '?'),
))
logfile.seek(0)
return log
class LogEntry:
def __init__(self, start, end, title, status):
self.start = start
self.end = end
self.title = title
self.status = status
def __repr__(self):
return f'LogEntry(title={self.title!r}, start={self.start!r}, end={self.end!r}, status={self.status!r})'
def __lt__(self, other):
return self.start < other.start
def format(self, datefmt, tz=pytz.utc):
start = self.start.astimezone(tz).strftime(datefmt)
if self.end:
end = self.end.astimezone(tz).strftime(datefmt)
else:
end = ' ' * len(start)
return f'{STATUS_MAP.get(self.status, "?")} {start} {end} {self.title}'
def elapsed_format(self, tz=pytz.utc):
elapsed = self.end - self.start
return f'{pretty_hms(*hms(elapsed.total_seconds())):>6} {self.title}'
@property
def elapsed_seconds(self):
end = self.end or datetime.now(tz=pytz.utc)
return (self.end-self.start).total_seconds()
class Orouboros(cmd.Cmd):
prompt = "pymatorouboros> "
def __init__(self, *args, logfile=None, **kwargs):
super().__init__(*args, **kwargs)
self._logfile = logfile
self.logs = parse_logs(self._logfile)
@property
def score(self):
today = datetime.now().date()
points_earned = 0
points_lost = 0
for log in self.logs:
if log.start.date() == today:
plus, minus = POINTS[STATUS_MAP[log.status]]
points_earned += plus
points_lost += minus
return f'+{points_earned:.1f}/-{points_lost:.1f}'
@property
def current(self):
now = datetime.now(pytz.utc)
for log in self.logs:
if log.start <= now <= log.end:
return log
return None
@property
def status(self):
cur = self.current
return ' ' if cur is None else STATUS_MAP[cur.status]
@property
def active(self):
cur = self.current
if cur is None:
return 'No active pom - start to get started'
else:
return cur.title
@property
def prompt(self):
self.logs = parse_logs(self._logfile)
return f"pymatorouboros ({self.score}):{self.status}:{self.active}> "
def dump_logs(self):
date_pat = '%Y-%m-%d %H:%M:%S%z'
self.logs.sort()
self._logfile.seek(0)
self._logfile.truncate()
for entry in self.logs:
print(
entry.format(date_pat),
file=self._logfile,
)
self._logfile.flush()
def do_status(self, line):
"""
status +/=/-/?
+ achieved goal
= focused on goal
- distracted from goal
? did not actually evaluate goal
"""
status = line.strip()
cur = self.current
if cur is None:
print('ERROR: Cannot set status of non-existent pom!')
elif status not in STATUS_SYMBOLS:
print(f'ERROR: {status!r} is not a valid status')
else:
cur.status = STATUS_SYMBOLS[status]
self.dump_logs()
def do_title(self, line):
'''
title ...
Set the title of the current pom.
'''
title = line.strip()
cur = self.current
if cur is None:
print('ERROR: Cannot set title for non-existent pom!')
elif not line:
print('ERROR: Gotta set a title')
else:
cur.title = line.strip()
self.dump_logs()
def do_quit(self, line):
return True
def do_EOF(self, line):
return self.do_quit(line)
do_q = do_quit
class Pymato(cmd.Cmd):
prompt = 'pymato> '
def __init__(self, *args, logfile=None, **kwargs):
super().__init__(*args, **kwargs)
self._logfile = logfile
self.log = []
#2018-01-04 16:16-06:00
self.date_pat = '%Y-%m-%d %H:%M:%S%z'
self._logfile.seek(0)
for line in self._logfile:
m = re.search(LOG_PATTERN, line)
# TODO: How should I handle failed parsing log lines? -W. Werner, 2021-10-30
if m['end'].strip() == '':
end_timestamp = None
else:
end_timestamp = datetime.strptime(
m['end'],
self.date_pat,
)
self.log.append(LogEntry(
start=datetime.strptime(m['start'], self.date_pat),
end=end_timestamp,
title=m['title'].strip(),
status=STATUS_SYMBOLS.get(m['status'], '?'),
))
# TODO: Maybe make pymato a context manager? -W. Werner, 2018-01-04
self._logfile.seek(0)
self._logfile.truncate()
self.log.sort()
for entry in self.log:
print(
entry.format(self.date_pat),
file=self._logfile,
)
self._logfile.flush()
def add_log(self, title, start, end):
self.log.append(LogEntry(
start=start,
end=end,
title=title,
))
print(
self.log[-1].format(self.date_pat),
file=self._logfile,
)
self._logfile.flush()
def do_log(self, line):
'''
Display a log of your time spent.
'''
def logfilter(entry): return True
if line.strip():
try:
filterdate = datetime.strptime(line.strip(), '%Y-%m-%d').date()
def logfilter(entry):
return entry.start.date() == filterdate
except ValueError:
def logfilter(entry):
return entry.title.lower() == line.lower()
self.log.sort()
printlogs = [log for log in sorted(self.log) if logfilter(log)]
if printlogs:
date = printlogs[0].start.date()
print(date)
total_time = 0
for log in printlogs:
if not logfilter(log):
continue
if log.start.date() != date:
date = log.start.date()
print(f'\n{date}')
print(f'\t{log.elapsed_format(tz=get_localzone())}')
total_time += log.elapsed_seconds
print('-'*30)
print(f'\t{pretty_hms(*hms(total_time)):>9} total pomodoro time')
def do_track(self, line):
'''
Start tracking time on an open-ended task. ^c to stop. You will
have the option to save (the default) or discard the time.
'''
start = datetime.now(pytz.utc)
try:
print('Task -', line)
while True:
diff = datetime.now(pytz.utc)-start
print('\r', str(diff).split('.')[0], sep='', end='')
time.sleep(1)
except KeyboardInterrupt:
end = datetime.now(pytz.utc)
print('\n{} time spent, save?'.format(str(diff).split('.')[0]))
choice = input('[y]/n: ').strip()
if choice.lower() in ('y', 'yes', ''):
self.add_log(title=line, start=start, end=end)
def do_pom(self, line):
'''
Start recording a pom, with whatever description you provide.
^c interrupts your pom, though you can save your current progress.
'''
start = datetime.now(pytz.utc)
end = start + timedelta(minutes=POM_MINS, seconds=0)
try:
print('Task -', line)
while datetime.now(pytz.utc) <= end:
diff = end-datetime.now(pytz.utc)
print('\r', str(diff).split('.')[0], sep='', end='')
time.sleep(1)
print('\nDone!\a')
except KeyboardInterrupt:
end = datetime.now(pytz.utc)
print('Aborted - save to log anyway?')
choice = input('y/[n]: ')
if choice.lower() in ('y', 'yes'):
self.add_log(title=line, start=start, end=end)
else:
self.add_log(title=line, start=start, end=end)
def do_orouboros(self, line):
o = Orouboros(logfile=self._logfile)
if line:
o.onecmd(line)
else:
o.cmdloop()
def do_version(self, line):
print(f'pymato {__version__}')
def do_quit(self, line):
'''
Quit
'''
return True
def do_EOF(self, line):
'''
Quit
'''
return self.do_quit(line)
do_q = do_quit
def main():
logfile = Path(os.environ.get('PYMATO_LOGFILE', 'pymato.log'))
logfile.touch(exist_ok=True)
with logfile.open('r+') as f:
mato = Pymato(logfile=f)
try:
if len(sys.argv) > 1:
mato.onecmd(' '.join(sys.argv[1:]))
else:
Pymato(logfile=f).cmdloop()
except KeyboardInterrupt:
print('\nBye!')
if __name__ == '__main__':
main()