From 9bcbf5e9b3d6adaff56fcf253d5787035067ce6b Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 3 Sep 2024 21:46:40 +0200 Subject: [PATCH] Update config.py --- backend/config.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/backend/config.py b/backend/config.py index 1fc673ad9..d693fe443 100644 --- a/backend/config.py +++ b/backend/config.py @@ -47,7 +47,7 @@ def run_migrations(): from alembic import command from alembic.config import Config - alembic_cfg = Config(BACKEND_DIR / "alembic.ini") + alembic_cfg = Config("alembic.ini") command.upgrade(alembic_cfg, "head") except Exception as e: print(f"Error: {e}") @@ -90,15 +90,6 @@ if os.path.exists(f"{DATA_DIR}/config.json"): save_to_db(data) os.rename(f"{DATA_DIR}/config.json", f"{DATA_DIR}/old_config.json") - -def save_config(): - try: - with open(f"{DATA_DIR}/config.json", "w") as f: - json.dump(CONFIG_DATA, f, indent="\t") - except Exception as e: - log.exception(e) - - DEFAULT_CONFIG = { "version": 0, "ui": { @@ -172,6 +163,25 @@ def get_config_value(config_path: str): return cur_config +PERSISTENT_CONFIG_REGISTRY = [] + + +def save_config(config): + global CONFIG_DATA + global PERSISTENT_CONFIG_REGISTRY + try: + save_to_db(config) + CONFIG_DATA = config + + # Trigger updates on all registered PersistentConfig entries + for config_item in PERSISTENT_CONFIG_REGISTRY: + config_item.update() + except Exception as e: + log.exception(e) + return False + return True + + T = TypeVar("T") @@ -187,6 +197,8 @@ class PersistentConfig(Generic[T]): else: self.value = env_value + PERSISTENT_CONFIG_REGISTRY.append(self) + def __str__(self): return str(self.value) @@ -203,6 +215,12 @@ class PersistentConfig(Generic[T]): ) return super().__getattribute__(item) + def update(self): + new_value = get_config_value(self.config_path) + if new_value is not None: + self.value = new_value + log.info(f"Updated {self.env_name} to new value {self.value}") + def save(self): log.info(f"Saving '{self.env_name}' to the database") path_parts = self.config_path.split(".")