-
Notifications
You must be signed in to change notification settings - Fork 8
/
on-add.context
executable file
·71 lines (49 loc) · 1.73 KB
/
on-add.context
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
#!/usr/bin/env python3
"""
Ensure Taskwarrior's context tags are automatically applied to new tasks.
Conversely, when tasks are added via Reminders, ensure their list is applied as
a project and a tag.
Concrete examples:
If a reminder is created in the "Work" list, then:
a task is created with `project:Work` and `+work`.
If a task is created under the "life" context, then:
a reminder is created in the "Life" list, and `+life` is applied.
If a task is created without a context, then:
a reminder is created in the default list, which is also applied as
project.
"""
import re
import json
import subprocess
import sys
def get_config():
p = subprocess.Popen(["/usr/local/bin/task", "show"], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = [x.decode('utf-8') for x in p.communicate()]
raw_output = stdout.rstrip().split('\n')
config = dict()
config_regex = re.compile(r'^(?P<key>[^\s]+)\s+(?P<value>[^\s].*$)')
for line in raw_output:
match = config_regex.match(line)
if match:
config[match.group('key')] = match.group('value').strip()
return config
def process(task):
config = get_config()
task.setdefault("tags", [])
if 'project' in task:
task['tags'] += [task['project'].lower().replace(" ", "_")]
return
context = config.get("context")
if not context:
return
task['tags'] += [context]
task.setdefault('project', context.title())
def main(stdin, stdout):
task = json.loads(stdin.readline())
msg = process(task)
print(json.dumps(task))
if msg:
print('could not add context tag: '+msg)
if __name__ == '__main__':
main(sys.stdin, sys.stdout)