2021-02-06 20:55:47 +00:00
|
|
|
from owrx.property import PropertyLayer
|
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
|
2020-03-29 15:14:37 +00:00
|
|
|
import json
|
2021-02-06 20:55:47 +00:00
|
|
|
from glob import glob
|
2020-03-29 16:08:26 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2021-02-06 15:38:03 +00:00
|
|
|
from configparser import ConfigParser
|
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
|
|
|
|
|
|
|
|
|
2021-02-06 21:08:27 +00:00
|
|
|
class ConfigError(Exception):
|
2020-03-21 21:40:39 +00:00
|
|
|
def __init__(self, key, message):
|
2021-02-06 21:08:27 +00:00
|
|
|
super().__init__("Configuration Error (key: {0}): {1}".format(key, message))
|
2019-05-18 19:38:15 +00:00
|
|
|
|
2019-05-09 20:44:29 +00:00
|
|
|
|
2020-03-29 16:08:26 +00:00
|
|
|
class ConfigMigrator(ABC):
|
|
|
|
@abstractmethod
|
|
|
|
def migrate(self, config):
|
|
|
|
pass
|
|
|
|
|
2020-05-14 20:56:49 +00:00
|
|
|
def renameKey(self, config, old, new):
|
2021-02-06 20:55:47 +00:00
|
|
|
if old in config and new not in config:
|
2020-05-14 20:56:49 +00:00
|
|
|
config[new] = config[old]
|
|
|
|
del config[old]
|
|
|
|
|
2020-03-29 16:08:26 +00:00
|
|
|
|
|
|
|
class ConfigMigratorVersion1(ConfigMigrator):
|
|
|
|
def migrate(self, config):
|
2020-03-29 16:49:13 +00:00
|
|
|
if "receiver_gps" in config:
|
|
|
|
gps = config["receiver_gps"]
|
|
|
|
config["receiver_gps"] = {"lat": gps[0], "lon": gps[1]}
|
|
|
|
|
|
|
|
if "waterfall_auto_level_margin" in config:
|
|
|
|
levels = config["waterfall_auto_level_margin"]
|
|
|
|
config["waterfall_auto_level_margin"] = {"min": levels[0], "max": levels[1]}
|
2020-03-29 16:08:26 +00:00
|
|
|
|
2020-05-14 20:56:49 +00:00
|
|
|
self.renameKey(config, "wsjt_queue_workers", "decoding_queue_workers")
|
|
|
|
self.renameKey(config, "wsjt_queue_length", "decoding_queue_length")
|
|
|
|
|
2020-03-29 16:08:26 +00:00
|
|
|
config["version"] = 2
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2020-09-17 18:59:16 +00:00
|
|
|
class ConfigMigratorVersion2(ConfigMigrator):
|
|
|
|
def migrate(self, config):
|
|
|
|
if "waterfall_colors" in config and any(v > 0xFFFFFF for v in config["waterfall_colors"]):
|
|
|
|
config["waterfall_colors"] = [v >> 8 for v in config["waterfall_colors"]]
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2021-02-06 15:38:03 +00:00
|
|
|
class CoreConfig(object):
|
|
|
|
defaults = {
|
|
|
|
"core": {
|
|
|
|
"data_directory": "/var/lib/openwebrx",
|
2021-02-06 20:55:47 +00:00
|
|
|
"temporary_directory": "/tmp",
|
2021-02-06 15:38:03 +00:00
|
|
|
},
|
|
|
|
"web": {
|
|
|
|
"port": 8073,
|
|
|
|
},
|
2021-02-06 23:21:57 +00:00
|
|
|
"aprs": {
|
|
|
|
"symbols_path": "/usr/share/aprs-symbols/png"
|
|
|
|
}
|
2021-02-06 15:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
config = ConfigParser()
|
2021-02-06 23:21:57 +00:00
|
|
|
# set up config defaults
|
|
|
|
config.read_dict(CoreConfig.defaults)
|
|
|
|
# check for overrides
|
2021-02-06 20:55:47 +00:00
|
|
|
overrides_dir = "/etc/openwebrx/openwebrx.conf.d"
|
|
|
|
if os.path.exists(overrides_dir) and os.path.isdir(overrides_dir):
|
|
|
|
overrides = glob(overrides_dir + "/*.conf")
|
|
|
|
else:
|
|
|
|
overrides = []
|
|
|
|
# sequence things together
|
|
|
|
config.read(["./openwebrx.conf", "/etc/openwebrx/openwebrx.conf"] + overrides)
|
2021-02-06 23:21:57 +00:00
|
|
|
self.data_directory = config.get("core", "data_directory")
|
2021-02-06 20:55:47 +00:00
|
|
|
CoreConfig.checkDirectory(self.data_directory, "data_directory")
|
2021-02-06 23:21:57 +00:00
|
|
|
self.temporary_directory = config.get("core", "temporary_directory")
|
2021-02-06 20:55:47 +00:00
|
|
|
CoreConfig.checkDirectory(self.temporary_directory, "temporary_directory")
|
2021-02-06 23:21:57 +00:00
|
|
|
self.web_port = config.getint("web", "port")
|
|
|
|
self.aprs_symbols_path = config.get("aprs", "symbols_path")
|
2021-02-06 15:38:03 +00:00
|
|
|
|
2021-02-06 20:55:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def checkDirectory(dir, key):
|
|
|
|
if not os.path.exists(dir):
|
|
|
|
raise ConfigError(key, "{dir} doesn't exist".format(dir=dir))
|
|
|
|
if not os.path.isdir(dir):
|
|
|
|
raise ConfigError(key, "{dir} is not a directory".format(dir=dir))
|
|
|
|
if not os.access(dir, os.W_OK):
|
|
|
|
raise ConfigError(key, "{dir} is not writable".format(dir=dir))
|
|
|
|
|
2021-02-06 15:38:03 +00:00
|
|
|
def get_web_port(self):
|
|
|
|
return self.web_port
|
|
|
|
|
|
|
|
def get_data_directory(self):
|
|
|
|
return self.data_directory
|
|
|
|
|
2021-02-06 20:55:47 +00:00
|
|
|
def get_temporary_directory(self):
|
|
|
|
return self.temporary_directory
|
|
|
|
|
2021-02-06 23:21:57 +00:00
|
|
|
def get_aprs_symbols_path(self):
|
|
|
|
return self.aprs_symbols_path
|
|
|
|
|
2021-02-06 15:38:03 +00:00
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
class Config:
|
|
|
|
sharedConfig = None
|
2020-09-17 18:59:16 +00:00
|
|
|
currentVersion = 3
|
2020-03-29 16:08:26 +00:00
|
|
|
migrators = {
|
2020-09-17 18:59:16 +00:00
|
|
|
1: ConfigMigratorVersion1(),
|
|
|
|
2: ConfigMigratorVersion2(),
|
2020-03-29 16:08:26 +00:00
|
|
|
}
|
2019-05-09 20:44:29 +00:00
|
|
|
|
2020-03-21 21:40:39 +00:00
|
|
|
@staticmethod
|
2020-03-27 22:44:03 +00:00
|
|
|
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)
|
2020-03-23 22:56:05 +00:00
|
|
|
pm = PropertyLayer()
|
2020-03-27 22:44:03 +00:00
|
|
|
for name, value in cfg.__dict__.items():
|
|
|
|
if name.startswith("__"):
|
|
|
|
continue
|
|
|
|
pm[name] = value
|
|
|
|
return pm
|
|
|
|
|
|
|
|
@staticmethod
|
2020-03-29 15:14:37 +00:00
|
|
|
def _loadJsonFile(file):
|
2020-03-27 22:44:03 +00:00
|
|
|
with open(file, "r") as f:
|
|
|
|
pm = PropertyLayer()
|
2020-03-29 15:14:37 +00:00
|
|
|
for k, v in json.load(f).items():
|
2020-03-27 22:44:03 +00:00
|
|
|
pm[k] = v
|
|
|
|
return pm
|
|
|
|
|
2021-02-06 15:38:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def _getSettingsFile():
|
|
|
|
coreConfig = CoreConfig()
|
|
|
|
return "{data_directory}/settings.json".format(data_directory=coreConfig.get_data_directory())
|
|
|
|
|
2020-03-27 22:44:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def _loadConfig():
|
2021-02-06 15:38:03 +00:00
|
|
|
for file in [Config._getSettingsFile(), "/etc/openwebrx/config_webrx.py", "./config_webrx.py"]:
|
2019-12-08 16:15:48 +00:00
|
|
|
try:
|
2020-03-27 22:44:03 +00:00
|
|
|
if file.endswith(".py"):
|
|
|
|
return Config._loadPythonFile(file)
|
2020-03-29 15:14:37 +00:00
|
|
|
elif file.endswith(".json"):
|
|
|
|
return Config._loadJsonFile(file)
|
2020-03-27 22:44:03 +00:00
|
|
|
else:
|
|
|
|
logger.warning("unsupported file type: %s", file)
|
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:
|
2020-03-29 16:08:26 +00:00
|
|
|
Config.sharedConfig = Config._migrate(Config._loadConfig())
|
2020-03-21 21:40:39 +00:00
|
|
|
return Config.sharedConfig
|
2020-03-15 22:32:19 +00:00
|
|
|
|
2020-03-27 22:44:03 +00:00
|
|
|
@staticmethod
|
|
|
|
def store():
|
2021-02-06 15:38:03 +00:00
|
|
|
with open(Config._getSettingsFile(), "w") as file:
|
2020-03-29 15:14:37 +00:00
|
|
|
json.dump(Config.get().__dict__(), file, indent=4)
|
2020-03-27 22:44:03 +00:00
|
|
|
|
2020-03-15 22:32:19 +00:00
|
|
|
@staticmethod
|
|
|
|
def validateConfig():
|
2021-02-06 21:08:27 +00:00
|
|
|
# no config checks atm
|
|
|
|
# just basic loading verification
|
|
|
|
Config.get()
|
2020-03-29 16:08:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _migrate(config):
|
|
|
|
version = config["version"] if "version" in config else 1
|
|
|
|
if version == Config.currentVersion:
|
|
|
|
return config
|
|
|
|
|
|
|
|
logger.debug("migrating config from version %i", version)
|
2020-09-17 18:59:16 +00:00
|
|
|
migrators = [Config.migrators[i] for i in range(version, Config.currentVersion)]
|
|
|
|
for migrator in migrators:
|
|
|
|
config = migrator.migrate(config)
|
|
|
|
return config
|