diff --git a/owrx/config.py b/owrx/config.py index 7f93733..a339410 100644 --- a/owrx/config.py +++ b/owrx/config.py @@ -2,6 +2,7 @@ from owrx.property import PropertyManager, PropertyLayer import importlib.util import os import logging +import yaml logger = logging.getLogger(__name__) @@ -23,18 +24,35 @@ class Config: sharedConfig = None @staticmethod - def _loadConfig(): + def _loadPythonFile(file): + spec = importlib.util.spec_from_file_location("config_webrx", file) + cfg = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cfg) pm = PropertyLayer() - for file in ["/etc/openwebrx/config_webrx.py", "./config_webrx.py"]: + for name, value in cfg.__dict__.items(): + if name.startswith("__"): + continue + pm[name] = value + return pm + + @staticmethod + def _loadYamlFile(file): + with open(file, "r") as f: + pm = PropertyLayer() + for k, v in yaml.load(f).items(): + pm[k] = v + return pm + + @staticmethod + def _loadConfig(): + for file in ["settings.yaml", "/etc/openwebrx/config_webrx.py", "./config_webrx.py"]: try: - spec = importlib.util.spec_from_file_location("config_webrx", file) - cfg = importlib.util.module_from_spec(spec) - spec.loader.exec_module(cfg) - for name, value in cfg.__dict__.items(): - if name.startswith("__"): - continue - pm[name] = value - return pm + if file.endswith(".py"): + return Config._loadPythonFile(file) + elif file.endswith(".yaml"): + return Config._loadYamlFile(file) + else: + logger.warning("unsupported file type: %s", file) except FileNotFoundError: pass raise ConfigNotFoundException("no usable config found! please make sure you have a valid configuration file!") @@ -45,6 +63,11 @@ class Config: Config.sharedConfig = Config._loadConfig() return Config.sharedConfig + @staticmethod + def store(): + with open("settings.yaml", "w") as file: + yaml.dump(Config.get().__dict__(), file) + @staticmethod def validateConfig(): pm = Config.get() diff --git a/owrx/controllers/settings.py b/owrx/controllers/settings.py index 4276f40..4e24f43 100644 --- a/owrx/controllers/settings.py +++ b/owrx/controllers/settings.py @@ -340,4 +340,5 @@ class SettingsController(AdminController): config = Config.get() for k, v in data.items(): config[k] = v + Config.store() self.send_redirect("/admin")