forked from EpocDotFr/todotxtio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todotxtio.py
700 lines (507 loc) · 18.7 KB
/
todotxtio.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# -*- coding: utf-8 -*-
import os
import regex as re
import io
__version__ = '1.1.2'
__all__ = [
'from_dicts',
'from_stream',
'from_file',
'from_string',
'to_dicts',
'to_stream',
'to_file',
'to_string',
'Todo',
'search'
]
#
# regular expressions
#
# all of a text line's elements are identified using regular expressions. these
# regular expressions definitions will be found below. please note that not
# every specific term has to be defined, as "tag" attributes hold every
# non-list elements defined by the general "colon" syntax.
# line prefix data
todo_data_regex = re.compile( \
'^(?:(x) )?' + \
'(?:(\d{4}-\d{2}-\d{2}) )?' + \
'(?:\(([A-Z])\) )?' + \
'(?:(\d{4}-\d{2}-\d{2}) )?' \
)
# project and subproject names (ignore single "+" chars)
todo_project_regex = re.compile(' \+(\w\S+)')
# context and subcontext names
todo_context_regex = re.compile(' @(\S*)')
# author names
todo_authors_regex = re.compile(' \[\*(\S*)\]')
# responsible person names
todo_responsibles_regex = re.compile(' \[([^\+\*\s]*)\]')
# names of regarded persons
todo_tobeinformed_regex = re.compile(' \[\+(\S*)\]')
# file and hyperlinks
todo_filelink_regex = re.compile(' (http://|https://|link:)(\S*)')
# text block of remarks
# todo_remarks_regex = re.compile(' \{([^\{\}]*)\}') # this one does not cover necessary recursions
todo_remarks_regex = re.compile('{((?:[^{}]|(?R))*)}')
# all other information as tags
todo_tag_regex = re.compile(' ([A-z]\S*?):(\S*)')
#
# input functions
#
def from_dicts(todos):
"""
Convert a list of todo dicts to a list of :class:`todotxtio.Todo` objects.
:param list todos: A list of todo dicts
:rtype: list
"""
return [Todo(**todo) for todo in todos]
def from_stream(stream, close=True):
"""
Load a todo list from an already-opened stream.
:param file stream: A file-like object
:param bool close: Whetever to close the stream or not after all operation are finised
:rtype: list
"""
string = stream.read()
if close:
stream.close()
return from_string(string)
def from_file(file_path, encoding='utf-8'):
"""
Load a todo list from a file.
:param str file_path: Path to the file
:param str encoding: The encoding of the file to open
:rtype: list
"""
if not os.path.isfile(file_path):
raise FileNotFoundError('File doesn\'t exists: ' + file_path)
stream = io.open(file_path, 'r', encoding=encoding)
return from_stream(stream)
def from_string(string):
"""
Load a todo list from a string.
:param str string: The string to parse
:rtype: list
"""
todos = []
#
# evaluate each line
#
for line in string.strip().splitlines():
line = line.strip()
todo_pre_data = todo_data_regex.match(line)
todo = Todo()
#
# evaluate prefix data
#
if todo_pre_data:
todo.completed = todo_pre_data.group(1) == 'x'
if todo.completed:
todo.creation_date = todo_pre_data.group(4)
if todo_pre_data.group(2):
todo.completion_date = todo_pre_data.group(2)
else:
if todo_pre_data.group(4):
todo.creation_date = todo_pre_data.group(4)
else:
todo.creation_date = todo_pre_data.group(2)
todo.priority = todo_pre_data.group(3)
text = todo_data_regex.sub('', line).strip()
else:
text = line
#
# evaluate remarks
#
# detect and remove remarks from todotxt line at erliest possible time
# in order to not allow further token identification within the remarks
# but only the remaining text section. otherwise, the usage of tokens
# within the remarks would have the potential to mess up the task's
# metadata.
# TECH CONCEPT
# especially task management systems like GitLab insert their own
# special characters into the remarks section of tasks. e.g. when
# providing Markdown or other formats. this might lead to "nested"
# curly braces, which would be mistakenly identified as delimiters for
# remarks when evaluated. to solve this, a "recursive" regex mechanism
# is needed. so, the "re" library has been replaced by the "regex"
# library which has a compatible interface but provides recursive
# functionalities.
# get all remark portions as a list of strings
todo_remarks = todo_remarks_regex.findall(text)
if todo_remarks:
# concatenate portions
todo_remarks = '\\'.join(todo_remarks)
# translate LINE BREAKS
todo.remarks = todo_remarks.replace('\\','\n')
# remove all remark portions from text
text = todo_remarks_regex.sub('', text).strip()
#
# evaluate contexts and projects
#
# projects
todo_projects = todo_project_regex.findall(text)
if len(todo_projects) > 0:
todo.projects = todo_projects
text = todo_project_regex.sub('', text).strip()
# contexts
todo_contexts = todo_context_regex.findall(text)
if len(todo_contexts) > 0:
todo.contexts = todo_contexts
text = todo_context_regex.sub('', text).strip()
#
# evaluate links
#
# http, https, link
todo_links = todo_filelink_regex.findall(text)
if todo_links:
todo.links = []
for _prot, _path in todo_links:
# check for colon
_idx = _prot.find(':')
# build link entry
todo.links.append(
[ _prot[:_idx], _path ]
)
# remove identified content from text
text = todo_filelink_regex.sub('', text).strip()
#
# evaluate persons
#
# responsibles
todo_responsibles = todo_responsibles_regex.findall(text)
if len(todo_responsibles) > 0:
todo.responsibles = todo_responsibles
text = todo_responsibles_regex.sub('', text).strip()
# tobeinformed
todo_tobeinformed = todo_tobeinformed_regex.findall(text)
if len(todo_tobeinformed) > 0:
todo.tobeinformed = todo_tobeinformed
text = todo_tobeinformed_regex.sub('', text).strip()
# authors
todo_authors = todo_authors_regex.findall(text)
if len(todo_authors) > 0:
todo.authors = todo_authors
text = todo_authors_regex.sub('', text).strip()
#
# evaluate further tags and text
#
todo_tags = todo_tag_regex.findall(text)
if len(todo_tags) > 0:
for todo_tag in todo_tags:
todo.tags[todo_tag[0]] = todo_tag[1]
text = todo_tag_regex.sub('', text).strip()
# evaluate address
#if 'loc' in [_key.lower() for _key in todo.tags.keys()]:
#todo.tags['loc'] = todo.tags['loc'].replace('\\', '\n')
#todo.tags['loc'] = todo.tags['loc'].replace('_', ' ')
# text
todo.text = text
#
# add this TODO to list of todos
#
todos.append(todo)
return todos
#
# output functions
#
def to_dicts(todos):
"""
Convert a list of :class:`todotxtio.Todo` objects to a list of todo dict.
:param list todos: List of :class:`todotxtio.Todo` objects
:rtype: list
"""
return [todo.to_dict() for todo in todos]
def to_stream(stream, todos, close=True):
"""
Write a list of todos to an already-opened stream.
:param file stream: A file-like object
:param list todos: List of :class:`todotxtio.Todo` objects
:param bool close: Whetever to close the stream or not after all operation are finised
:rtype: None
"""
stream.write(to_string(todos))
if close:
stream.close()
def to_file(file_path, todos, encoding='utf-8'):
"""
Write a list of todos to a file.
:param str file_path: Path to the file
:param list todos: List of :class:`todotxtio.Todo` objects
:param str encoding: The encoding of the file to open
:rtype: None
"""
stream = io.open(file_path, 'w', encoding=encoding)
to_stream(stream, todos)
def to_string(todos):
"""
Convert a list of todos to a string.
:param list todos: List of :class:`todotxtio.Todo` objects
:rtype: str
"""
return '\n'.join([serialize(todo) for todo in todos])
#
# main class definition
#
class Todo(object):
"""
Represent one todo.
:param str text: The text of the todo
:param bool completed: Should this todo be marked as completed?
:param str completion_date: A date of completion, in the ``YYYY-MM-DD`` format.
Setting this property will automatically set the
``completed`` attribute to ``True``.
:param str priority: The priority of the todo represented by a char between ``A-Z``
:param str creation_date: A date of creation, in the ``YYYY-MM-DD`` format
:param list projects: A list of projects without leading ``+``
:param list contexts: A list of projects without leading ``@``
:param dict tags: A dict of tags
"""
text = None
completed = False
completion_date = None
priority = None
creation_date = None
projects = []
contexts = []
tags = {}
remarks = []
authors = []
responsibles = []
tobeinformed = []
links = []
def __init__(self,
text=None,
completed=False,
completion_date=None,
priority=None,
creation_date=None,
projects=None,
contexts=None,
tags=None,
remarks=None,
authors=None,
responsibles=None,
tobeinformed=None,
):
self.text = text
self.completed = completed
if completion_date and self.completed:
self.completion_date = completion_date
self.priority = priority
self.creation_date = creation_date
self.projects = projects
self.contexts = contexts
self.tags = tags
self.remarks = remarks
self.authors = authors
self.responsibles = responsibles
self.tobeinformed = tobeinformed
def to_dict(self):
"""
Return a dict representation of this Todo instance.
:rtype: dict
"""
return {
'text': self.text,
'completed': self.completed,
'completion_date': self.completion_date,
'priority': self.priority,
'creation_date': self.creation_date,
'projects': self.projects,
'contexts': self.contexts,
'tags': self.tags,
'remarks': self.remarks,
'authors': self.authors,
'responsibles': self.responsibles,
'tobeinformed': self.tobeinformed,
'links': self.links,
}
def __setattr__(self, name, value):
# BOOL TYPE
if name == 'completed':
if not value:
super(Todo, self).__setattr__('completion_date', None) # Uncompleted todo must not have any completion date
# DATE TYPE
elif name == 'completion_date':
if value:
super(Todo, self).__setattr__('completed', True) # Setting the completion date must set this todo as completed...
else:
super(Todo, self).__setattr__('completed', False) # ...and vice-versa
# STRING TYPE
elif name in ['remarks']:
if not value:
super(Todo, self).__setattr__(name, '') # Force contexts, projects to be lists when setting them to a falsely value
return
#elif type(value) is not str:
#raise ValueError(name + ' should be a string')
# LIST TYPE
elif name in ['projects', 'contexts', 'authors', 'responsibles', 'tobeinformed']:
if not value:
super(Todo, self).__setattr__(name, []) # Force contexts, projects to be lists when setting them to a falsely value
return
elif type(value) is not list: # Make sure, otherwise, that the provided value is a list
raise ValueError(name + ' should be a list')
# TAG TYPE
elif name == 'tags':
if not value:
super(Todo, self).__setattr__(name, {}) # Force tags to be a dict when setting them to a falsely value
return
elif type(value) is not dict: # Make sure, otherwise, that the provided value is a dict
raise ValueError(name + ' should be a dict')
super(Todo, self).__setattr__(name, value)
def __str__(self):
"""
Convert this Todo object in a valid Todo.txt line.
"""
ret = []
if self.completed:
ret.append('x')
if self.completion_date:
ret.append(self.completion_date)
if self.priority:
ret.append('(' + self.priority + ')')
if self.creation_date:
ret.append(self.creation_date)
ret.append(self.text)
if self.projects:
ret.append(''.join([' +' + project for project in self.projects]).strip())
if self.contexts:
ret.append(''.join([' @' + context for context in self.contexts]).strip())
if self.tags:
ret.append(''.join([' ' + tag_name + ':' + tag_value for tag_name, tag_value in self.tags.items()]).strip())
return ' '.join(ret)
def __repr__(self):
"""
Call the __str__ method to return a textual representation of this Todo object.
"""
return self.__str__()
#
# search and format functions
#
def search(todos,
text=None,
completed=None,
completion_date=None,
priority=None,
creation_date=None,
projects=None,
contexts=None,
tags=None,
remarks=None,
authors=None,
responsible=None,
tobeinformed=None,
links=None,
):
"""
Return a list of todos that matches the provided filters.
It takes the exact same parameters as the :class:`todotxtio.Todo`
object constructor, and return a list of :class:`todotxtio.Todo` objects as well.
All criteria defaults to ``None`` which means that the criteria is ignored.
A todo will be returned in the results list if all of the criteria matches. From
the moment when a todo is sent in the results list, it will never be checked again.
:param str text: String to be found in the todo text
:param bool completed: Search for completed/uncompleted todos only
:param str completion_date: Match this completion date
:param list priority: List of priorities to match
:param str creation_date: Match this creation date
:param list projects: List of projects to match
:param list contexts: List of contexts to match
:param dict tags: Dict of tag to match
:rtype: list
"""
results = []
for todo in todos:
text_match \
= completed_match \
= completion_date_match \
= priority_match \
= creation_date_match \
= projects_match \
= contexts_match \
= tags_match \
= True
if text is not None:
text_match = text in todo.text
if completed is not None:
completed_match = todo.completed == completed
if completion_date is not None:
completion_date_match = todo.completion_date == completion_date
if priority is not None:
priority_match = todo.priority in priority
if creation_date is not None:
creation_date_match = todo.creation_date == creation_date
if projects is not None:
projects_match = any(i in projects for i in todo.projects)
if contexts is not None:
contexts_match = any(i in contexts for i in todo.contexts)
if tags is not None:
tags_match = any(todo.tags[k] == v for k, v in tags.items() if k in todo.tags)
if text_match \
and completed_match \
and completion_date_match \
and priority_match \
and creation_date_match \
and projects_match \
and contexts_match \
and tags_match:
results.append(todo)
return results
def serialize(todo):
"""
Convert a Todo object in a serial Todo.txt line.
"""
# in Python v2 there seems to be a problem with __str__ and non-standard
# string characters (as they are encountered e.g. in German languages).
# __str__ seems to return only regular string characters.
ret = []
#
# create prefix
#
if todo.completed:
ret.append('x')
if todo.completion_date:
ret.append(todo.completion_date)
if todo.priority:
ret.append('(' + todo.priority + ')')
if todo.creation_date:
ret.append(todo.creation_date)
#
# append text
#
ret.append(todo.text)
#
# append remarks
#
if todo.remarks:
#ret.append(''.join([' {' + remarks + '}' for remarks in todo.remarks]).strip())
ret.append(' {' + todo.remarks.replace('\n', '\\').strip() + '}')
#
# append links
#
if todo.links:
for _prot, _path in todo.links:
if _prot in [ 'link' ]:
ret.append((' ' + _prot + ':' + _path).strip())
else:
ret.append((' ' + _prot + '://' + _path).strip())
#
# append projects, contexts and tags
#
if todo.projects:
ret.append(''.join([' +' + project for project in todo.projects]).strip())
if todo.contexts:
ret.append(''.join([' @' + context for context in todo.contexts]).strip())
if todo.tags:
ret.append(''.join([' ' + tag_name + ':' + tag_value for tag_name, tag_value in todo.tags.items()]).strip())
#
# append persons
#
if todo.authors:
ret.append(''.join([' [*' + auth + ']' for auth in todo.authors]).strip())
if todo.responsibles:
ret.append(''.join([' [' + resp + ']' for resp in todo.responsibles]).strip())
if todo.tobeinformed:
ret.append(''.join([' [+' + info + ']' for info in todo.tobeinformed]).strip())
return ' '.join(ret)