This repository has been archived by the owner on Jun 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtasktime.py
executable file
·219 lines (168 loc) · 5.95 KB
/
tasktime.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
#!/usr/bin/env python3
# Copyright (c) 2012 Sven Hertle <[email protected]>
import sys
import subprocess
import json
import re
import datetime
#
# Calculations
#
class Calculator:
printer = None
task_cmd = "task"
print_null = False
def __init__(self):
self.printer = ReadablePrinter()
def setPrinter(self, printer):
self.printer = printer
def setTaskCmd(self, task_cmd):
self.task_cmd = task_cmd
def setPrintNull(self, print_null):
self.print_null = print_null
def create_statistic(self, project):
if self.printer == None:
print("Printer is None")
sys.exit(1)
# Get data from taskwarrior
try:
json_tmp = subprocess.check_output([self.task_cmd,
"export",
"pro:" + project,
"rc.json.array=on",
])
except OSError as e:
print(str(e))
sys.exit(1)
except subprocess.CalledProcessError as e:
print("Export from taskwarrior fails: " + str(e.output, encoding="utf8"))
sys.exit(1)
# Make valid JSON
json_str=str(json_tmp, encoding="utf8")
# Parse JSON
tasks = json.loads(json_str)
# Print data
self.printer.print_header(project)
time = self.handle_tasks(tasks)
self.printer.print_result(time)
def handle_tasks(self, tasks):
seconds = 0
for t in tasks:
tmp_seconds = self.get_task_time(t)
seconds += tmp_seconds
if self.print_null or tmp_seconds != 0:
self.printer.print_task(t["description"], tmp_seconds)
return seconds
def get_task_time(self, task):
seconds = 0
last_start = ""
if "annotations" in task:
annotations = task["annotations"]
for a in annotations:
if a["description"] == "Started task":
last_start = a["entry"]
elif a["description"] == "Stopped task":
seconds += self.calc_time_delta(last_start, a["entry"])
return seconds
def calc_time_delta(self, start, stop):
start_time = self.internal_to_datetime(start)
stop_time = self.internal_to_datetime(stop)
delta = stop_time - start_time
return delta.total_seconds()
def internal_to_datetime(self, string):
match = re.search("^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z$", string)
if match == None:
return None
year = int(match.group(1))
month = int(match.group(2))
day = int(match.group(3))
hour = int(match.group(4))
minute = int(match.group(5))
second = int(match.group(6))
return datetime.datetime(year, month, day, hour, minute, second)
#
# Printer
#
class Printer:
def print_header(self, project):
raise NotImplementedError()
def print_task(self, description, seconds):
raise NotImplementedError()
def print_result(self, seconds):
raise NotImplementedError()
def seconds_to_readable(self, seconds):
second = seconds % 60
minute = (seconds // 60) % 60
hour = (seconds // 3600)
return self._number_to_2_digits(hour) + ":" + self._number_to_2_digits(minute) + ":" + self._number_to_2_digits(second)
def _number_to_2_digits(self, n):
return repr(round(n)).zfill(2)
# CSV
class CSVPrinter(Printer):
def _csv_encode(self, string):
return string.replace("\"", "\"\"")
def print_header(self, project):
print("\"Project\",\"" + self._csv_encode(project) + "\"")
print("\"\",\"\"")
print("\"Description\",\"Duration (hours)\"")
print("\"\",\"\"")
def print_task(self, description, seconds):
print("\"" + self._csv_encode(description) + "\",\"" + self.seconds_to_readable(seconds) + "\"")
def print_result(self, seconds):
print("\"\",\"\"")
print("\"Sum\",\"" + self.seconds_to_readable(seconds) + "\"")
# Readable
class ReadablePrinter(Printer):
def print_header(self, project):
print("Project: " + project)
print()
def print_task(self, description, seconds):
print(description)
if seconds != 0:
print("\tDuration: " + self.seconds_to_readable(seconds))
def print_result(self, seconds):
print()
print("Sum: " + self.seconds_to_readable(seconds))
# Help
def print_help():
print(sys.argv[0] + " [parameters...] <project>")
print()
print("Calculate and print spent time for a project from taskwarrior")
print()
print("Parameters:")
print("\t-h, --help\t\tShow this help")
print("\t-c, --csv\t\tPrint output in CSV format")
print("\t-n, --null\t\tPrint also tasks without time information (default: no)")
print("\t-t, --task [cmd]\tChange task command")
#
# Main
#
if __name__ == "__main__":
params = sys.argv[1:]
c = Calculator()
project = None
show_help = False
skip = False
for i,param in enumerate(params):
if skip:
skip = False
continue
if param == "--csv" or param == "-c":
c.setPrinter(CSVPrinter())
elif param == "--help" or param == "-h":
show_help = True
elif param == "--task" or param == "-t":
if i == len(params)-1: # Last parameter -> error
print("--task needs another parameter")
sys.exit(1)
else:
c.setTaskCmd(params[i+1])
skip = True
elif param == "--null" or param == "-n":
c.setPrintNull(True)
elif i == len(params)-1: # Last parameter
project = param
if show_help or project == None:
print_help()
else:
c.create_statistic(project)