add first steps towards a storage implementation

This commit is contained in:
Jakob Ketterl 2020-03-27 23:44:03 +01:00
parent 54dc412c4a
commit 3011e62fad
2 changed files with 34 additions and 10 deletions

View File

@ -2,6 +2,7 @@ from owrx.property import PropertyManager, PropertyLayer
import importlib.util import importlib.util
import os import os
import logging import logging
import yaml
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -23,18 +24,35 @@ class Config:
sharedConfig = None sharedConfig = None
@staticmethod @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() 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: try:
spec = importlib.util.spec_from_file_location("config_webrx", file) if file.endswith(".py"):
cfg = importlib.util.module_from_spec(spec) return Config._loadPythonFile(file)
spec.loader.exec_module(cfg) elif file.endswith(".yaml"):
for name, value in cfg.__dict__.items(): return Config._loadYamlFile(file)
if name.startswith("__"): else:
continue logger.warning("unsupported file type: %s", file)
pm[name] = value
return pm
except FileNotFoundError: except FileNotFoundError:
pass pass
raise ConfigNotFoundException("no usable config found! please make sure you have a valid configuration file!") 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() Config.sharedConfig = Config._loadConfig()
return Config.sharedConfig return Config.sharedConfig
@staticmethod
def store():
with open("settings.yaml", "w") as file:
yaml.dump(Config.get().__dict__(), file)
@staticmethod @staticmethod
def validateConfig(): def validateConfig():
pm = Config.get() pm = Config.get()

View File

@ -340,4 +340,5 @@ class SettingsController(AdminController):
config = Config.get() config = Config.get()
for k, v in data.items(): for k, v in data.items():
config[k] = v config[k] = v
Config.store()
self.send_redirect("/admin") self.send_redirect("/admin")