argos-timewarrior/timewarrior.py

56 lines
2.1 KiB
Python
Raw Normal View History

2020-09-21 08:17:04 +02:00
#! /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>
2021-04-28 10:31:21 +02:00
# <bitbar.abouturl>https://gitlab.com/schtobia/argos-timewarrior/blob/master/timewarrior.py</bitbar.abouturl>
2020-09-21 08:17:04 +02:00
import functools
import locale
from datetime import datetime, timedelta
from timew import TimeWarrior # type: ignore
2020-09-21 08:17:04 +02:00
2021-04-28 10:31:21 +02:00
__author__ = "Tobias Schmidl"
__copyright__ = "Copyright 2021, Tobias Schmidl"
__license__ = "MIT"
__version__ = "1.0"
__maintainer__ = "Tobias Schmidl"
2020-09-21 08:17:04 +02:00
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':
2021-12-13 07:29:27 +01:00
x['tags'] if 'tags' in x else None
2020-09-21 08:17:04 +02:00
}, TW.summary()),
key=lambda x: x['start'])
2021-04-27 15:42:25 +02:00
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 "")
2021-04-27 15:42:25 +02:00
print("---")
2020-09-21 08:17:04 +02:00
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")