diff options
-rwxr-xr-x | worktime.py | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/worktime.py b/worktime.py index 3f577ea..1cc648a 100755 --- a/worktime.py +++ b/worktime.py | |||
@@ -30,21 +30,26 @@ class TogglAPI(object): | |||
30 | 30 | ||
31 | api_path = api.value | 31 | api_path = api.value |
32 | section_path = '/'.join(section) | 32 | section_path = '/'.join(section) |
33 | return uricompose(scheme='https', host='www.toggl.com', path=f"{api_path}/{section_path}", query=params) | 33 | uri = uricompose(scheme='https', host='www.toggl.com', path=f"{api_path}/{section_path}", query=params) |
34 | |||
35 | return uri | ||
34 | 36 | ||
35 | def _query(self, url, method): | 37 | def _query(self, url, method): |
36 | 38 | ||
37 | headers = {'content-type': 'application/json'} | 39 | headers = {'content-type': 'application/json'} |
40 | response = None | ||
38 | 41 | ||
39 | if method == 'GET': | 42 | if method == 'GET': |
40 | return requests.get(url, headers=headers, auth=HTTPBasicAuth(self._api_token, 'api_token')) | 43 | response = requests.get(url, headers=headers, auth=HTTPBasicAuth(self._api_token, 'api_token')) |
41 | elif method == 'POST': | 44 | elif method == 'POST': |
42 | return requests.post(url, headers=headers, auth=HTTPBasicAuth(self._api_token, 'api_token')) | 45 | response = requests.post(url, headers=headers, auth=HTTPBasicAuth(self._api_token, 'api_token')) |
43 | else: | 46 | else: |
44 | raise ValueError(f"Undefined HTTP method “{method}”") | 47 | raise ValueError(f"Undefined HTTP method “{method}”") |
45 | 48 | ||
46 | def get_billable_hours(self, start_date, end_date=datetime.now(timezone.utc)): | 49 | return response |
47 | url = self._make_url(api = TogglAPISection.REPORTS, section = ['summary'], params={'since': start_date.astimezone(timezone.utc).isoformat(), 'until': end_date.astimezone(timezone.utc).isoformat()}) | 50 | |
51 | def get_billable_hours(self, start_date, end_date=datetime.now(timezone.utc), rounding=False): | ||
52 | 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}) | ||
48 | r = self._query(url = url, method='GET') | 53 | r = self._query(url = url, method='GET') |
49 | return timedelta(milliseconds=r.json()['total_billable']) if r.json()['total_billable'] else timedelta(milliseconds=0) | 54 | return timedelta(milliseconds=r.json()['total_billable']) if r.json()['total_billable'] else timedelta(milliseconds=0) |
50 | 55 | ||
@@ -79,6 +84,19 @@ class Worktime(object): | |||
79 | start_date = start_datetime or datetime.strptime(config['WORKTIME']['StartDate'], date_format).replace(tzinfo=tzlocal()) | 84 | start_date = start_datetime or datetime.strptime(config['WORKTIME']['StartDate'], date_format).replace(tzinfo=tzlocal()) |
80 | end_date = end_datetime or self.now | 85 | end_date = end_datetime or self.now |
81 | 86 | ||
87 | try: | ||
88 | with open(f"{config_dir}/reset", 'r') as reset: | ||
89 | for line in reset: | ||
90 | stripped_line = line.strip() | ||
91 | reset_date = datetime.strptime(stripped_line, date_format).replace(tzinfo=tzlocal()) | ||
92 | |||
93 | if reset_date > start_date and reset_date < end_date: | ||
94 | start_date = reset_date | ||
95 | except IOError as e: | ||
96 | if e.errno != 2: | ||
97 | raise e | ||
98 | |||
99 | |||
82 | hours_per_week = float(config.get('WORKTIME', 'HoursPerWeek', fallback=40)) | 100 | hours_per_week = float(config.get('WORKTIME', 'HoursPerWeek', fallback=40)) |
83 | workdays = set([int(d.strip()) for d in config.get('WORKTIME', 'Workdays', fallback='1,2,3,4,5').split(',')]) | 101 | workdays = set([int(d.strip()) for d in config.get('WORKTIME', 'Workdays', fallback='1,2,3,4,5').split(',')]) |
84 | time_per_day = timedelta(hours = hours_per_week) / len(workdays) | 102 | time_per_day = timedelta(hours = hours_per_week) / len(workdays) |
@@ -160,7 +178,7 @@ class Worktime(object): | |||
160 | self.time_pulled_forward += pull_forward[day] - hours_per_day_forward * len(days_forward) | 178 | self.time_pulled_forward += pull_forward[day] - hours_per_day_forward * len(days_forward) |
161 | self.time_to_work += self.time_pulled_forward | 179 | self.time_to_work += self.time_pulled_forward |
162 | 180 | ||
163 | self.time_worked = api.get_billable_hours(start_date, self.now) | 181 | self.time_worked = api.get_billable_hours(start_date, self.now, rounding = config.getboolean('WORKTIME', 'rounding', fallback=True)) |
164 | self.running_entry = api.get_running_clock(self.now) | 182 | self.running_entry = api.get_running_clock(self.now) |
165 | 183 | ||
166 | if self.running_entry: | 184 | if self.running_entry: |