Added implementation of config changes in the utils.Config class

This commit is contained in:
BlueFox 2024-11-15 18:26:05 +01:00
parent aa3110d564
commit 2aac36e15c
Signed by: BlueFox
GPG Key ID: 327233DA85435270

View File

@ -54,7 +54,7 @@ class Config:
def save_config(self):
with open(self._config_file, "w") as f:
from json import dump
dump(self._config, f)
dump(self._config, f, separators=(',\n', ': '))
del dump
collect()
@ -102,8 +102,17 @@ class Config:
def __setattr__(self, name, value):
#print(f"Someone tried to edit my poor attributes! Affected: '{name}' should be set to '{value}'")
object.__setattr__(self, name, value)
if name.startswith("_"): # make private attributes settable as normal
object.__setattr__(self, name, value)
elif name in self._attr_list: # valid attributes (only capital letters and -_ etc. are allowed)
try:
self._config[name] = value
self.save_config()
except KeyError:
raise AttributeError(f"Attribute '{name}' does not exist in the config file '{self._config_file}'")
else:
raise AttributeError(f"Can't set attribute '{name}' for a '{self.__class__.__name__}' object: forbidden")
def __delattr__(self, name):
raise AttributeError(f"You may not delete any attribute of the '{self.__class__.__name__}' object")
@ -124,3 +133,5 @@ def log(log_level: int, message: str):
elif cfg.LOG_LEVEL >= log_level: # if log level is valid
print(f"[{log_mapping[log_level]}] {message}")