-
Notifications
You must be signed in to change notification settings - Fork 8
/
on-modify.relative-recur
executable file
·73 lines (63 loc) · 2.21 KB
/
on-modify.relative-recur
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
#!/usr/bin/env python
from __future__ import print_function
from builtins import str
import datetime
import json
import sys
import subprocess
import uuid
import os
import tempfile
import time
TIME_FORMAT = '%Y%m%dT%H%M%SZ'
UDA_DUE = 'relativeRecurDue'
UDA_WAIT = 'relativeRecurWait'
env = os.environ.copy()
env['TZ'] = 'UTC0'
# Hand back duration format parsing to task warrior
def calc(statement):
calc = subprocess.Popen(['task', 'rc.verbose=nothing', 'rc.date.iso=yes', 'calc', statement], stdout=subprocess.PIPE, env=env)
out, err = calc.communicate()
# Workaround for TW-1254 (https://bug.tasktools.org/browse/TW-1254)
return out.decode("utf-8").rstrip().replace('-', '').replace(':', '') + 'Z'
# Parse the modified task
original = json.loads(sys.stdin.readline())
modified = sys.stdin.readline()
# Return the unmodified modified task, so it is actually changed
print(modified)
modified = json.loads(modified)
# Has a task with UDA been marked as completed?
if (UDA_DUE in original or UDA_WAIT in original) and original['status']!='completed' and modified['status']=='completed':
del original['modified']
if 'start' in original:
del original['start']
if UDA_DUE in original:
original['due'] = calc(modified['end'] + '+' + original[UDA_DUE])
if UDA_WAIT in original:
original['wait'] = calc(modified['end'] + '+' + original[UDA_WAIT])
original['status'] = 'waiting'
else:
original['status'] = 'pending'
print('Created follow-up task')
original['entry'] = modified['end']
original['uuid'] = str(uuid.uuid4())
# Wait for taskwarrior to finish, so we can safely `task import` the new task
sys.stdout.flush()
task_pid = os.getppid()
if (0 < os.fork()):
sys.exit(0)
else:
# Taskwarrior also waits for stdout to close
try:
os.close(sys.stdout.fileno())
except OSError:
pass # Thrown because of closing stdout. Don't worry, that's fine.
# Wait for taskwarrior to finish
while (os.path.exists("/proc/%s" % str(task_pid))):
time.sleep(0.25)
# Import the follow-up task
with tempfile.NamedTemporaryFile(mode="wt") as new_task:
new_task.write(json.dumps(original));
new_task.flush();
add = subprocess.Popen(['task', 'rc.verbose=nothing', 'import', new_task.name], env=env)
add.communicate();