argos-timewarrior/timewarrior.py
2021-12-13 07:29:27 +01:00

56 lines
2.1 KiB
Python
Executable file

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""Timewarrior adapter for `argos <https://github.com/p-e-w/argos>`."""
# <bitbar.title>timewarrior</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Tobias Schmidl</bitbar.author>
# <bitbar.author.github>schtobia</bitbar.author.github>
# <bitbar.desc>Greps and formats data from timewarrior.</bitbar.desc>
# <bitbar.image></bitbar.image>
# <bitbar.dependencies>python,timew</bitbar.dependencies>
# <bitbar.abouturl>https://gitlab.com/schtobia/argos-timewarrior/blob/master/timewarrior.py</bitbar.abouturl>
import functools
import locale
from datetime import datetime, timedelta
from timew import TimeWarrior # type: ignore
__author__ = "Tobias Schmidl"
__copyright__ = "Copyright 2021, Tobias Schmidl"
__license__ = "MIT"
__version__ = "1.0"
__maintainer__ = "Tobias Schmidl"
TW = TimeWarrior()
TIMEFORMATTER = '%Y%m%dT%H%M%S%z'
locale.setlocale(locale.LC_ALL, '')
RESULTS = sorted(map(
lambda x: {
'start':
datetime.strptime(x['start'], TIMEFORMATTER).astimezone(tz=None),
'end':
datetime.strptime(x['end'], TIMEFORMATTER).astimezone(tz=None)
if 'end' in x else datetime.now().astimezone(tz=None),
'id':
x['id'],
'tags':
x['tags'] if 'tags' in x else None
}, TW.summary()),
key=lambda x: x['start'])
IS_ACTIVE = len(list(filter(lambda x: "end" not in x, TW.summary()))) > 0
TOTAL_TIME = functools.reduce(lambda total, x: total + x['end'] - x['start'], RESULTS, timedelta())
TOTAL_TIME_HOURS, TOTAL_TIME_REMAINDER = divmod(int(TOTAL_TIME.total_seconds()), 3600)
TOTAL_TIME_MINUTES, _ = divmod(TOTAL_TIME_REMAINDER, 60)
print("" if IS_ACTIVE else "", f"{TOTAL_TIME_HOURS:02}:{TOTAL_TIME_MINUTES:02}", "" if not IS_ACTIVE else "")
print("---")
print(
" | font=monospace | size=8\n".join(
map(
lambda x: str({
'start': datetime.strftime(x['start'], "%x %X"),
'end': datetime.strftime(x['end'], "%x %X"),
'tags': x['tags']
}), RESULTS)), "| font=monospace | size=8")