From 2aac36e15cd00e64c25399fa9c5dbb25113285e8 Mon Sep 17 00:00:00 2001 From: BlueFox Date: Fri, 15 Nov 2024 18:26:05 +0100 Subject: [PATCH] Added implementation of config changes in the utils.Config class --- utils.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/utils.py b/utils.py index ad5b76c..cad71dd 100644 --- a/utils.py +++ b/utils.py @@ -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}") + +