Add timewarrior config
This commit is contained in:
parent
940e8f2fea
commit
4518f0efb0
2 changed files with 127 additions and 0 deletions
|
|
@ -0,0 +1,87 @@
|
|||
#!/usr/bin/env python3
|
||||
from collections import defaultdict
|
||||
from collections.abc import Iterable
|
||||
from datetime import datetime, timedelta, timezone, time
|
||||
|
||||
import json
|
||||
|
||||
import sys
|
||||
|
||||
DATE_FORMAT: str = "%Y%m%dT%H%M%SZ"
|
||||
DATE_OUT_FORMAT: str = "%H:%M"
|
||||
|
||||
|
||||
def fmt_decimal_hours(duration: timedelta) -> float:
|
||||
return duration.total_seconds() / 3600
|
||||
|
||||
|
||||
def fmt_row(record: dict) -> str:
|
||||
return "(%3.2f) %5s-%5s %s: %s" % (
|
||||
fmt_decimal_hours(record["duration"]),
|
||||
record["start"].strftime(DATE_OUT_FORMAT),
|
||||
record["end"].strftime(DATE_OUT_FORMAT),
|
||||
record["project"],
|
||||
", ".join(record["tags"]),
|
||||
)
|
||||
|
||||
|
||||
def print_details(records: Iterable[dict]) -> None:
|
||||
print("{:-^80}".format(" Details "))
|
||||
print()
|
||||
for record in records:
|
||||
print(fmt_row(record))
|
||||
|
||||
|
||||
def print_projects(projects) -> None:
|
||||
print("{:-^80}".format(" Projects "))
|
||||
print()
|
||||
|
||||
longest = 0
|
||||
|
||||
for name in projects.keys():
|
||||
longest = max(len(name), longest)
|
||||
|
||||
for name, project in projects.items():
|
||||
print(
|
||||
f"(%3.2f) %{longest + 2}s: %s"
|
||||
% (fmt_decimal_hours(project["duration"]), name, ", ".join(project["tags"]))
|
||||
)
|
||||
|
||||
|
||||
for line in sys.stdin:
|
||||
if line.strip() == "":
|
||||
break
|
||||
|
||||
input = json.load(sys.stdin)
|
||||
projects = defaultdict(dict)
|
||||
|
||||
for record in input:
|
||||
record["start"] = datetime.strptime(record["start"], DATE_FORMAT)
|
||||
|
||||
if "end" in record:
|
||||
record["end"] = datetime.strptime(record["end"], DATE_FORMAT)
|
||||
else:
|
||||
record["end"] = datetime.now()
|
||||
|
||||
record["duration"] = record["end"] - record["start"]
|
||||
record["project"] = ""
|
||||
|
||||
for i, tag in enumerate(record["tags"]):
|
||||
if len(tag) > 0 and tag[0].isupper():
|
||||
record["project"] = tag
|
||||
del record["tags"][i]
|
||||
break
|
||||
|
||||
if record["project"] not in projects:
|
||||
projects[record["project"]] = {
|
||||
"duration": record["duration"],
|
||||
"tags": record["tags"],
|
||||
}
|
||||
else:
|
||||
projects[record["project"]]["duration"] += record["duration"]
|
||||
projects[record["project"]]["tags"] += record["tags"]
|
||||
|
||||
|
||||
print_projects(projects)
|
||||
print()
|
||||
print_details(input)
|
||||
Loading…
Add table
Add a link
Reference in a new issue