69 lines
2.3 KiB
Python
Executable File
69 lines
2.3 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
from datetime import datetime
|
|
|
|
from model.DataStore import DataStore
|
|
from model.TimeSlot import TimeSlot
|
|
|
|
from util.views import convert_to_table
|
|
from util.parser import create_parser
|
|
|
|
|
|
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()))
|
|
|
|
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:
|
|
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))
|
|
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:
|
|
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()
|
|
ds.write_update()
|
|
print(f"Ended event {running[0].name}.")
|
|
|
|
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.")
|