From f9476f8b1dda0efdcedd0e5e0615f15d0d9f4cde Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 4 Apr 2023 10:34:46 +0200 Subject: ... --- overlays/worktime/poetry.lock | 30 ++++++++++------------- overlays/worktime/pyproject.toml | 2 +- overlays/worktime/worktime/__main__.py | 44 ++++++++++++++++++---------------- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/overlays/worktime/poetry.lock b/overlays/worktime/poetry.lock index 7a9a9b86..eab1d070 100644 --- a/overlays/worktime/poetry.lock +++ b/overlays/worktime/poetry.lock @@ -109,22 +109,6 @@ files = [ {file = "charset_normalizer-3.1.0-py3-none-any.whl", hash = "sha256:3d9098b479e78c85080c98e1e35ff40b4a31d8953102bb0fd7d1b6f8a2111a3d"}, ] -[[package]] -name = "configparser" -version = "5.3.0" -description = "Updated configparser from stdlib for earlier Pythons." -category = "main" -optional = false -python-versions = ">=3.7" -files = [ - {file = "configparser-5.3.0-py3-none-any.whl", hash = "sha256:b065779fd93c6bf4cee42202fa4351b4bb842e96a3fb469440e484517a49b9fa"}, - {file = "configparser-5.3.0.tar.gz", hash = "sha256:8be267824b541c09b08db124917f48ab525a6c3e837011f3130781a224c57090"}, -] - -[package.extras] -docs = ["jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx"] -testing = ["flake8 (<5)", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=1.3)", "pytest-flake8", "pytest-mypy (>=0.9.1)", "types-backports"] - [[package]] name = "idna" version = "3.4" @@ -213,6 +197,18 @@ files = [ [package.extras] widechars = ["wcwidth"] +[[package]] +name = "toml" +version = "0.10.2" +description = "Python Library for Tom's Obvious, Minimal Language" +category = "main" +optional = false +python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*" +files = [ + {file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"}, + {file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"}, +] + [[package]] name = "uritools" version = "4.0.1" @@ -245,4 +241,4 @@ socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "30e7918385e12f686b92da70a0be69cd22879892239b65e399828c24d13ca262" +content-hash = "0d556c1b7f4ca6764a006e10ef9949359911925a9dae09d25a3c3d26d8966790" diff --git a/overlays/worktime/pyproject.toml b/overlays/worktime/pyproject.toml index f3fd3dfa..61257422 100644 --- a/overlays/worktime/pyproject.toml +++ b/overlays/worktime/pyproject.toml @@ -10,9 +10,9 @@ pyxdg = "^0.28" python-dateutil = "^2.8.2" uritools = "^4.0.1" requests = "^2.28.2" -configparser = "^5.3.0" tabulate = "^0.9.0" backoff = "^2.2.1" +toml = "^0.10.2" [tool.poetry.scripts] worktime = "worktime.__main__:main" diff --git a/overlays/worktime/worktime/__main__.py b/overlays/worktime/worktime/__main__.py index ed7880db..2dc9ed72 100755 --- a/overlays/worktime/worktime/__main__.py +++ b/overlays/worktime/worktime/__main__.py @@ -3,7 +3,7 @@ from requests.exceptions import HTTPError from requests.auth import HTTPBasicAuth from datetime import * from xdg import (BaseDirectory) -import configparser +import toml from uritools import uricompose from dateutil.easter import * @@ -30,6 +30,8 @@ from functools import cache import backoff +from pathlib import Path + class TogglAPISection(Enum): TOGGL = '/api/v8' @@ -201,10 +203,8 @@ class Worktime(object): @staticmethod def config(): - config = configparser.ConfigParser() config_dir = BaseDirectory.load_first_config('worktime') - config.read(f"{config_dir}/worktime.ini") - return config + return toml.load(Path(config_dir) / 'worktime.toml') def ordinal_workday(self, date): start_date = datetime(date.year, 1, 1, tzinfo=tzlocal()).date() @@ -222,14 +222,18 @@ class Worktime(object): config = Worktime.config() config_dir = BaseDirectory.load_first_config('worktime') - api = TogglAPI(api_token=config['TOGGL']['ApiToken'], workspace_id=config['TOGGL']['Workspace'], client_ids=config.get('TOGGL', 'ClientIds', fallback=None)) - date_format = config.get('WORKTIME', 'DateFormat', fallback='%Y-%m-%d') - - start_date = start_datetime or datetime.strptime(config['WORKTIME']['StartDate'], date_format).replace(tzinfo=tzlocal()) + api = TogglAPI( + api_token=config.get("TOGGL", {}).get("ApiToken", None), + workspace_id=config.get("TOGGL", {}).get("Workspace", None), + client_ids=config.get("TOGGL", {}).get("ClientIds", None) + ) + date_format = config.get("WORKTIME", {}).get("DateFormat", '%Y-%m-%d') + + start_date = start_datetime or datetime.strptime(config.get("WORKTIME", {}).get("StartDate"), date_format).replace(tzinfo=tzlocal()) end_date = end_datetime or self.now try: - with open(f"{config_dir}/reset", 'r') as reset: + with open(Path(config_dir) / "reset", 'r') as reset: for line in reset: stripped_line = line.strip() reset_date = datetime.strptime(stripped_line, date_format).replace(tzinfo=tzlocal()) @@ -241,13 +245,13 @@ class Worktime(object): raise e - hours_per_week = float(config.get('WORKTIME', 'HoursPerWeek', fallback=40)) - self.workdays = set([int(d.strip()) for d in config.get('WORKTIME', 'Workdays', fallback='1,2,3,4,5').split(',')]) + hours_per_week = float(config.get("WORKTIME", {}).get("HoursPerWeek", 40)) + self.workdays = set([int(d.strip()) for d in config.get("WORKTIME", {}).get("Workdays", '1,2,3,4,5').split(',')]) self.time_per_day = timedelta(hours = hours_per_week) / len(self.workdays) holidays = dict() - leave_per_year = int(config.get('WORKTIME', 'LeavePerYear', fallback=30)) + leave_per_year = int(config.get("WORKTIME", {}).get("LeavePerYear", 30)) for year in range(start_date.year, end_date.year + 1): holidays |= {k: v * self.time_per_day for k, v in Worktime.holidays(year).items()} leave_frac = 1 @@ -256,7 +260,7 @@ class Worktime(object): self.leave_budget |= {year: floor(leave_per_year * leave_frac)} try: - with open(f"{config_dir}/reset-leave", 'r') as excused: + with open(Path(config_dir) / "reset-leave", 'r') as excused: for line in excused: stripped_line = line.strip() if stripped_line: @@ -273,7 +277,7 @@ class Worktime(object): for excused_kind in {'excused', 'leave'}: try: - with open(f"{config_dir}/{excused_kind}", 'r') as excused: + with open(Path(config_dir) / excused_kind, 'r') as excused: for line in excused: stripped_line = line.strip() if stripped_line: @@ -314,7 +318,7 @@ class Worktime(object): end_day = end_date.date() try: - with open(f"{config_dir}/pull-forward", 'r') as excused: + with open(Path(config_dir) / "pull-forward", 'r') as excused: for line in excused: stripped_line = line.strip() if stripped_line: @@ -357,7 +361,7 @@ class Worktime(object): extra_days_to_work = dict() try: - with open(f"{config_dir}/days-to-work", 'r') as extra_days_to_work_file: + with open(Path(config_dir) / "days-to-work", 'r') as extra_days_to_work_file: for line in extra_days_to_work_file: stripped_line = line.strip() if stripped_line: @@ -413,7 +417,7 @@ class Worktime(object): self.time_to_work += self.time_pulled_forward - self.time_worked += api.get_billable_hours(start_date, self.now, rounding = config.getboolean('WORKTIME', 'rounding', fallback=True)) + self.time_worked += api.get_billable_hours(start_date, self.now, rounding = config.get("WORKTIME", {}).get("rounding", True)) def worktime(**args): worktime = Worktime(**args) @@ -520,7 +524,7 @@ def diff(now, **args): def holidays(year, **args): config = Worktime.config() - date_format = config.get('WORKTIME', 'DateFormat', fallback='%Y-%m-%d') + date_format = config.get("WORKTIME", {}).get("DateFormat", '%Y-%m-%d') table_data = [] @@ -534,8 +538,8 @@ def leave(year, table, **args): def_year = datetime.now(tzlocal()).year worktime = Worktime(**dict(**args, end_datetime = datetime(year = (year if year else def_year) + 1, month = 1, day = 1, tzinfo=tzlocal()) - timedelta(microseconds=1))) config = Worktime.config() - date_format = config.get('WORKTIME', 'DateFormat', fallback='%Y-%m-%d') - leave_expires = config.get('WORKTIME', 'LeaveExpires', fallback=None) + date_format = config.get("WORKTIME", {}).get("DateFormat", '%Y-%m-%d') + leave_expires = config.get("WORKTIME", {}).get("LeaveExpires", None) if leave_expires: leave_expires = datetime.strptime(leave_expires, '%m-%d').date() -- cgit v1.2.3