-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.py
executable file
·352 lines (286 loc) · 12.8 KB
/
t.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
#!/usr/bin/env python
"""t is for people that want do things, not organize their tasks."""
from __future__ import with_statement, print_function
import os, re, sys, hashlib
from operator import itemgetter
from optparse import OptionParser, OptionGroup
import random
from colored import bg, attr, fg
class InvalidTaskfile(Exception):
"""Raised when the path to a task file already exists as a directory."""
pass
class AmbiguousPrefix(Exception):
"""Raised when trying to use a prefix that could identify multiple tasks."""
def __init__(self, prefix):
super(AmbiguousPrefix, self).__init__()
self.prefix = prefix
class UnknownPrefix(Exception):
"""Raised when trying to use a prefix that does not match any tasks."""
def __init__(self, prefix):
super(UnknownPrefix, self).__init__()
self.prefix = prefix
class BadFile(Exception):
"""Raised when something else goes wrong trying to work with the task file."""
def __init__(self, path, problem):
super(BadFile, self).__init__()
self.path = path
self.problem = problem
def _hash(text):
"""Return a hash of the given text for use as an id.
Currently SHA1 hashing is used. It should be plenty for our purposes.
"""
return hashlib.sha1(text.encode('utf-8')).hexdigest()
def _get_category(tasktext):
"""Returns a task with its category if it has one."""
catg, _, _ = tasktext.rpartition(':')
task = {'text': tasktext.strip()}
if catg.isalnum():
task['category'] = catg.strip()
return task
def _task_from_taskline(taskline):
"""Parse a taskline (from a task file) and return a task.
A taskline should be in the format:
summary text ... | meta1:meta1_value,meta2:meta2_value,...
The task returned will be a dictionary such as:
{ 'id': <hash id>,
'text': <summary text>,
... other metadata ... }
A taskline can also consist of only summary text, in which case the id
and other metadata will be generated when the line is read. This is
supported to enable editing of the taskfile with a simple text editor.
"""
if taskline.strip().startswith('#'):
return None
elif '|' in taskline:
text, _, meta = taskline.rpartition('|')
task = _get_category(text)
for piece in meta.strip().split(','):
label, data = piece.split(':')
task[label.strip()] = data.strip()
else:
text = taskline.strip()
task = _get_category(text)
task['id'] = _hash(text)
return task
def _tasklines_from_tasks(tasks):
"""Parse a list of tasks into tasklines suitable for writing."""
tasklines = []
for task in tasks:
meta = [m for m in task.items() if m[0] != 'text']
meta_str = ', '.join('%s:%s' % m for m in meta)
tasklines.append('%s | %s\n' % (task['text'], meta_str))
return tasklines
def _prefixes(ids):
"""Return a mapping of ids to prefixes in O(n) time.
Each prefix will be the shortest possible substring of the ID that
can uniquely identify it among the given group of IDs.
If an ID of one task is entirely a substring of another task's ID, the
entire ID will be the prefix.
"""
ps = {}
for id in ids:
id_len = len(id)
for i in range(1, id_len+1):
# identifies an empty prefix slot, or a singular collision
prefix = id[:i]
if (not prefix in ps) or (ps[prefix] and prefix != ps[prefix]):
break
if prefix in ps:
# if there is a collision
other_id = ps[prefix]
for j in range(i, id_len+1):
if other_id[:j] == id[:j]:
ps[id[:j]] = ''
else:
ps[other_id[:j]] = other_id
ps[id[:j]] = id
break
else:
ps[other_id[:id_len+1]] = other_id
ps[id] = id
else:
# no collision, can safely add
ps[prefix] = id
ps = dict(zip(ps.values(), ps.keys()))
if '' in ps:
del ps['']
return ps
def _randcolor():
return bg(random.choice(range(1,16))) +fg(0)
class TaskDict(object):
"""A set of tasks, both finished and unfinished, for a given list.
The list's files are read from disk when the TaskDict is initialized. They
can be written back out to disk with the write() function.
"""
def __init__(self, taskdir='.', name='tasks'):
"""Initialize by reading the task files, if they exist."""
self.tasks = {}
self.done = {}
self.categories = {}
self.name = name
self.taskdir = taskdir
filemap = (('tasks', self.name), ('done', '.%s.done' % self.name))
for kind, filename in filemap:
path = os.path.join(os.path.expanduser(self.taskdir), filename)
if os.path.isdir(path):
raise InvalidTaskfile
if os.path.exists(path):
try:
with open(path, 'r') as tfile:
tls = [tl.strip() for tl in tfile if tl]
tasks = map(_task_from_taskline, tls)
for task in tasks:
if task is not None:
getattr(self, kind)[task['id']] = task
if "category" in task:
if not task["category"] in self.categories:
self.categories[task["category"]] = _randcolor()
except IOError as e:
raise BadFile(path, e.strerror)
def __getitem__(self, prefix):
"""Return the unfinished task with the given prefix.
If more than one task matches the prefix an AmbiguousPrefix exception
will be raised, unless the prefix is the entire ID of one task.
If no tasks match the prefix an UnknownPrefix exception will be raised.
"""
matched = [tid for tid in self.tasks.keys() if tid.startswith(prefix)]
if len(matched) == 1:
return self.tasks[matched[0]]
elif len(matched) == 0:
raise UnknownPrefix(prefix)
else:
matched = [tid for tid in self.tasks.keys() if tid == prefix]
if len(matched) == 1:
return self.tasks[matched[0]]
else:
raise AmbiguousPrefix(prefix)
def add_task(self, text):
"""Add a new, unfinished task with the given summary text."""
task_id = _hash(text)
self.tasks[task_id] = {'id': task_id, 'text': text}
def edit_task(self, prefix, text):
"""Edit the task with the given prefix.
If more than one task matches the prefix an AmbiguousPrefix exception
will be raised, unless the prefix is the entire ID of one task.
If no tasks match the prefix an UnknownPrefix exception will be raised.
"""
task = self[prefix]
if text.startswith('s/') or text.startswith('/'):
text = re.sub('^s?/', '', text).rstrip('/')
find, _, repl = text.partition('/')
text = re.sub(find, repl, task['text'])
task['text'] = text
task['id'] = _hash(text)
def finish_task(self, prefix):
"""Mark the task with the given prefix as finished.
If more than one task matches the prefix an AmbiguousPrefix exception
will be raised, if no tasks match it an UnknownPrefix exception will
be raised.
"""
task = self.tasks.pop(self[prefix]['id'])
self.done[task['id']] = task
def remove_task(self, prefix):
"""Remove the task from tasks list.
If more than one task matches the prefix an AmbiguousPrefix exception
will be raised, if no tasks match it an UnknownPrefix exception will
be raised.
"""
self.tasks.pop(self[prefix]['id'])
def print_list(self, kind='tasks', verbose=False, quiet=False, grep=''):
"""Print out a nicely formatted list of unfinished tasks."""
tasks = dict(getattr(self, kind).items())
label = 'prefix' if not verbose else 'id'
if not verbose:
for task_id, prefix in _prefixes(tasks).items():
tasks[task_id]['prefix'] = prefix
plen = max(map(lambda t: len(t[label]), tasks.values())) if tasks else 0
for _, task in sorted(tasks.items()):
if grep.lower() in task['text'].lower():
p = '%s - ' % task[label].ljust(plen) if not quiet else ''
s = (self.categories[task['category']] + task['category'] + attr('reset') + task['text'][len(task['category']):]) if "category" in task else task['text']
print(p + s)
def write(self, delete_if_empty=False):
"""Flush the finished and unfinished tasks to the files on disk."""
filemap = (('tasks', self.name), ('done', '.%s.done' % self.name))
for kind, filename in filemap:
path = os.path.join(os.path.expanduser(self.taskdir), filename)
if os.path.isdir(path):
raise InvalidTaskfile
tasks = sorted(getattr(self, kind).values(), key=itemgetter('id'))
if tasks or not delete_if_empty:
try:
with open(path, 'w') as tfile:
for taskline in _tasklines_from_tasks(tasks):
tfile.write(taskline)
except IOError as e:
raise BadFile(path, e.strerror)
elif not tasks and os.path.isfile(path):
os.remove(path)
def _build_parser():
"""Return a parser for the command-line interface."""
usage = "Usage: %prog [-t DIR] [-l LIST] [options] [TEXT]"
parser = OptionParser(usage=usage)
actions = OptionGroup(parser, "Actions",
"If no actions are specified the TEXT will be added as a new task.")
actions.add_option("-e", "--edit", dest="edit", default="",
help="edit TASK to contain TEXT", metavar="TASK")
actions.add_option("-f", "--finish", dest="finish",
help="mark TASK as finished", metavar="TASK")
actions.add_option("-r", "--remove", dest="remove",
help="Remove TASK from list", metavar="TASK")
parser.add_option_group(actions)
config = OptionGroup(parser, "Configuration Options")
config.add_option("-l", "--list", dest="name", default="tasks",
help="work on LIST", metavar="LIST")
config.add_option("-t", "--task-dir", dest="taskdir", default="",
help="work on the lists in DIR", metavar="DIR")
config.add_option("-d", "--delete-if-empty",
action="store_true", dest="delete", default=False,
help="delete the task file if it becomes empty")
parser.add_option_group(config)
output = OptionGroup(parser, "Output Options")
output.add_option("-g", "--grep", dest="grep", default='',
help="print only tasks that contain WORD", metavar="WORD")
output.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="print more detailed output (full task ids, etc)")
output.add_option("-q", "--quiet",
action="store_true", dest="quiet", default=False,
help="print less detailed output (no task ids, etc)")
output.add_option("--done",
action="store_true", dest="done", default=False,
help="list done tasks instead of unfinished ones")
parser.add_option_group(output)
return parser
def _main():
"""Run the command-line interface."""
(options, args) = _build_parser().parse_args()
td = TaskDict(taskdir=options.taskdir, name=options.name)
text = ' '.join(args).strip()
try:
if options.finish:
td.finish_task(options.finish)
td.write(options.delete)
elif options.remove:
td.remove_task(options.remove)
td.write(options.delete)
elif options.edit:
td.edit_task(options.edit, text)
td.write(options.delete)
elif text:
td.add_task(text)
td.write(options.delete)
else:
kind = 'tasks' if not options.done else 'done'
td.print_list(kind=kind, verbose=options.verbose, quiet=options.quiet,
grep=options.grep)
except AmbiguousPrefix:
e = sys.exc_info()[1]
sys.stderr.write('The ID "%s" matches more than one task.\n' % e.prefix)
except UnknownPrefix:
e = sys.exc_info()[1]
sys.stderr.write('The ID "%s" does not match any task.\n' % e.prefix)
except BadFile as e:
sys.stderr.write('%s - %s\n' % (e.problem, e.path))
if __name__ == '__main__':
_main()