#!/usr/bin/python3 import argparse from lib.DataStore import DataStore from lib.TimeSlot import TimeSlot from lib.ui import convert_to_table 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 parser_ls = subparsers.add_parser("ls", help="List all events.") # Create the parser for the "ps" command parser_ps = subparsers.add_parser("ps", help="Show the currently running event.") # Create the parser for the "add" command parser_add = subparsers.add_parser("add", help="Adding a new event.") parser_add.add_argument("-b", "--begin", help="Begin datetime") 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 parser_end = subparsers.add_parser("end", help="Ending the current event.") return 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: 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}.")