from pathlib import Path from model.TimeSlot import TimeSlot import json 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) def add_time_slot(self, time_slot: TimeSlot): self._time_slots.append(time_slot) def remove_time_slot(self, time_slot: TimeSlot): self._time_slots.remove(time_slot) def get_all_time_slots(self) -> list[TimeSlot]: return self._time_slots[:]