-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtw-kanban.py
executable file
·97 lines (69 loc) · 2.89 KB
/
tw-kanban.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
#!/usr/bin/env python3
import subprocess
import json
import jinja2
import datetime
import os
import sys
SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
MAX_PENDING = 1000
MAX_COMPLETED = 10 # max. no. of completed tasks to display
def get_tasks(tags):
command = ['task', 'rc.json.depends.array=no'] + tags + ['export']
data = subprocess.check_output(command)
data = data.decode('utf-8') # decode bytestring to string
data = data.replace('\n','') # remove newline indicators
# load taskwarrior export as json data
tasks = json.loads(data)
return tasks
def check_due_date(tasks):
for task in tasks:
if 'due' in task:
# calculate due date in days
due_date = datetime.datetime.strptime(task['due'], '%Y%m%dT%H%M%SZ')
due_in_days = (due_date - datetime.datetime.utcnow()).days
if due_in_days > 7: # if due after a week, remove due date
task.pop('due', None)
else:
task['due'] = due_in_days
def render_template(tasks_dic):
# jinja stuff to load template
template_loader = jinja2.FileSystemLoader(searchpath=SCRIPT_PATH)
template_env = jinja2.Environment(loader=template_loader)
template_file = 'template.jinja'
template = template_env.get_template(template_file)
# render template and return html
return template.render(tasks_dic)
def write_html(data, filename):
# write data to file
with open(filename, 'w') as f:
f.write(data)
def main(additional_filters=[]):
# empty master dictionary to be filled up and passed to jinja template rendering function
tasks_dic = {}
# get pending tasks
pending_tasks = get_tasks(['status:pending', f'limit:{MAX_PENDING}'] + additional_filters)
# get tasks to do
todo_tasks = [task for task in pending_tasks if 'start' not in task]
# sort tasks by urgency (descending order)
todo_tasks = sorted(todo_tasks, key=lambda task: task['urgency'], reverse=True)
# check due dates
check_due_date(todo_tasks)
# get started tasks
started_tasks = get_tasks(['+ACTIVE'] + additional_filters)
# sort tasks by urgency (descending order)
started_tasks = sorted(started_tasks, key=lambda task: task['urgency'], reverse=True)
# check due dates
check_due_date(started_tasks)
# add pending tasks to master dictionary
tasks_dic['todo_tasks'] = todo_tasks
tasks_dic['started_tasks'] = started_tasks
# get completed tasks and add to master dictionary (same as above)
completed_tasks = get_tasks(['status:completed', f'limit:{MAX_COMPLETED}'] + additional_filters)
tasks_dic['completed_tasks'] = completed_tasks
# pass master dictionary to render template and get html
html = render_template(tasks_dic)
# write html to file
write_html(html, SCRIPT_PATH + '/index.html')
if __name__ == '__main__':
main(sys.argv[1:])