diff --git a/lib/DataStore.py b/lib/DataStore.py index ee69298..23ac5f9 100644 --- a/lib/DataStore.py +++ b/lib/DataStore.py @@ -9,7 +9,7 @@ class DataStore: if create: # Store emtpty list self._time_slots = [] - self._write_update() + self.write_update() else: # Load dicts from file with open(self._filename, "r") as f: @@ -23,18 +23,18 @@ class DataStore: self._time_slots = [deserialize(d) for d in self._time_slots] - def _write_update(self): + def write_update(self): """Saves the updates made to the DataStore object to disk.""" with open(self._filename, "w+") as f: json.dump(self._time_slots, f, default=vars) def add_time_slot(self, time_slot: TimeSlot): self._time_slots.append(time_slot) - self._write_update() + self.write_update() def remove_time_slot(self, time_slot: TimeSlot): self._time_slots.remove(time_slot) - self._write_update() + self.write_update() def get_all_time_slots(self): return self._time_slots[:] diff --git a/lib/TimeSlot.py b/lib/TimeSlot.py index 7780d3c..0241dca 100644 --- a/lib/TimeSlot.py +++ b/lib/TimeSlot.py @@ -37,3 +37,6 @@ class TimeSlot(): if d < self.start: raise ValueError("End date must be after the start date.") self._end = d.timestamp() + + def end_now(self): + self.end = datetime.now() diff --git a/main.py b/main.py index ca131ab..c334a5b 100755 --- a/main.py +++ b/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}.") +