2020-03-21 21:40:39 +00:00
|
|
|
from owrx.property import PropertyManager
|
2019-12-08 16:15:48 +00:00
|
|
|
import importlib.util
|
2020-03-15 22:32:19 +00:00
|
|
|
import os
|
2020-03-21 21:40:39 +00:00
|
|
|
import logging
|
2019-07-21 17:40:28 +00:00
|
|
|
|
2019-05-10 19:50:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-05-12 13:56:18 +00:00
|
|
|
|
2019-12-08 16:15:48 +00:00
|
|
|
class ConfigNotFoundException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
class ConfigError(object):
|
|
|
|
def __init__(self, key, message):
|
|
|
|
self.key = key
|
|
|
|
self.message = message
|
2019-05-05 17:59:03 +00:00
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "Configuration Error (key: {0}): {1}".format(self.key, self.message)
|
2019-05-18 19:38:15 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
class Config:
|
|
|
|
sharedConfig = None
|
2019-05-09 20:44:29 +00:00
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
@staticmethod
|
|
|
|
def _loadConfig():
|
|
|
|
pm = PropertyManager()
|
2019-12-08 20:00:01 +00:00
|
|
|
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
|
2020-03-21 21:40:39 +00:00
|
|
|
pm[name] = value
|
|
|
|
return pm
|
2019-12-08 16:15:48 +00:00
|
|
|
except FileNotFoundError:
|
2020-02-01 20:37:43 +00:00
|
|
|
pass
|
2019-12-08 16:15:48 +00:00
|
|
|
raise ConfigNotFoundException("no usable config found! please make sure you have a valid configuration file!")
|
2020-03-15 22:32:19 +00:00
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
if Config.sharedConfig is None:
|
|
|
|
Config.sharedConfig = Config._loadConfig()
|
|
|
|
return Config.sharedConfig
|
2020-03-15 22:32:19 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def validateConfig():
|
2020-03-21 21:40:39 +00:00
|
|
|
pm = Config.get()
|
2020-03-15 22:32:19 +00:00
|
|
|
errors = [
|
|
|
|
Config.checkTempDirectory(pm)
|
|
|
|
]
|
|
|
|
|
|
|
|
return [e for e in errors if e is not None]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def checkTempDirectory(pm: PropertyManager):
|
|
|
|
key = "temporary_directory"
|
2020-03-21 21:40:39 +00:00
|
|
|
if key not in pm or pm[key] is None:
|
2020-03-15 22:32:19 +00:00
|
|
|
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
|