-
Notifications
You must be signed in to change notification settings - Fork 0
/
todotxt.py
executable file
·399 lines (317 loc) · 12.8 KB
/
todotxt.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
#!/usr/bin/env python3
"""
This roughly follows the todotxt standard to describe hierarchies of todo items in a flat text file format.
For example
```
This is a sprint
This is a story
This is a (new) task in that story
This task is in progress status:doing
x This task is done
This task is assigned to @martin
This Task has it's description included
I'm part of the description
Which can be multi line of course
It can also have child tasks like this
```
You can assign whatever meaning you want to the nesting, So it might be Sprint / Story / Task
for you, or rather Milestone / Epic / Task - whatever you need.
The text format is not yet set in stone, as I'm still trying to find out what works best.
Planning for this project is supposed to happen in the todo.txt file in this directory. Look there for inspiration.
Some syntax ideas that are not yet very very final
- Would it be better to use the established syntax of #234 as a ticket id?
- Is there a better / cleaner way to display the task status? Currently its nothing for
`new` `status:doing` and `x ...` for done tasks. I could think about going :doing, or *doing* or whatever
"""
import re
from functools import wraps
import uuid
import fluentpy as _
import json
def tupelize(method):
@wraps(method)
def wrapper(*args, **kwargs):
return tuple(method(*args, **kwargs))
return wrapper
def id_generator():
if not hasattr(id_generator, 'counter'):
id_generator.counter = 0
id_generator.counter -= 1
return id_generator.counter
class FilterableList(list):
def __init__(self, parent):
super()
self._parent = parent
@property
def tagged(self):
return self
# REFACT consider putting them on their own class, to have a smaller interface
@property
@tupelize
def new(self):
candidates = self._parent.children_tagged('status:new') \
+ self._parent.children_not_tagged('status:')
done = self._parent.children.tagged.done
for child in self:
if child in candidates and child not in done:
yield child
@property
def doing(self):
return self._parent.children_tagged('status:doing')
@property
@tupelize
def done(self):
done_tasks = _(self._parent.children).filter(_.each.is_done._)._ + self._parent.children_tagged('status:done')
for child in self._parent.children:
if child in done_tasks:
yield child
@property
@tupelize
def unknown(self):
known_tagged = self.new + self.doing + self.done
for child in self:
if child not in known_tagged:
yield child
# TODO consider to retain offsets of all matches, so they can easily be used for a highlighter
class Todo:
"int: how many spaces is one level of indentation."
INDENT = 4
class Parser:
IS_DONE = re.compile(r'^\s*(x)\s')
ID = re.compile(r'#(\d+)')
CONTEXTS = re.compile(r'@(\w+)')
PROJECTS = re.compile(r'\+(\w+)')
# TODO consider allowing escaped ' and " in the strings. Perhaps easier to allow ''' and """.
TAGS = re.compile(r'''
(\w+):
(?:(?P<simple>[\-\w]+)
|\'(?P<single>[\-\w\s]+)\'
|\"(?P<double>[\-\w\s]+)\")
''', flags=re.X
)
@classmethod
def is_whitespace(cls, line):
return line.strip() == ''
@classmethod
def indentation_level(cls, line):
if line is None:
return -1
return len(cls.prefix(line)) // Todo.INDENT
@classmethod
def prefix(cls, line):
return re.match(r'^(\s*)', line).groups()[0] or ''
def __init__(self, line=None, body=None):
self.line = line
self.body = body
# REFACT lazy create
self.children = FilterableList(self)
@classmethod
def from_lines(cls, lines):
"""Tasks become children of Tasks when they are indented by two spaces after another task.
"""
root = Todo(line=None, body=None)
for line in lines.split('\n'):
root.append_body_or_child(line)
if root.body or 1 != len(root.children):
return root
return root.children[0]
def append_body_or_child(self, line):
current_indentation_level = self.Parser.indentation_level(self.line)
indentation_level = self.Parser.indentation_level(line)
is_body_line = indentation_level > 1 + current_indentation_level
if self.Parser.is_whitespace(line) or is_body_line:
if self.has_children():
return self.children[-1].append_body_or_child(line)
else:
return self.add_body_line(line)
else:
return self.children.append(Todo(line))
# REFACT consider inlining
@property
def is_virtual(self):
return self.line is None
def add_body_line(self, line):
if self.body is None:
self.body = line
return
self.body += '\n' + line
def __str__(self):
lines = []
if self.line is not None:
lines.append(self.line)
if self.body is not None:
lines.append(self.body)
lines.extend(map(str, self.children))
return '\n'.join(lines)
def __repr__(self):
return f'<Todo(line={self.line!r}, body={self.body!r} children={self.children!r})>'
@property
def uuid(self):
if not hasattr(self, '_uuid'):
self._uuid = str(uuid.uuid4())
return self._uuid
# REFACT this should really use a dict as cache or as primary lookup structure
def task_by_uuid(self, uuid):
if self.uuid == uuid:
return self
for child in self.children:
child_or_none = child.task_by_uuid(uuid)
if child_or_none is not None:
return child_or_none
@property
def id(self):
ids = self.Parser.ID.findall(self.line)
assert len(ids) in (0,1), 'Detected more than one ID for this task'
if 0 == len(ids):
return
return ids[0]
# REFACT consider to remove, self.status should be easier to work with
@property
def is_done(self):
return bool(self.Parser.IS_DONE.match(self.line)) \
or self.has_tags('status:done')
@property
def contexts(self):
return self.Parser.CONTEXTS.findall(self.line)
@property
def projects(self):
return self.Parser.PROJECTS.findall(self.line)
@property
def tags(self):
if not self.line:
return dict()
matches = self.Parser.TAGS.findall(self.line)
return dict((
match[0],
match[1] or match[2] or match[3]
) for match in matches)
@property
def status(self):
# FIXME find a way to make these status configurable
# REFACT consider using is: tag for status because of shortness
if self.has_tags('status:'):
status = self.tags['status']
if status in 'new doing done'.split():
return status
else:
return 'unknown'
if self.is_done:
return 'done'
if self.has_no_tags('status:'):
return 'new'
def has_children(self):
return len(self.children) > 0
@property
def json(self):
if self.is_virtual:
return dict(
body=self.body,
children=[child.json for child in self.children],
)
return dict(
uuid=self.uuid,
line=self.line,
id=self.id,
body=self.body,
is_done=self.is_done,
status=self.status,
contexts=self.contexts,
projects=self.projects,
tags=self.tags,
children=[child.json for child in self.children],
)
def on_operation(self, operation_name, **json):
if operation_name in ['change_tag']:
self.json = json
elif operation_name in ['add_child']:
child = Todo()
child.json = json
self.children.append(child)
else:
assert False, f'operation {operation_name} is not supported'
@json.setter
def json(self, json):
"""Updates the line from the json properties
Currently very basic, in that it only really updates the status
Everything else needs to happen later
TODO allow updating all parts via json
To make this method viable, it probably needs that the client files
minimal update bundles, that are then applied as an update that only changes one aspect?
Not sure how this is to work with child tasks.
"""
# REFACT Gnarly code, not sure how to write this more beautifull
if 'line' in json:
self.line = json['line']
if 'body' in json:
self.body = json['body']
if 'status' in json:
json.setdefault('tags', {})['status'] = json.get('status')
if 'id' in json:
if json['id'] and not self.id:
self.line += f' #{json["id"]}'
else:
self.edit(remove=f'#{self.id}', replace_with=f" #{json['id']}")
if json.get('tags', {}) or self.tags:
for existing_key, existing_value in self.tags.items():
if existing_key not in json.get('tags', {}) or existing_value != json.get('tags', {}).get(existing_key):
self.edit(remove=f'{existing_key}:{existing_value}') # FIXME handle quoting
existing_tags = self.tags
for key, value in json.get('tags', {}).items():
if key not in existing_tags:
self.line += f' {key}:{value}' # FIXME handle quoting
# normalize status tags
if self.has_tags('status:new'):
self.edit(remove='status:new')
if self.has_tags('status:done'):
self.edit(remove='status:done')
json['is_done'] = True
# REFACT Could be that it is signifficantly easier to just reflect the status
# as it's own syntactic ting instead of hijacking tags
if 'is_done' in json:
if json['is_done'] and not self.is_done:
self.line = re.sub(r'(^\s*)(.*$)', r'\1x \2', self.line)
elif not json['is_done'] and self.is_done:
self.line = re.sub(r'(^\s*)x\s+\b(.*$)', r'\1\2', self.line)
if json.get('children', []):
json_children = json.get('children', [])
if len(json_children) > len(self.children):
for _ in range((len(json_children) - len(self.children))):
self.children.append(Todo())
elif len(json_children) < len(self.children):
self.children = self.children[:len(json_children)]
for child, child_json in zip(self.children, json_children):
child.json = child_json
def edit(self, remove=None, remove_re=None, replace_with=' '):
if remove is not None:
self.line, ignored = re.subn(r'\s+' + re.escape(remove) + r'(\s+|$)', replace_with, self.line)
if remove_re is not None:
self.line, ignored = re.subn(remove_re, replace_with, self.line)
self.line = self.line.rstrip()
@tupelize
def children_tagged(self, *tags):
for child in self.children:
if child.has_tags(*tags):
yield child
@tupelize
def children_not_tagged(self, *tags):
"Does not have any of the tags given"
for child in self.children:
if child.has_no_tags(*tags):
yield child
def has_tags(self, *tags):
"Accepts 'tag:' if the value doesn't matter or 'tag:foo' for specific values"
for tag in tags:
key, value = tag.split(':')
if value == '': # value doesn't matter:
if key not in self.tags:
return False
else:
if self.tags.get(key, None) != value:
return False
return True
def has_no_tags(self, *tags):
"Accepts 'tag:' if the value doesn't matter or 'tag:foo' for specific values"
for tag in tags:
if self.has_tags(tag):
return False
return True