add ls, start and end command
This commit is contained in:
54
main.py
54
main.py
@@ -6,15 +6,59 @@ from lib.TimeSlot import TimeSlot
|
||||
from lib.ui import convert_to_table
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description='Time tracker')
|
||||
parser.add_argument('command', type=str, help='Manage time slots.', choices=['start', 'stop', 'add', 'ps', 'ls'])
|
||||
parser.add_argument('-b', '--begin', type=str, help="The start time of a time slot.")
|
||||
parser.add_argument('-e', '--end', type=str, help="The end time of a time slot.")
|
||||
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}.")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user