diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2023-04-04 10:34:46 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2023-04-04 10:34:46 +0200 |
commit | f9476f8b1dda0efdcedd0e5e0615f15d0d9f4cde (patch) | |
tree | 5f25ea51197000313a28d20e16a6eb3eda775d1f /overlays/worktime/worktime/__main__.py | |
parent | dce843e98018baffd6a7d2d157d3362d1395fb0f (diff) | |
download | nixos-f9476f8b1dda0efdcedd0e5e0615f15d0d9f4cde.tar nixos-f9476f8b1dda0efdcedd0e5e0615f15d0d9f4cde.tar.gz nixos-f9476f8b1dda0efdcedd0e5e0615f15d0d9f4cde.tar.bz2 nixos-f9476f8b1dda0efdcedd0e5e0615f15d0d9f4cde.tar.xz nixos-f9476f8b1dda0efdcedd0e5e0615f15d0d9f4cde.zip |
...
Diffstat (limited to 'overlays/worktime/worktime/__main__.py')
-rwxr-xr-x | overlays/worktime/worktime/__main__.py | 44 |
1 files changed, 24 insertions, 20 deletions
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 | |||
3 | from requests.auth import HTTPBasicAuth | 3 | from requests.auth import HTTPBasicAuth |
4 | from datetime import * | 4 | from datetime import * |
5 | from xdg import (BaseDirectory) | 5 | from xdg import (BaseDirectory) |
6 | import configparser | 6 | import toml |
7 | from uritools import uricompose | 7 | from uritools import uricompose |
8 | 8 | ||
9 | from dateutil.easter import * | 9 | from dateutil.easter import * |
@@ -30,6 +30,8 @@ from functools import cache | |||
30 | 30 | ||
31 | import backoff | 31 | import backoff |
32 | 32 | ||
33 | from pathlib import Path | ||
34 | |||
33 | 35 | ||
34 | class TogglAPISection(Enum): | 36 | class TogglAPISection(Enum): |
35 | TOGGL = '/api/v8' | 37 | TOGGL = '/api/v8' |
@@ -201,10 +203,8 @@ class Worktime(object): | |||
201 | 203 | ||
202 | @staticmethod | 204 | @staticmethod |
203 | def config(): | 205 | def config(): |
204 | config = configparser.ConfigParser() | ||
205 | config_dir = BaseDirectory.load_first_config('worktime') | 206 | config_dir = BaseDirectory.load_first_config('worktime') |
206 | config.read(f"{config_dir}/worktime.ini") | 207 | return toml.load(Path(config_dir) / 'worktime.toml') |
207 | return config | ||
208 | 208 | ||
209 | def ordinal_workday(self, date): | 209 | def ordinal_workday(self, date): |
210 | start_date = datetime(date.year, 1, 1, tzinfo=tzlocal()).date() | 210 | start_date = datetime(date.year, 1, 1, tzinfo=tzlocal()).date() |
@@ -222,14 +222,18 @@ class Worktime(object): | |||
222 | 222 | ||
223 | config = Worktime.config() | 223 | config = Worktime.config() |
224 | config_dir = BaseDirectory.load_first_config('worktime') | 224 | config_dir = BaseDirectory.load_first_config('worktime') |
225 | api = TogglAPI(api_token=config['TOGGL']['ApiToken'], workspace_id=config['TOGGL']['Workspace'], client_ids=config.get('TOGGL', 'ClientIds', fallback=None)) | 225 | api = TogglAPI( |
226 | date_format = config.get('WORKTIME', 'DateFormat', fallback='%Y-%m-%d') | 226 | api_token=config.get("TOGGL", {}).get("ApiToken", None), |
227 | 227 | workspace_id=config.get("TOGGL", {}).get("Workspace", None), | |
228 | start_date = start_datetime or datetime.strptime(config['WORKTIME']['StartDate'], date_format).replace(tzinfo=tzlocal()) | 228 | client_ids=config.get("TOGGL", {}).get("ClientIds", None) |
229 | ) | ||
230 | date_format = config.get("WORKTIME", {}).get("DateFormat", '%Y-%m-%d') | ||
231 | |||
232 | start_date = start_datetime or datetime.strptime(config.get("WORKTIME", {}).get("StartDate"), date_format).replace(tzinfo=tzlocal()) | ||
229 | end_date = end_datetime or self.now | 233 | end_date = end_datetime or self.now |
230 | 234 | ||
231 | try: | 235 | try: |
232 | with open(f"{config_dir}/reset", 'r') as reset: | 236 | with open(Path(config_dir) / "reset", 'r') as reset: |
233 | for line in reset: | 237 | for line in reset: |
234 | stripped_line = line.strip() | 238 | stripped_line = line.strip() |
235 | reset_date = datetime.strptime(stripped_line, date_format).replace(tzinfo=tzlocal()) | 239 | reset_date = datetime.strptime(stripped_line, date_format).replace(tzinfo=tzlocal()) |
@@ -241,13 +245,13 @@ class Worktime(object): | |||
241 | raise e | 245 | raise e |
242 | 246 | ||
243 | 247 | ||
244 | hours_per_week = float(config.get('WORKTIME', 'HoursPerWeek', fallback=40)) | 248 | hours_per_week = float(config.get("WORKTIME", {}).get("HoursPerWeek", 40)) |
245 | self.workdays = set([int(d.strip()) for d in config.get('WORKTIME', 'Workdays', fallback='1,2,3,4,5').split(',')]) | 249 | self.workdays = set([int(d.strip()) for d in config.get("WORKTIME", {}).get("Workdays", '1,2,3,4,5').split(',')]) |
246 | self.time_per_day = timedelta(hours = hours_per_week) / len(self.workdays) | 250 | self.time_per_day = timedelta(hours = hours_per_week) / len(self.workdays) |
247 | 251 | ||
248 | holidays = dict() | 252 | holidays = dict() |
249 | 253 | ||
250 | leave_per_year = int(config.get('WORKTIME', 'LeavePerYear', fallback=30)) | 254 | leave_per_year = int(config.get("WORKTIME", {}).get("LeavePerYear", 30)) |
251 | for year in range(start_date.year, end_date.year + 1): | 255 | for year in range(start_date.year, end_date.year + 1): |
252 | holidays |= {k: v * self.time_per_day for k, v in Worktime.holidays(year).items()} | 256 | holidays |= {k: v * self.time_per_day for k, v in Worktime.holidays(year).items()} |
253 | leave_frac = 1 | 257 | leave_frac = 1 |
@@ -256,7 +260,7 @@ class Worktime(object): | |||
256 | self.leave_budget |= {year: floor(leave_per_year * leave_frac)} | 260 | self.leave_budget |= {year: floor(leave_per_year * leave_frac)} |
257 | 261 | ||
258 | try: | 262 | try: |
259 | with open(f"{config_dir}/reset-leave", 'r') as excused: | 263 | with open(Path(config_dir) / "reset-leave", 'r') as excused: |
260 | for line in excused: | 264 | for line in excused: |
261 | stripped_line = line.strip() | 265 | stripped_line = line.strip() |
262 | if stripped_line: | 266 | if stripped_line: |
@@ -273,7 +277,7 @@ class Worktime(object): | |||
273 | 277 | ||
274 | for excused_kind in {'excused', 'leave'}: | 278 | for excused_kind in {'excused', 'leave'}: |
275 | try: | 279 | try: |
276 | with open(f"{config_dir}/{excused_kind}", 'r') as excused: | 280 | with open(Path(config_dir) / excused_kind, 'r') as excused: |
277 | for line in excused: | 281 | for line in excused: |
278 | stripped_line = line.strip() | 282 | stripped_line = line.strip() |
279 | if stripped_line: | 283 | if stripped_line: |
@@ -314,7 +318,7 @@ class Worktime(object): | |||
314 | end_day = end_date.date() | 318 | end_day = end_date.date() |
315 | 319 | ||
316 | try: | 320 | try: |
317 | with open(f"{config_dir}/pull-forward", 'r') as excused: | 321 | with open(Path(config_dir) / "pull-forward", 'r') as excused: |
318 | for line in excused: | 322 | for line in excused: |
319 | stripped_line = line.strip() | 323 | stripped_line = line.strip() |
320 | if stripped_line: | 324 | if stripped_line: |
@@ -357,7 +361,7 @@ class Worktime(object): | |||
357 | extra_days_to_work = dict() | 361 | extra_days_to_work = dict() |
358 | 362 | ||
359 | try: | 363 | try: |
360 | with open(f"{config_dir}/days-to-work", 'r') as extra_days_to_work_file: | 364 | with open(Path(config_dir) / "days-to-work", 'r') as extra_days_to_work_file: |
361 | for line in extra_days_to_work_file: | 365 | for line in extra_days_to_work_file: |
362 | stripped_line = line.strip() | 366 | stripped_line = line.strip() |
363 | if stripped_line: | 367 | if stripped_line: |
@@ -413,7 +417,7 @@ class Worktime(object): | |||
413 | 417 | ||
414 | self.time_to_work += self.time_pulled_forward | 418 | self.time_to_work += self.time_pulled_forward |
415 | 419 | ||
416 | self.time_worked += api.get_billable_hours(start_date, self.now, rounding = config.getboolean('WORKTIME', 'rounding', fallback=True)) | 420 | self.time_worked += api.get_billable_hours(start_date, self.now, rounding = config.get("WORKTIME", {}).get("rounding", True)) |
417 | 421 | ||
418 | def worktime(**args): | 422 | def worktime(**args): |
419 | worktime = Worktime(**args) | 423 | worktime = Worktime(**args) |
@@ -520,7 +524,7 @@ def diff(now, **args): | |||
520 | 524 | ||
521 | def holidays(year, **args): | 525 | def holidays(year, **args): |
522 | config = Worktime.config() | 526 | config = Worktime.config() |
523 | date_format = config.get('WORKTIME', 'DateFormat', fallback='%Y-%m-%d') | 527 | date_format = config.get("WORKTIME", {}).get("DateFormat", '%Y-%m-%d') |
524 | 528 | ||
525 | table_data = [] | 529 | table_data = [] |
526 | 530 | ||
@@ -534,8 +538,8 @@ def leave(year, table, **args): | |||
534 | def_year = datetime.now(tzlocal()).year | 538 | def_year = datetime.now(tzlocal()).year |
535 | worktime = Worktime(**dict(**args, end_datetime = datetime(year = (year if year else def_year) + 1, month = 1, day = 1, tzinfo=tzlocal()) - timedelta(microseconds=1))) | 539 | worktime = Worktime(**dict(**args, end_datetime = datetime(year = (year if year else def_year) + 1, month = 1, day = 1, tzinfo=tzlocal()) - timedelta(microseconds=1))) |
536 | config = Worktime.config() | 540 | config = Worktime.config() |
537 | date_format = config.get('WORKTIME', 'DateFormat', fallback='%Y-%m-%d') | 541 | date_format = config.get("WORKTIME", {}).get("DateFormat", '%Y-%m-%d') |
538 | leave_expires = config.get('WORKTIME', 'LeaveExpires', fallback=None) | 542 | leave_expires = config.get("WORKTIME", {}).get("LeaveExpires", None) |
539 | if leave_expires: | 543 | if leave_expires: |
540 | leave_expires = datetime.strptime(leave_expires, '%m-%d').date() | 544 | leave_expires = datetime.strptime(leave_expires, '%m-%d').date() |
541 | 545 | ||