Files
tt/main.py

86 lines
3.0 KiB
Python
Raw Normal View History

2024-06-13 09:40:09 +02:00
#!/usr/bin/python3
import argparse
2024-06-13 10:21:19 +02:00
from lib.DataStore import DataStore
from lib.TimeSlot import TimeSlot
2024-06-13 10:38:43 +02:00
from lib.ui import convert_to_table
from datetime import datetime
2024-06-13 09:40:09 +02:00
2024-06-13 11:15:48 +02:00
def create_parser():
# Create the top-level parser
parser = argparse.ArgumentParser(description="Time tracker.")
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.")
2024-06-13 11:15:48 +02:00
# Create the parser for the "ps" command
_ = subparsers.add_parser("ps", help="Show the currently running event.")
2024-06-13 11:15:48 +02:00
# Create the parser for the "add" command
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("-s", "--start", help="Start datetime (default now)")
2024-06-13 11:15:48 +02:00
parser_add.add_argument("-e", "--end", help="End datetime")
# Create the parser for the "start" command
parser_start = subparsers.add_parser("start", help="Starting a new event.")
parser_start.add_argument("name", help="Name of the event")
# Create the parser for the "end" command
_ = subparsers.add_parser("end", help="Ending the current event.")
2024-06-13 09:40:09 +02:00
2024-06-13 11:15:48 +02:00
return parser
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:
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:
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.")