From 0abf886d11ee9631ea5c641a2d0662393fc9074b Mon Sep 17 00:00:00 2001 From: Elias Kohout Date: Thu, 13 Jun 2024 10:21:19 +0200 Subject: [PATCH] add deserialization of TimeSlot --- lib/DataStore.py | 11 +++++++++++ main.py | 21 ++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/DataStore.py b/lib/DataStore.py index f75708c..ee69298 100644 --- a/lib/DataStore.py +++ b/lib/DataStore.py @@ -7,13 +7,24 @@ class DataStore: def __init__(self, filename: Path, create=False): self._filename = filename if create: + # Store emtpty list self._time_slots = [] self._write_update() else: + # Load dicts from file with open(self._filename, "r") as f: self._time_slots = list(json.load(f)) + # Convert dicts into TimeSlot objects + def deserialize(d: dict) -> TimeSlot: + ts = TimeSlot.__new__(TimeSlot) # Create a new, empty instance + ts.__dict__.update(d) + return ts + + self._time_slots = [deserialize(d) for d in self._time_slots] + 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) diff --git a/main.py b/main.py index 39c2479..b7c3cff 100755 --- a/main.py +++ b/main.py @@ -1,12 +1,19 @@ #!/usr/bin/python3 import argparse - -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.") - -args = parser.parse_args() +from lib.DataStore import DataStore +from lib.TimeSlot import TimeSlot +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.") + + args = parser.parse_args() + + ds = DataStore("data.json") + + if args.command == "ls": + print(ds.get_all_time_slots())