add first steps towards a storage implementation
This commit is contained in:
parent
54dc412c4a
commit
3011e62fad
@ -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):
|
||||||
pm = PropertyLayer()
|
|
||||||
for file in ["/etc/openwebrx/config_webrx.py", "./config_webrx.py"]:
|
|
||||||
try:
|
|
||||||
spec = importlib.util.spec_from_file_location("config_webrx", file)
|
spec = importlib.util.spec_from_file_location("config_webrx", file)
|
||||||
cfg = importlib.util.module_from_spec(spec)
|
cfg = importlib.util.module_from_spec(spec)
|
||||||
spec.loader.exec_module(cfg)
|
spec.loader.exec_module(cfg)
|
||||||
|
pm = PropertyLayer()
|
||||||
for name, value in cfg.__dict__.items():
|
for name, value in cfg.__dict__.items():
|
||||||
if name.startswith("__"):
|
if name.startswith("__"):
|
||||||
continue
|
continue
|
||||||
pm[name] = value
|
pm[name] = value
|
||||||
return pm
|
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:
|
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()
|
||||||
|
@ -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")
|
||||||
|
Loading…
Reference in New Issue
Block a user