From 6a596c6527c955348595d61075945b3ca941b1db Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 1 Apr 2020 08:53:34 +0200 Subject: ... --- worktime.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/worktime.py b/worktime.py index 19e2186..639d09c 100755 --- a/worktime.py +++ b/worktime.py @@ -30,21 +30,26 @@ class TogglAPI(object): api_path = api.value section_path = '/'.join(section) - return uricompose(scheme='https', host='www.toggl.com', path=f"{api_path}/{section_path}", query=params) + uri = uricompose(scheme='https', host='www.toggl.com', path=f"{api_path}/{section_path}", query=params) + + return uri def _query(self, url, method): headers = {'content-type': 'application/json'} + response = None if method == 'GET': - return requests.get(url, headers=headers, auth=HTTPBasicAuth(self._api_token, 'api_token')) + response = requests.get(url, headers=headers, auth=HTTPBasicAuth(self._api_token, 'api_token')) elif method == 'POST': - return requests.post(url, headers=headers, auth=HTTPBasicAuth(self._api_token, 'api_token')) + response = requests.post(url, headers=headers, auth=HTTPBasicAuth(self._api_token, 'api_token')) else: raise ValueError(f"Undefined HTTP method “{method}”") - def get_billable_hours(self, start_date, end_date=datetime.now(timezone.utc)): - url = self._make_url(api = TogglAPISection.REPORTS, section = ['summary'], params={'since': start_date.astimezone(timezone.utc).isoformat(), 'until': end_date.astimezone(timezone.utc).isoformat()}) + return response + + def get_billable_hours(self, start_date, end_date=datetime.now(timezone.utc), rounding=False): + url = self._make_url(api = TogglAPISection.REPORTS, section = ['summary'], params={'since': start_date.astimezone(timezone.utc).isoformat(), 'until': end_date.astimezone(timezone.utc).isoformat(), 'rounding': rounding}) r = self._query(url = url, method='GET') return timedelta(milliseconds=r.json()['total_billable']) @@ -79,6 +84,19 @@ class Worktime(object): start_date = start_datetime or datetime.strptime(config['WORKTIME']['StartDate'], date_format).replace(tzinfo=tzlocal()) end_date = end_datetime or self.now + try: + with open(f"{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()) + + if reset_date > start_date and reset_date < end_date: + start_date = reset_date + except IOError as e: + if e.errno != 2: + raise e + + hours_per_week = float(config.get('WORKTIME', 'HoursPerWeek', fallback=40)) workdays = set([int(d.strip()) for d in config.get('WORKTIME', 'Workdays', fallback='1,2,3,4,5').split(',')]) time_per_day = timedelta(hours = hours_per_week) / len(workdays) @@ -160,7 +178,7 @@ class Worktime(object): self.time_pulled_forward += pull_forward[day] - hours_per_day_forward * len(days_forward) self.time_to_work += self.time_pulled_forward - self.time_worked = api.get_billable_hours(start_date, self.now) + self.time_worked = api.get_billable_hours(start_date, self.now, rounding = config.getboolean('WORKTIME', 'rounding', fallback=True)) self.running_entry = api.get_running_clock(self.now) if self.running_entry: -- cgit v1.2.3