making main less complex
This commit is contained in:
53
main.py
53
main.py
@@ -2,7 +2,7 @@
|
||||
|
||||
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 util.views import convert_to_table
|
||||
@@ -13,54 +13,25 @@ if __name__ == "__main__":
|
||||
parser = create_parser()
|
||||
args = parser.parse_args()
|
||||
|
||||
ds = DataStore("data.json")
|
||||
|
||||
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":
|
||||
time_slots = ds.get_all_time_slots()
|
||||
running = list(filter(lambda x: x.end is None, time_slots))
|
||||
if len(running):
|
||||
print("An event is already running.")
|
||||
if args.start is not None:
|
||||
tsc.open_time_slot(args.name, datetime(args.start))
|
||||
else:
|
||||
if args.start is not None:
|
||||
ds.add_time_slot(TimeSlot(args.name, start=datetime(args.start)))
|
||||
else:
|
||||
ds.add_time_slot(TimeSlot(args.name))
|
||||
print(f"Started event {args.name}.")
|
||||
tsc.open_time_slot(args.name)
|
||||
print(f"Started event {args.name}.")
|
||||
|
||||
elif args.command == "end":
|
||||
time_slots = ds.get_all_time_slots()
|
||||
running = list(filter(lambda x: x.end is None, time_slots))
|
||||
if len(running) <= 0:
|
||||
print("No running event.")
|
||||
elif len(running) > 1:
|
||||
raise Exception("Found multiple running event.")
|
||||
if args.end is not None:
|
||||
tsc.close_time_slot(datetime(args.end))
|
||||
else:
|
||||
if args.end is not None:
|
||||
running[0].end = datetime(args.end)
|
||||
else:
|
||||
running[0].end_now()
|
||||
ds.write_update()
|
||||
print(f"Ended event {running[0].name}.")
|
||||
tsc.close_time_slot()
|
||||
print("Ended event.")
|
||||
|
||||
elif args.command == "ps":
|
||||
time_slots = ds.get_all_time_slots()
|
||||
running = list(filter(lambda x: x.end is None, time_slots))
|
||||
print(convert_to_table(running))
|
||||
print(convert_to_table(tsc.get_open_time_slots()))
|
||||
|
||||
elif args.command == "add":
|
||||
time_slots = ds.get_all_time_slots()
|
||||
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.")
|
||||
tsc.add_time_slot(args.name, datetime(args.start), datetime(args.end))
|
||||
|
||||
Reference in New Issue
Block a user