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 os
import logging
import yaml
logger = logging.getLogger(__name__)
@ -23,18 +24,35 @@ class Config:
sharedConfig = None
@staticmethod
def _loadConfig():
pm = PropertyLayer()
for file in ["/etc/openwebrx/config_webrx.py", "./config_webrx.py"]:
try:
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 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:
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()

View File

@ -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")