-
Notifications
You must be signed in to change notification settings - Fork 1
/
daily_update.py
executable file
·306 lines (258 loc) · 8.33 KB
/
daily_update.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
#!/usr/bin/env python3
# consider i18n
import sys
import codecs
import datetime
import os
# from http://github.com/brandenburg/python-taskpaper
from taskpaper.taskpaper import TaskPaper
DATE_FORMAT = "%d-%m-%Y"
DATE_TAG = "last_updated"
import optparse
o = optparse.make_option
DAYS = [
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday'
]
MONTHS = [
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december'
]
opts = [
o('-d', '--day', action='store', dest='day',
type='choice', choices=DAYS,
help='pretend today is DAY (default: infer from system date)'),
o('-s', '--simulate', action='store_true', dest='simulate',
help="don't write back results to file; dump to stdout instead"),
o('-r', '--recurring', action='store', dest='recurring',
help="read recurring tasks from file RECURRING"),
o('-t', '--tomorrow', action='store_true', dest='tomorrow',
help="when processing items tagged @monday, @tuesday, etc., tag " +
"events on the next day with '@tomorrow'"),
o('-c', '--catch-up', action='store_true',
help='infer day of last update from archive and run update for all skipped days'),
]
defaults = {
'day' : None,
'simulate' : False,
'recurring' : None,
'tomorrow' : False,
'catch_up' : False,
'date' : None,
}
options = None
ONE_DAY = datetime.timedelta(days=1)
def date():
return datetime.date.today() if options.date is None else options.date
def infer_date():
d = datetime.date.today()
while options.day and DAYS[d.weekday()] != options.day:
d -= ONE_DAY
return d
def day_of_week():
return DAYS[date().weekday()]
def this_month():
return MONTHS[date().month - 1]
def dates_since(d, until=datetime.date.today()):
d += ONE_DAY
while d <= until:
yield d
d += ONE_DAY
def merge_recurring(todos, tag):
if options.recurring:
for nd in options.recurring[tag]:
path = nd.path_from_root()
todos.add_path(path)
def process_countdown(todos, tag):
for nd in todos[tag]:
try:
val = nd.tags[tag]
if val is None:
val = 0
else:
val = int(val)
if val > 2:
nd.tags[tag] = str(val - 1)
elif val == 2:
nd.drop_tag(tag)
nd.add_tag('tomorrow')
elif val <= 1:
nd.drop_tag(tag)
nd.add_tag('today')
except ValueError:
pass # silently ignore strings we can't parse
def advance_day(todos):
# convert a given tag to another given tag (in all nodes)
def convert_to(tag, what):
for nd in todos[tag]:
nd.drop_tag(tag)
nd.add_tag(what)
convert_to_today = lambda tag: convert_to(tag, 'today')
convert_to_tomorrow = lambda tag: convert_to(tag, 'tomorrow')
# everything marked as @tomorrow becomes @today
convert_to_today('tomorrow')
# tolerate frequent misspelling
convert_to_today('tomorow')
# everything explicitly marked by weekday name becomes @today
day = day_of_week()
merge_recurring(todos, day)
# process countdowns
process_countdown(todos, 'indays')
process_countdown(todos, 'snooze')
if options.tomorrow:
# also merge tasks for tomorrow
day_idx = DAYS.index(day)
next_day = DAYS[(day_idx + 1) % len(DAYS)]
merge_recurring(todos, next_day)
convert_to_tomorrow(next_day)
merge_recurring(todos, 'daily')
convert_to_today('daily')
# on certain days also pull in additional items
# The weekend starts on Saturday.
if day == 'saturday':
convert_to_today('weekend')
# The new (work) week starts on Monday.
if day == 'monday':
for nd in todos['nextweek']:
arg = nd.tags['nextweek']
nd.drop_tag('nextweek')
if arg in DAYS:
nd.add_tag(arg)
else:
nd.add_tag('today')
merge_recurring(todos, 'weekly')
convert_to_today('weekly')
if date().day == 1:
merge_recurring(todos, 'monthly')
merge_recurring(todos, this_month())
convert_to_today('monthly')
convert_to_today('nextmonth')
convert_to_today(this_month())
convert_to_today(day)
# convert next-week-$DAY tags to just $DAY
convert_to('n' + day, day)
convert_to('next' + day, day)
def drop_done(todos):
dones = []
for nd in todos['done']:
nd.delete()
dones.append(nd)
return dones
def drop_should(todos):
"deferred items: drop @should from @today items to raise awareness"
for nd in todos['today']:
nd.drop_tag('should')
def update_date_tag(todos):
# first, remove any old tags
for nd in todos[DATE_TAG]:
nd.drop_tag(DATE_TAG)
todos[0].add_tag(DATE_TAG, datetime.datetime.now().date().strftime(DATE_FORMAT))
def archive_done(todos, archive):
today = "%04d-%02d-%02d" % (date().year, date().month, date().day)
for nd in todos['done']:
# remove "stale" tags
for tag in ['today', 'tomorrow', 'weekend', 'nextweek'] + DAYS:
nd.drop_tag(tag)
# add to archive
path = nd.path_from_root()
new = archive.add_path(path)
# check if we added this node before (=> recurrent tasks)
if 'archived' in new.tags:
if new.tags['archived'] != today:
# this is a repeated task -> add another 'archived' marker
new.tags['archived'] += ' ' + today
else:
new.add_tag('archived', today)
# finally remove the completed node from the todo list
nd.delete()
def load_file(fname):
try:
f = codecs.open(fname, 'r', 'utf8')
todo = TaskPaper.parse(f)
f.close()
return todo
except IOError as err:
print("Could not open '%s' (%s)." % (fname, err))
return None
def last_modification_date(todos, fname):
try:
for nd in todos[DATE_TAG]:
try:
return datetime.datetime.strptime(nd.tags[DATE_TAG], DATE_FORMAT).date()
except ValueError:
continue
# if that didn't work, try to get the last modification time from the FS
return datetime.date.fromtimestamp(os.path.getmtime(fname))
except IOError as err:
print("Could not get last modification time of '%s' (%s)." % (fname, err))
return None
def write_file(todos, fname):
try:
f = codecs.open(fname, 'w', 'utf8')
f.write(str(todos))
f.close()
return True
except IOError as msg:
print("Could not store '%s' (%s)." % (fname, err))
return False
def update(todos, archive):
drop_should(todos)
archive_done(todos, archive)
advance_day(todos)
def update_file(fname):
todos = load_file(fname)
if not todos:
return None
archive_fname = fname.replace('.taskpaper', ' Archive.taskpaper')
archive = load_file(archive_fname)
if not archive:
print("Starting new archive: %s" % archive_fname)
archive = TaskPaper()
if options.catch_up:
last_update = last_modification_date(archive, archive_fname)
if not last_update:
return None
for skipped_day in dates_since(last_update):
# carry out updates that we missed
options.date = skipped_day
update(todos, archive)
else:
# regular one-shot update
options.date = infer_date()
update(todos, archive)
# date-tag archive for future catchup-mode invocations
update_date_tag(archive)
if not options.simulate:
write_file(archive, archive_fname)
write_file(todos, fname)
else:
write_file(archive, '/tmp/todo-archive')
write_file(todos, '/tmp/todos-dump')
print(str(todos))
print('*' * 80)
print(str(archive))
def main(args=sys.argv[1:]):
if options.recurring:
options.recurring = load_file(options.recurring)
for fname in args:
update_file(fname)
if __name__ == '__main__':
parser = optparse.OptionParser(option_list=opts)
parser.set_defaults(**defaults)
(options, files) = parser.parse_args()
main(files)