From a58b96622154b0dc341c77e0f6229e16313e0c6c Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 31 Mar 2021 18:12:12 +0200 Subject: worktime holidays --- overlays/worktime/default.nix | 2 +- overlays/worktime/worktime.py | 68 +++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/overlays/worktime/default.nix b/overlays/worktime/default.nix index 26e1dfed..ab6fb40a 100644 --- a/overlays/worktime/default.nix +++ b/overlays/worktime/default.nix @@ -5,7 +5,7 @@ final: prev: { phases = [ "buildPhase" "installPhase" ]; - python = prev.python37.withPackages (ps: with ps; [pyxdg dateutil uritools requests configparser]); + python = prev.python39.withPackages (ps: with ps; [pyxdg dateutil uritools requests configparser tabulate]); buildPhase = '' substituteAll $src worktime diff --git a/overlays/worktime/worktime.py b/overlays/worktime/worktime.py index 9e514e65..b9af430a 100755 --- a/overlays/worktime/worktime.py +++ b/overlays/worktime/worktime.py @@ -22,6 +22,10 @@ import argparse from copy import deepcopy +import sys + +from tabulate import tabulate + class TogglAPISection(Enum): TOGGL = '/api/v8' REPORTS = '/reports/api/v2' @@ -100,6 +104,36 @@ class Worktime(object): time_to_work = None force_day_to_work = True + @staticmethod + def holidays(year): + holidays = dict() + + y_easter = datetime.combine(easter(year), time(), tzinfo=tzlocal()) + + # Legal holidays in munich, bavaria + holidays[datetime(year, 1, 1, tzinfo=tzlocal()).date()] = 1 + holidays[datetime(year, 1, 6, tzinfo=tzlocal()).date()] = 1 + holidays[(y_easter+timedelta(days=-2)).date()] = 1 + holidays[(y_easter+timedelta(days=+1)).date()] = 1 + holidays[datetime(year, 5, 1, tzinfo=tzlocal()).date()] = 1 + holidays[(y_easter+timedelta(days=+39)).date()] = 1 + holidays[(y_easter+timedelta(days=+50)).date()] = 1 + holidays[(y_easter+timedelta(days=+60)).date()] = 1 + holidays[datetime(year, 8, 15, tzinfo=tzlocal()).date()] = 1 + holidays[datetime(year, 10, 3, tzinfo=tzlocal()).date()] = 1 + holidays[datetime(year, 11, 1, tzinfo=tzlocal()).date()] = 1 + holidays[datetime(year, 12, 25, tzinfo=tzlocal()).date()] = 1 + holidays[datetime(year, 12, 26, tzinfo=tzlocal()).date()] = 1 + + return holidays + + @staticmethod + def config(): + config = configparser.ConfigParser() + config_dir = BaseDirectory.load_first_config('worktime') + config.read(f"{config_dir}/worktime.ini") + return config + def __init__(self, start_datetime=None, end_datetime=None, now=None, include_running=True, force_day_to_work=True, **kwargs): self.include_running = include_running self.force_day_to_work = force_day_to_work @@ -107,9 +141,8 @@ class Worktime(object): if now: self.now = now - config = configparser.ConfigParser() + config = Worktime.config() config_dir = BaseDirectory.load_first_config('worktime') - config.read(f"{config_dir}/worktime.ini") api = TogglAPI(api_token=config['TOGGL']['ApiToken'], workspace_id=config['TOGGL']['Workspace']) date_format = config.get('WORKTIME', 'DateFormat', fallback='%Y-%m-%d') @@ -136,22 +169,7 @@ class Worktime(object): holidays = dict() for year in range(start_date.year, end_date.year + 1): - y_easter = datetime.combine(easter(year), time(), tzinfo=tzlocal()) - - # Legal holidays in munich, bavaria - holidays[datetime(year, 1, 1, tzinfo=tzlocal()).date()] = time_per_day - holidays[datetime(year, 1, 6, tzinfo=tzlocal()).date()] = time_per_day - holidays[(y_easter+timedelta(days=-2)).date()] = time_per_day - holidays[(y_easter+timedelta(days=+1)).date()] = time_per_day - holidays[datetime(year, 5, 1, tzinfo=tzlocal()).date()] = time_per_day - holidays[(y_easter+timedelta(days=+39)).date()] = time_per_day - holidays[(y_easter+timedelta(days=+50)).date()] = time_per_day - holidays[(y_easter+timedelta(days=+60)).date()] = time_per_day - holidays[datetime(year, 8, 15, tzinfo=tzlocal()).date()] = time_per_day - holidays[datetime(year, 10, 3, tzinfo=tzlocal()).date()] = time_per_day - holidays[datetime(year, 11, 1, tzinfo=tzlocal()).date()] = time_per_day - holidays[datetime(year, 12, 25, tzinfo=tzlocal()).date()] = time_per_day - holidays[datetime(year, 12, 26, tzinfo=tzlocal()).date()] = time_per_day + holidays |= {k: v * time_per_day for k, v in Worktime.holidays(year).items()} try: with open(f"{config_dir}/excused", 'r') as excused: @@ -365,7 +383,19 @@ def diff(now, **args): now = Worktime(**dict(args, now = now, include_running = False)) print(now.time_to_work - then.time_to_work) + +def holidays(now, **args): + config = Worktime.config() + date_format = config.get('WORKTIME', 'DateFormat', fallback='%Y-%m-%d') + + table_data = [] + holidays = Worktime.holidays(now.year) + for k, v in holidays.items(): + kstr = k.strftime(date_format) + + table_data += [[kstr, v]] + print(tabulate(table_data, tablefmt="plain")) def main(): parser = argparse.ArgumentParser(prog = "worktime", description = 'Track worktime using toggl API') @@ -379,6 +409,8 @@ def main(): time_worked_parser.set_defaults(cmd = time_worked) diff_parser = subparsers.add_parser('diff') diff_parser.set_defaults(cmd = diff) + holidays_parser = subparsers.add_parser('holidays') + holidays_parser.set_defaults(cmd = holidays) args = parser.parse_args() args.cmd(**vars(args)) -- cgit v1.2.3