-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevlogger.py
More file actions
executable file
·69 lines (56 loc) · 2.09 KB
/
Copy pathdevlogger.py
File metadata and controls
executable file
·69 lines (56 loc) · 2.09 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
#!/usr/bin/env python3
import os
import re
import sys
import time
import argparse
# A logged entry starts with a "HH:MM AM - " timestamp; wrapped continuation
# lines are indented by 11 spaces (the width of that timestamp prefix).
_TS = re.compile(r'^\d{1,2}:\d{2} [AP]M - ')
_CONT_INDENT = ' ' # 11 spaces, matches devlog()'s wrap indent
def read_today():
"""Return today's devlog entries as whole strings (timestamp stripped,
wrapped lines rejoined). Empty list if there is no log for today."""
folder_path = os.path.join(os.path.expanduser('~'), 'devlogs')
day_str = time.strftime("%A_%b%d_%Y")
path = '%s/%s.txt' % (folder_path, day_str)
if not os.path.isfile(path):
return []
entries = []
with open(path) as f:
for line in f.read().split('\n'):
if not line.strip():
continue
match = _TS.match(line)
if match:
entries.append(line[match.end():])
elif line.startswith(_CONT_INDENT) and entries:
entries[-1] += ' ' + line.strip()
else:
entries.append(line.strip())
return entries
def devlog(text):
folder_path = os.path.join(os.path.expanduser('~'), 'devlogs')
day_str = time.strftime("%A_%b%d_%Y")
time_str = time.strftime("%I:%M %p")
fn = '%s/%s.txt' % (folder_path, day_str)
max_len = 69
text_sections = []
if len(text) <= max_len:
text_sections = [text]
else:
words = text.split()
while len(words):
for i in range(len(words)):
if len(' '.join(words[:i+1])) > max_len:
ind = i
break
text_sections.append(' '.join(words[:ind]))
words = words[ind:]
final_text = '%s - %s\n' % (time_str, text_sections[0])
if len(text_sections) > 1:
final_text = '%s%s\n' % (final_text, '\n'.join(["%s%s" % (' ', section) for section in text_sections[1:]]))
if not os.path.isdir(folder_path):
os.mkdir(folder_path)
with open(fn, 'a') as f:
f.write(final_text)