-
Notifications
You must be signed in to change notification settings - Fork 0
/
timew2csv.py
executable file
·42 lines (32 loc) · 1.11 KB
/
timew2csv.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
#! /usr/bin/env python3
''' timew2csv.py - convert timewarrior json-reports to csv.
Usage: `timew export :yesterday | ./timew2csv.py`
'''
import sys
import json
from datetime import datetime
from typing import Generator
if sys.version_info.minor < 8:
sys.exit("Error need python version >= python3.8")
TIMEW_DATE_FORMAT = '%Y%m%dT%H%M%SZ'
OUTPUT_FORMAT = '%d-%m-%Y %H:%M:%S'
def decode(data: dict) -> Generator:
for interval in data:
if start := interval.get('start'):
yield datetime.strptime(start, TIMEW_DATE_FORMAT)
if end := interval.get('end'):
yield datetime.strptime(end, TIMEW_DATE_FORMAT)
if tags := interval.get('tags'):
yield " ".join(tags)
if __name__ == "__main__":
data = json.load(sys.stdin)
data_gen = decode(data)
while True:
try:
start_t = next(data_gen)
end_t = next(data_gen)
tag_str = next(data_gen)
print("{},{},{}".format(start_t.strftime(OUTPUT_FORMAT),
end_t.strftime(OUTPUT_FORMAT), tag_str))
except StopIteration:
break