making main less complex

This commit is contained in:
2024-06-19 22:53:43 +02:00
parent bb5364d021
commit a54b19b938
4 changed files with 63 additions and 47 deletions

53
main.py
View File

@@ -2,7 +2,7 @@
from util.str_to_datetime import datetime from util.str_to_datetime import datetime
from model.DataStore import DataStore from model.TimeSlotContainer import time_slot_container as tsc
from model.TimeSlot import TimeSlot from model.TimeSlot import TimeSlot
from util.views import convert_to_table from util.views import convert_to_table
@@ -13,54 +13,25 @@ if __name__ == "__main__":
parser = create_parser() parser = create_parser()
args = parser.parse_args() args = parser.parse_args()
ds = DataStore("data.json")
if args.command == "ls": if args.command == "ls":
print(convert_to_table(ds.get_all_time_slots())) print(convert_to_table(tsc.get_time_slots_by_date()))
elif args.command == "start": elif args.command == "start":
time_slots = ds.get_all_time_slots() if args.start is not None:
running = list(filter(lambda x: x.end is None, time_slots)) tsc.open_time_slot(args.name, datetime(args.start))
if len(running):
print("An event is already running.")
else: else:
if args.start is not None: tsc.open_time_slot(args.name)
ds.add_time_slot(TimeSlot(args.name, start=datetime(args.start))) print(f"Started event {args.name}.")
else:
ds.add_time_slot(TimeSlot(args.name))
print(f"Started event {args.name}.")
elif args.command == "end": elif args.command == "end":
time_slots = ds.get_all_time_slots() if args.end is not None:
running = list(filter(lambda x: x.end is None, time_slots)) tsc.close_time_slot(datetime(args.end))
if len(running) <= 0:
print("No running event.")
elif len(running) > 1:
raise Exception("Found multiple running event.")
else: else:
if args.end is not None: tsc.close_time_slot()
running[0].end = datetime(args.end) print("Ended event.")
else:
running[0].end_now()
ds.write_update()
print(f"Ended event {running[0].name}.")
elif args.command == "ps": elif args.command == "ps":
time_slots = ds.get_all_time_slots() print(convert_to_table(tsc.get_open_time_slots()))
running = list(filter(lambda x: x.end is None, time_slots))
print(convert_to_table(running))
elif args.command == "add": elif args.command == "add":
time_slots = ds.get_all_time_slots() tsc.add_time_slot(args.name, datetime(args.start), datetime(args.end))
running = list(filter(lambda x: x.end is None, time_slots))
if len(running) > 0 and args.end is None:
print("An event is already running.")
else:
ts = TimeSlot(args.name)
if args.start is not None:
ts.start = datetime(args.start)
if args.end is not None:
ts.end = datetime(args.end)
ds.add_time_slot(ts)
ds.write_update()
print("The event was added.")

View File

@@ -1,5 +1,5 @@
from pathlib import Path from pathlib import Path
from .TimeSlot import TimeSlot from model.TimeSlot import TimeSlot
import json import json
@@ -30,11 +30,9 @@ class DataStore:
def add_time_slot(self, time_slot: TimeSlot): def add_time_slot(self, time_slot: TimeSlot):
self._time_slots.append(time_slot) self._time_slots.append(time_slot)
self.write_update()
def remove_time_slot(self, time_slot: TimeSlot): def remove_time_slot(self, time_slot: TimeSlot):
self._time_slots.remove(time_slot) self._time_slots.remove(time_slot)
self.write_update()
def get_all_time_slots(self): def get_all_time_slots(self) -> list[TimeSlot]:
return self._time_slots[:] return self._time_slots[:]

View File

@@ -0,0 +1,47 @@
from datetime import date, datetime
from model.DataStore import DataStore
from model.TimeSlot import TimeSlot
class TimeSlotContainer:
def __init__(self, ds: DataStore):
self._ds = ds
def get_open_time_slots(self) -> list[TimeSlot]:
return [ts for ts in self._ds.get_all_time_slots() if ts.end is None]
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()):
return [ts for ts in self._ds.get_all_time_slots()
if ts.start.date() == datetime.now().date()]
def open_time_slot(self, name: str, start_dt: datetime = datetime.now()):
if len(self.get_open_time_slots()) > 0:
raise Exception("Ein event ist bereits aktiv.")
ts = TimeSlot(name)
ts.start = start_dt
self._ds.add_time_slot(ts)
self._ds.write_update()
def close_time_slot(self, end_dt: datetime = datetime.now()):
ts = self.get_open_time_slots()[0]
self._ds.remove_time_slot(ts)
ts.end = end_dt
self._ds.add_time_slot(ts)
self._ds.write_update()
def add_time_slot(self, name: str, start_dt: datetime, end_dt: datetime):
self.open_time_slot(name, start_dt)
self.close_time_slot(end_dt)
def update_time_slot(self, old: TimeSlot, new: TimeSlot):
raise NotImplementedError
def delete_time_slot(self, ts: TimeSlot):
raise NotImplementedError
time_slot_container = TimeSlotContainer(DataStore("data.json"))

View File

@@ -15,8 +15,8 @@ def create_parser():
# Create the parser for the "add" command # Create the parser for the "add" command
parser_add = subparsers.add_parser("add", help="Adding a new event.") parser_add = subparsers.add_parser("add", help="Adding a new event.")
parser_add.add_argument("name", help="Name of the event") parser_add.add_argument("name", help="Name of the event")
parser_add.add_argument("-s", "--start", help="Start datetime (default now)") parser_add.add_argument("-s", "--start", required=True, help="Start datetime (default now)")
parser_add.add_argument("-e", "--end", help="End datetime") parser_add.add_argument("-e", "--end", required=True, help="End datetime")
# Create the parser for the "start" command # Create the parser for the "start" command
parser_start = subparsers.add_parser("start", help="Starting a new event.") parser_start = subparsers.add_parser("start", help="Starting a new event.")