From 5f55aa32fad7b0998c7468f37cd5af305905f6d6 Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Tue, 25 Nov 2025 11:52:10 +0100 Subject: [PATCH] adding definition for timespans for ls and acc command --- main.py | 35 ++++++++++++++++++++++++++++------- model/TimeSlotContainer.py | 11 +++-------- util/parser.py | 4 +++- util/timeslotlist.py | 12 ++++++++++++ 4 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 util/timeslotlist.py diff --git a/main.py b/main.py index 7eccf7d..ea90fd8 100755 --- a/main.py +++ b/main.py @@ -1,31 +1,52 @@ #!/usr/bin/python3 +from datetime import datetime, timedelta -from util.str_to_datetime import str_to_datetime as datetime +from util.str_to_datetime import str_to_datetime from model.TimeSlotContainer import time_slot_container as tsc from model.TimeSlot import TimeSlot from util.views import convert_to_table from util.parser import create_parser +from util import timeslotlist if __name__ == "__main__": parser = create_parser() args = parser.parse_args() + if args.command in ["ls", "acc"]: + match args.span: + case "w": + start_of_week = datetime.now() - timedelta(days=datetime.now().weekday()) + start_of_week_midnight = start_of_week.replace(hour=0, minute=0, second=0, microsecond=0) + slots = tsc.get_time_slots_by_date(start=start_of_week_midnight) + case "lw": + start_of_week = datetime.now() - timedelta(days=datetime.now().weekday()) + start_of_week_midnight = start_of_week.replace(hour=0, minute=0, second=0, microsecond=0) + slots = tsc.get_time_slots_by_date( + start=start_of_week_midnight - timedelta(days=7), + end=start_of_week_midnight, + ) + case "d": + last_midnight = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) + slots = tsc.get_time_slots_by_date(start=last_midnight) + case _: + slots = tsc.get_all_time_slots() + if args.command == "ls": - print(convert_to_table(tsc.get_time_slots_by_date())) + print(convert_to_table(slots)) elif args.command == "start": if args.start is not None: - tsc.open_time_slot(args.name, datetime(args.start)) + tsc.open_time_slot(args.name, str_to_datetime(args.start)) else: tsc.open_time_slot(args.name) print(f"Started event {args.name}.") elif args.command == "end": if args.end is not None: - tsc.close_time_slot(datetime(args.end)) + tsc.close_time_slot(str_to_datetime(args.end)) else: tsc.close_time_slot() print("Ended event.") @@ -34,8 +55,8 @@ if __name__ == "__main__": print(convert_to_table(tsc.get_open_time_slots())) elif args.command == "add": - tsc.add_time_slot(args.name, datetime(args.start), datetime(args.end)) + tsc.add_time_slot(args.name, str_to_datetime(args.start), str_to_datetime(args.end)) elif args.command == "acc": - d = tsc.accumulate_duration(args.query) - print(f"{d.seconds // 3600}:{d.seconds % 3600 // 60}") + d = timeslotlist.accumulate_duration(timeslotlist.filter(slots, args.query)) + print(f"{d.seconds // 3600}:{d.seconds % 3600 // 60:02}") diff --git a/model/TimeSlotContainer.py b/model/TimeSlotContainer.py index 84a99d1..e23a390 100644 --- a/model/TimeSlotContainer.py +++ b/model/TimeSlotContainer.py @@ -1,5 +1,3 @@ -import re - from datetime import date, datetime, timedelta from model.DataStore import DataStore from model.TimeSlot import TimeSlot @@ -16,9 +14,10 @@ class TimeSlotContainer: def get_all_time_slots(self) -> list[TimeSlot]: return self._ds.get_all_time_slots() - def get_time_slots_by_date(self, d: date = datetime.now().date()): + def get_time_slots_by_date(self, start: date, end: date = datetime.now().date()): + # The selection will be oriented at the start date return [ts for ts in self._ds.get_all_time_slots() - if ts.start.date() == datetime.now().date()] + if ts.start >= start and ts.start <= end] def open_time_slot(self, name: str, start_dt: datetime = datetime.now()): if len(self.get_open_time_slots()) > 0: @@ -45,10 +44,6 @@ class TimeSlotContainer: def delete_time_slot(self, ts: TimeSlot): raise NotImplementedError - def accumulate_duration(self, query: str) -> timedelta: - pattern = re.compile(query) - return sum([ts.duration() for ts in self._ds.get_all_time_slots() - if pattern.search(ts.name) ], start=timedelta(0)) try: time_slot_container = TimeSlotContainer(DataStore("data.json")) diff --git a/util/parser.py b/util/parser.py index b44bcc2..98258d8 100644 --- a/util/parser.py +++ b/util/parser.py @@ -7,7 +7,8 @@ def create_parser(): subparsers = parser.add_subparsers(dest="command", help="Sub-command help") # Create the parser for the "ls" command - _ = subparsers.add_parser("ls", help="List all events.") + parser_ls = subparsers.add_parser("ls", help="List events.") + parser_ls.add_argument("-s", "--span", choices=['d', 'w', 'lw'], help="Display only envent in a certain time span (d = current day; w = current week; lw = last week)") # Create the parser for the "ps" command _ = subparsers.add_parser("ps", help="Show the currently running event.") @@ -30,5 +31,6 @@ def create_parser(): # Create the parser for the "acc" command parser_end = subparsers.add_parser("acc", help="Accumulate the duration of events.") parser_end.add_argument("query", help="The regex matching for the name of the event.") + parser_end.add_argument("-s", "--span", choices=['d', 'w', 'lw'], help="Display only envent in a certain time span (d = current day; w = current week; lw = last week)") return parser diff --git a/util/timeslotlist.py b/util/timeslotlist.py new file mode 100644 index 0000000..4561abe --- /dev/null +++ b/util/timeslotlist.py @@ -0,0 +1,12 @@ +import re +from datetime import timedelta + +from model.TimeSlot import TimeSlot + + +def accumulate_duration(timeslots: list[TimeSlot]) -> timedelta: + return sum([ts.duration() for ts in timeslots], start=timedelta(0)) + +def filter(timeslots: list[TimeSlot], query: str) -> list[TimeSlot]: + pattern = re.compile(query) + return [ts for ts in timeslots if pattern.search(ts.name)]