adding definition for timespans for ls and acc command

This commit is contained in:
2025-11-25 11:52:10 +01:00
parent 0e48ce265c
commit 5f55aa32fa
4 changed files with 46 additions and 16 deletions

35
main.py
View File

@@ -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}")

View File

@@ -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"))

View File

@@ -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

12
util/timeslotlist.py Normal file
View File

@@ -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)]