2024-06-13 09:40:09 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
2024-06-13 11:59:46 +02:00
|
|
|
from datetime import datetime
|
2024-06-13 09:40:09 +02:00
|
|
|
|
2024-06-19 14:55:39 +02:00
|
|
|
from model.DataStore import DataStore
|
|
|
|
|
from model.TimeSlot import TimeSlot
|
2024-06-13 09:40:09 +02:00
|
|
|
|
2024-06-19 14:55:39 +02:00
|
|
|
from util.views import convert_to_table
|
|
|
|
|
from util.parser import create_parser
|
2024-06-13 11:15:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
parser = create_parser()
|
2024-06-13 10:21:19 +02:00
|
|
|
args = parser.parse_args()
|
2024-06-13 09:40:09 +02:00
|
|
|
|
2024-06-13 10:21:19 +02:00
|
|
|
ds = DataStore("data.json")
|
|
|
|
|
|
|
|
|
|
if args.command == "ls":
|
2024-06-13 10:38:43 +02:00
|
|
|
print(convert_to_table(ds.get_all_time_slots()))
|
2024-06-13 11:15:48 +02:00
|
|
|
|
|
|
|
|
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.")
|
|
|
|
|
else:
|
2024-06-15 14:16:56 +02:00
|
|
|
if args.start is not None:
|
|
|
|
|
s = datetime.strptime(args.start, '%d.%m.%y %H:%M')
|
|
|
|
|
ds.add_time_slot(TimeSlot(args.name, start=s))
|
|
|
|
|
else:
|
|
|
|
|
ds.add_time_slot(TimeSlot(args.name))
|
2024-06-13 11:15:48 +02:00
|
|
|
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.")
|
|
|
|
|
else:
|
2024-06-15 14:16:56 +02:00
|
|
|
if args.end is not None:
|
|
|
|
|
s = datetime.strptime(args.end, '%d.%m.%y %H:%M')
|
|
|
|
|
running[0].end = s
|
|
|
|
|
else:
|
|
|
|
|
running[0].end_now()
|
2024-06-13 11:15:48 +02:00
|
|
|
ds.write_update()
|
|
|
|
|
print(f"Ended event {running[0].name}.")
|
|
|
|
|
|
2024-06-13 11:59:46 +02:00
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
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.strptime(args.start, '%d.%m.%y %H:%M')
|
|
|
|
|
if args.end is not None:
|
|
|
|
|
ts.end = datetime.strptime(args.end, '%d.%m.%y %H:%M')
|
|
|
|
|
ds.add_time_slot(ts)
|
|
|
|
|
ds.write_update()
|
|
|
|
|
print("The event was added.")
|