openwebrx-clone/owrx/config.py

69 lines
2.1 KiB
Python
Raw Normal View History

from owrx.property import PropertyLayer
2019-12-08 16:15:48 +00:00
import importlib.util
import os
import logging
2019-05-10 19:50:58 +00:00
logger = logging.getLogger(__name__)
2019-12-08 16:15:48 +00:00
class ConfigNotFoundException(Exception):
pass
class ConfigError(object):
def __init__(self, key, message):
self.key = key
self.message = message
2019-05-05 17:59:03 +00:00
def __str__(self):
return "Configuration Error (key: {0}): {1}".format(self.key, self.message)
2019-05-09 20:44:29 +00:00
class Config:
sharedConfig = None
2019-05-09 20:44:29 +00:00
@staticmethod
def _loadConfig():
pm = PropertyLayer()
for file in ["/etc/openwebrx/config_webrx.py", "./config_webrx.py"]:
2019-12-08 16:15:48 +00:00
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
2019-12-08 16:15:48 +00:00
except FileNotFoundError:
pass
2019-12-08 16:15:48 +00:00
raise ConfigNotFoundException("no usable config found! please make sure you have a valid configuration file!")
@staticmethod
def get():
if Config.sharedConfig is None:
Config.sharedConfig = Config._loadConfig()
return Config.sharedConfig
@staticmethod
def validateConfig():
pm = Config.get()
errors = [
Config.checkTempDirectory(pm)
]
return [e for e in errors if e is not None]
@staticmethod
def checkTempDirectory(pm: PropertyLayer):
key = "temporary_directory"
if key not in pm or pm[key] is None:
return ConfigError(key, "temporary directory is not set")
if not os.path.exists(pm[key]):
return ConfigError(key, "temporary directory doesn't exist")
if not os.path.isdir(pm[key]):
return ConfigError(key, "temporary directory path is not a directory")
if not os.access(pm[key], os.W_OK):
return ConfigError(key, "temporary directory is not writable")
return None