adding persistence
This commit is contained in:
29
lib/DataStore.py
Normal file
29
lib/DataStore.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from pathlib import Path
|
||||
from .TimeSlot import TimeSlot
|
||||
import json
|
||||
|
||||
|
||||
class DataStore:
|
||||
def __init__(self, filename: Path, create=False):
|
||||
self._filename = filename
|
||||
if create:
|
||||
self._time_slots = []
|
||||
self._write_update()
|
||||
else:
|
||||
with open(self._filename, "r") as f:
|
||||
self._time_slots = list(json.load(f))
|
||||
|
||||
def _write_update(self):
|
||||
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[:]
|
||||
Reference in New Issue
Block a user