A bunch more state cleanup, no need for functions that only get used once
This commit is contained in:
parent
ef4dd5c571
commit
63d9081e07
1 changed files with 11 additions and 41 deletions
|
@ -171,16 +171,17 @@ def datetime_to_ts(ts: datetime) -> str:
|
||||||
|
|
||||||
class State():
|
class State():
|
||||||
def __init__(self, data: dict, file: str | None = None):
|
def __init__(self, data: dict, file: str | None = None):
|
||||||
"""Initialise and validate the state"""
|
"""Copy the data, migrate if needed, and validate"""
|
||||||
self.validate(data)
|
|
||||||
self._dict = copy.deepcopy(data)
|
self._dict = copy.deepcopy(data)
|
||||||
self._file = file
|
self._file = file
|
||||||
|
|
||||||
def validate(self, dict: dict = None):
|
version = self._dict.get("version", 0)
|
||||||
"""Initialise and validate a state dict"""
|
for i in range(version, _VERSION):
|
||||||
if not dict:
|
logger.info("Migrating from v%s to v%s", version, version+1)
|
||||||
dict = self._dict
|
_MIGRATIONS[i](self._dict)
|
||||||
_SCHEMA.validate(dict)
|
self._dict[_Key.VERSION] = _VERSION
|
||||||
|
|
||||||
|
_SCHEMA.validate(self._dict)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def safe_write(func):
|
def safe_write(func):
|
||||||
|
@ -193,9 +194,9 @@ class State():
|
||||||
def inner(self: State, *args, **kwargs):
|
def inner(self: State, *args, **kwargs):
|
||||||
tmp = State(self._dict, self._file)
|
tmp = State(self._dict, self._file)
|
||||||
func(tmp, *args, **kwargs)
|
func(tmp, *args, **kwargs)
|
||||||
tmp.validate()
|
_SCHEMA.validate(tmp._dict)
|
||||||
if tmp._file:
|
if tmp._file:
|
||||||
tmp._save_to_file()
|
ops.save(tmp._file, tmp._dict)
|
||||||
self._dict = tmp._dict
|
self._dict = tmp._dict
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
@ -345,11 +346,6 @@ class State():
|
||||||
# We did not manage to remove the schedule (or add it? though that should be impossible)
|
# We did not manage to remove the schedule (or add it? though that should be impossible)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@property
|
|
||||||
def dict_internal_copy(self) -> dict:
|
|
||||||
"""Only to be used to get the internal dict as a copy"""
|
|
||||||
return copy.deepcopy(self._dict)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def _users(self) -> dict[str]:
|
def _users(self) -> dict[str]:
|
||||||
return self._dict[_Key.USERS]
|
return self._dict[_Key.USERS]
|
||||||
|
@ -361,43 +357,17 @@ class State():
|
||||||
@safe_write
|
@safe_write
|
||||||
def _set_user_channel_prop(self, id: str, channel_id: str, key: str, value):
|
def _set_user_channel_prop(self, id: str, channel_id: str, key: str, value):
|
||||||
"""Set a user channel property helper"""
|
"""Set a user channel property helper"""
|
||||||
# Dive in
|
|
||||||
user = self._users.setdefault(str(id), {})
|
user = self._users.setdefault(str(id), {})
|
||||||
channels = user.setdefault(_Key.CHANNELS, {})
|
channels = user.setdefault(_Key.CHANNELS, {})
|
||||||
channel = channels.setdefault(str(channel_id), {})
|
channel = channels.setdefault(str(channel_id), {})
|
||||||
|
|
||||||
# Set the value
|
|
||||||
channel[key] = value
|
channel[key] = value
|
||||||
|
|
||||||
def _save_to_file(self):
|
|
||||||
"""Saves the state out to the chosen file"""
|
|
||||||
ops.save(self._file, self.dict_internal_copy)
|
|
||||||
|
|
||||||
|
|
||||||
def _migrate(dict: dict):
|
|
||||||
"""Migrate a dict through versions"""
|
|
||||||
version = dict.get("version", 0)
|
|
||||||
for i in range(version, _VERSION):
|
|
||||||
logger.info("Migrating from v%s to v%s", version, version+1)
|
|
||||||
_MIGRATIONS[i](dict)
|
|
||||||
dict[_Key.VERSION] = _VERSION
|
|
||||||
|
|
||||||
|
|
||||||
def load_from_file(file: str) -> State:
|
def load_from_file(file: str) -> State:
|
||||||
"""
|
"""
|
||||||
Load the state from a files
|
Load the state from a files
|
||||||
Apply any required migrations
|
|
||||||
"""
|
"""
|
||||||
loaded = _EMPTY_DICT
|
loaded = ops.load(file) if os.path.isfile(file) else _EMPTY_DICT
|
||||||
|
|
||||||
# If there's a file load it and try to migrate
|
|
||||||
if os.path.isfile(file):
|
|
||||||
loaded = ops.load(file)
|
|
||||||
_migrate(loaded)
|
|
||||||
|
|
||||||
st = State(loaded, file)
|
st = State(loaded, file)
|
||||||
|
|
||||||
# Save out the migrated (or new) file
|
|
||||||
ops.save(file, st._dict)
|
ops.save(file, st._dict)
|
||||||
|
|
||||||
return st
|
return st
|
||||||
|
|
Loading…
Add table
Reference in a new issue