From 20168a40bc72096b2abd2b5fceae4a5f8a318cc9 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 29 Jan 2025 20:33:15 +0100 Subject: ... --- overlays/worktime/worktime/__main__.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/overlays/worktime/worktime/__main__.py b/overlays/worktime/worktime/__main__.py index 9335afdc..ba6c5ff6 100755 --- a/overlays/worktime/worktime/__main__.py +++ b/overlays/worktime/worktime/__main__.py @@ -224,6 +224,7 @@ class Worktime(object): leave_budget = dict() time_per_day = None workdays = None + pull_forward = dict() @staticmethod @cache @@ -391,8 +392,6 @@ class Worktime(object): if e.errno != 2: raise e - pull_forward = dict() - start_day = self.start_date.date() end_day = self.end_date.date() @@ -419,15 +418,15 @@ class Worktime(object): if not d == datetime.strptime(c, date_format).replace(tzinfo=tzlocal()).date(): break else: if d >= self.end_date.date(): - pull_forward[d] = min(timedelta(hours = float(hours)), self.time_per_day(d) - (holidays[d] if d in holidays else timedelta())) + self.pull_forward[d] = min(timedelta(hours = float(hours)), self.time_per_day(d) - (holidays[d] if d in holidays else timedelta())) except IOError as e: if e.errno != 2: raise e self.days_to_work = dict() - if pull_forward: - end_day = max(end_day, max(list(pull_forward))) + if self.pull_forward: + end_day = max(end_day, max(list(self.pull_forward))) for day in [start_day + timedelta(days = x) for x in range(0, (end_day - start_day).days + 1)]: if day.isoweekday() in self.workdays: @@ -471,17 +470,17 @@ class Worktime(object): self.extra_days_to_work[self.now.date()] = timedelta() self.time_to_work = sum([self.days_to_work[day] for day in self.days_to_work.keys() if day <= self.end_date.date()], timedelta()) - for day in [d for d in list(pull_forward) if d > self.end_date.date()]: - days_forward = set([d for d in self.days_to_work.keys() if d >= self.end_date.date() and d < day and (not d in pull_forward or d == self.end_date.date())]) - extra_days_forward = set([d for d in self.extra_days_to_work.keys() if d >= self.end_date.date() and d < day and (not d in pull_forward or d == self.end_date.date())]) + for day in [d for d in list(self.pull_forward) if d > self.end_date.date()]: + days_forward = set([d for d in self.days_to_work.keys() if d >= self.end_date.date() and d < day and (not d in self.pull_forward or d == self.end_date.date())]) + extra_days_forward = set([d for d in self.extra_days_to_work.keys() if d >= self.end_date.date() and d < day and (not d in self.pull_forward or d == self.end_date.date())]) days_forward = days_forward.union(extra_days_forward) extra_day_time_left = timedelta() for extra_day in extra_days_forward: day_time = max(timedelta(), self.time_per_day(extra_day) - self.extra_days_to_work[extra_day]) extra_day_time_left += day_time - extra_day_time = min(extra_day_time_left, pull_forward[day]) - time_forward = pull_forward[day] - extra_day_time + extra_day_time = min(extra_day_time_left, self.pull_forward[day]) + time_forward = self.pull_forward[day] - extra_day_time if extra_day_time_left > timedelta(): for extra_day in extra_days_forward: day_time = max(timedelta(), self.time_per_day(extra_day) - self.extra_days_to_work[extra_day]) @@ -571,7 +570,10 @@ def worktime(pull_forward_cutoff, waybar, **args): return f"({difference_string})" out_class = "running" if worktime.running_entry else "stopped" - tooltip = tooltip_timedelta(worktime.time_to_work - worktime.time_worked) + difference = worktime.time_to_work - worktime.time_worked + if worktime.running_entry and -min(timedelta(milliseconds=0), difference) > sum(worktime.pull_forward.values(), start=timedelta(milliseconds=0)) or not worktime.running_entry and max(timedelta(milliseconds=0), difference) > worktime.time_per_day(worktime.now.date()) and worktime.now_is_workday: + out_class = "over" + tooltip = tooltip_timedelta(difference) if worktime.time_pulled_forward >= min(pull_forward_cutoff, timedelta(seconds = 1)): worktime_no_pulled_forward = deepcopy(worktime) worktime_no_pulled_forward.time_to_work -= worktime_no_pulled_forward.time_pulled_forward @@ -593,6 +595,10 @@ def worktime(pull_forward_cutoff, waybar, **args): else: print(out_text) +def pull_forward(**args): + worktime = Worktime(**args) + print(tooltip_timedelta(sum(worktime.pull_forward.values(), start=timedelta(milliseconds=0)))) + def time_worked(now, waybar, **args): then = now.replace(hour = 0, minute = 0, second = 0, microsecond = 0) if now.time() == time(): @@ -895,6 +901,8 @@ def main(): classification_parser.add_argument('--table', action = 'store_true') classification_parser.add_argument('--table-format', dest='table_format', type=str, default='fancy_grid') classification_parser.set_defaults(cmd = partial(classification, classification_name=classification_name)) + pull_forward_parser = subparsers.add_parser('pull-forward') + pull_forward_parser.set_defaults(cmd = pull_forward) parser.set_default_subparser('time_worked') args = parser.parse_args() -- cgit v1.2.3