From 8da343920ba5084c0eae864c63f349d4e33c9561 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 5 Oct 2023 22:13:09 +0200 Subject: ... --- overlays/worktime/worktime/__main__.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'overlays') diff --git a/overlays/worktime/worktime/__main__.py b/overlays/worktime/worktime/__main__.py index 2caf7604..35aef8f7 100755 --- a/overlays/worktime/worktime/__main__.py +++ b/overlays/worktime/worktime/__main__.py @@ -34,12 +34,9 @@ from pathlib import Path from collections import defaultdict -import shelve import jsonpickle from hashlib import blake2s -shelve_d = shelve.open(str(Path(BaseDirectory.save_cache_path('worktime')) / 'entry_durations')) - class TogglAPISection(Enum): TOGGL = '/api/v8' REPORTS = '/reports/api/v2' @@ -99,6 +96,7 @@ class TogglAPI(object): if client_ids is not None and not client_ids: return + cache_dir = Path(BaseDirectory.save_cache_path('worktime')) / 'entry_durations' step = timedelta(days = 120) for req_start in (start_date + step * i for i in count()): req_start = min(req_start, end_date) @@ -110,7 +108,7 @@ class TogglAPI(object): # elif req_start > start_date: # req_start = datetime.combine(req_start.astimezone(timezone.utc).date(), time(tzinfo=timezone.utc)) + timedelta(days = 1) - cache_key = None + cache_path = None if req_end + timedelta(days=60) < datetime.now().astimezone(timezone.utc): cache_key = blake2s(jsonpickle.encode({ 'start': req_start, @@ -120,9 +118,13 @@ class TogglAPI(object): 'workspace': self._workspace_id, 'workspace_clients': self._client_ids }).encode('utf-8'), key = self._api_token.encode('utf-8')).hexdigest() - if cache_key in shelve_d: - yield from shelve_d[cache_key] - continue + cache_path = cache_dir / cache_key[:2] / cache_key[2:4] / f'{cache_key[4:]}.json' + try: + with cache_path.open('r', encoding='utf-8') as ch: + yield from jsonpickle.decode(ch.read()) + continue + except FileNotFoundError: + pass entries = list() params = { 'since': (req_start - timedelta(days=1)).astimezone(timezone.utc).isoformat(), @@ -152,8 +154,10 @@ class TogglAPI(object): if not report['data']: break - if cache_key: - shelve_d[cache_key] = entries + if cache_path: + cache_path.parent.mkdir(parents=True, exist_ok=True) + with cache_path.open('w', encoding='utf-8') as ch: + ch.write(jsonpickle.encode(entries)) # res = timedelta(milliseconds=report['total_billable']) if report['total_billable'] else timedelta(milliseconds=0) # return res -- cgit v1.2.3