forked from benjaminrose/TaskPaper-Parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtd.py
More file actions
108 lines (90 loc) · 3.49 KB
/
gtd.py
File metadata and controls
108 lines (90 loc) · 3.49 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
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
from datetime import datetime
from collections import namedtuple
from dateutil import parser
import sys
import re
import argparse
argparser = argparse.ArgumentParser(description='Create Context list from Taskpaper file.')
argparser.add_argument('file', help='The filename for the Taskpaper file')
argparser.add_argument('-c', '--context', help='limit output to a specific context')
args = argparser.parse_args()
tpfile = args.file
with open(tpfile, encoding='utf-8') as f:
tplines = f.readlines()
Flagged = namedtuple('Flagged', ['type', 'taskdate', 'project', 'task'])
flaglist = []
contextlist = set()
errlist = []
done_count = 0
na_count = 0
total_count = 0
project = ''
for line in tplines:
try:
if ':\n' in line:
project = line.strip()[:-1]
elif (('@done' in line) or ('cancelled' in line)):
done_count = done_count + 1
elif ('' == line.strip()) or ('☐' not in line):
continue
else:
na_count = na_count + 1
subject = context = created = ''
found = False
match = re.search(r'☐(?P<subject>[^@]+)@(?P<context>.*)\((?P<date>.*)\)', line)
if match:
found = True
subject = match.group('subject').strip()
context = match.group('context')
created = match.group('date')
if not found:
match = re.search(r'☐(?P<subject>[^@]+)@(?P<context>.*)', line)
if match:
found = True
subject = match.group('subject').strip()
context = match.group('context')
if not found:
match = re.search(r'☐(?P<subject>.*)', line)
if match:
subject = match.group('subject').strip()
if created:
taskdate = datetime.date(parser.parse(created))
else:
taskdate = ''
flaglist.append(
Flagged(context, taskdate, project, subject))
contextlist.add(context)
except Exception as e:
errlist.append((line, e))
total_count = na_count + done_count
today_date = datetime.date(datetime.now())
print('{0} of {1} tasks done, {2} tasks open'.format(done_count, total_count, na_count))
if args.context:
print('\n%s\n' % (args.context.upper()))
found = False
for task in flaglist:
if ((task.type.lower() == args.context.lower()) and (task.project != 'Archive')):
found = True
open_since = ''
if task.taskdate:
open_since = (today_date - task.taskdate).days
print('\t[%s] %s open since %s days' % (task.project, task.task, open_since))
if not found:
print('\t (none)')
else:
for context in sorted(contextlist):
print('\n%s\n' % (context.upper()))
found = False
for task in flaglist:
if ((task.type == context) and (task.project != 'Archive')):
found = True
open_since = ''
if task.taskdate:
open_since = (today_date - task.taskdate).days
print('\t[%s] %s open since %s days' % (task.project, task.task, open_since))
if not found:
print('\t (none)')
if len(errlist) != 0:
print('\nERROR PARSING THESE LINES\n')
for errtask in errlist:
print('\t%s' % str(errtask))