restructuring

This commit is contained in:
2024-06-19 14:55:39 +02:00
parent bf76c368c9
commit 6fa1c5a4c3
5 changed files with 36 additions and 32 deletions

40
model/DataStore.py Normal file
View File

@@ -0,0 +1,40 @@
from pathlib import Path
from .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)
self.write_update()
def remove_time_slot(self, time_slot: TimeSlot):
self._time_slots.remove(time_slot)
self.write_update()
def get_all_time_slots(self):
return self._time_slots[:]

44
model/TimeSlot.py Normal file
View File

@@ -0,0 +1,44 @@
from datetime import datetime
class TimeSlot():
def __init__(self, name: str, start: datetime = datetime.now(), end: datetime = None):
self.name = name
self.start = start
if end:
self.end = end
self._creation_time = datetime.now().timestamp()
@property
def name(self):
return self._name
@name.setter
def name(self, n: str):
self._name = n
@property
def start(self):
return datetime.fromtimestamp(self._start)
@start.setter
def start(self, d: datetime):
_d = d.replace(second=0, microsecond=0)
self._start = _d.timestamp()
@property
def end(self):
if "_end" in self.__dict__:
return datetime.fromtimestamp(self._end)
return None
@end.setter
def end(self, d: datetime):
_d = d.replace(second=0, microsecond=0)
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()