diff --git a/owrx/__main__.py b/owrx/__main__.py index 4c791ea1..468d4912 100644 --- a/owrx/__main__.py +++ b/owrx/__main__.py @@ -20,6 +20,7 @@ from owrx.reporting import ReportingEngine from owrx.version import openwebrx_version from owrx.audio.queue import DecoderQueue from owrx.admin import add_admin_parser, run_admin_action +from pathlib import Path import signal import argparse import socket @@ -44,6 +45,14 @@ def handleSignal(sig, frame): def main(): parser = argparse.ArgumentParser(description="OpenWebRX - Open Source SDR Web App for Everyone!") + parser.add_argument( + "-c", + "--config", + action="store", + help="Read core configuration from specified file", + metavar="configfile", + type=Path, + ) parser.add_argument("-v", "--version", action="store_true", help="Show the software version") parser.add_argument("--debug", action="store_true", help="Set loglevel to DEBUG") @@ -66,6 +75,8 @@ def main(): print("OpenWebRX version {version}".format(version=openwebrx_version)) return 0 + CoreConfig.load(args.config) + if args.module == "admin": return run_admin_action(adminparser, args) diff --git a/owrx/config/core.py b/owrx/config/core.py index e6026fb4..0d8341cb 100644 --- a/owrx/config/core.py +++ b/owrx/config/core.py @@ -1,10 +1,12 @@ from owrx.config import ConfigError from configparser import ConfigParser +from pathlib import Path import os -from glob import glob class CoreConfig(object): + defaultSearchLocations = ["./openwebrx.conf", "/etc/openwebrx/openwebrx.conf"] + defaults = { "core": { "data_directory": "/var/lib/openwebrx", @@ -20,18 +22,41 @@ class CoreConfig(object): } } - def __init__(self): + sharedConfig = None + + @staticmethod + def load(file: Path = None): + + def expand_base(base: Path): + # check if config exists + if not base.exists() or not base.is_file(): + return [] + # every location can additionally have a directory containing config overrides + # this directory must have the same name, with the ".d" suffix + override_dir = Path(str(base) + ".d") + # check if override dir exists + if not override_dir.exists() or not override_dir.is_dir(): + return [base] + # load all .conf files from the override dir + overrides = override_dir.glob("*.conf") + return [base] + [o for o in overrides if o.is_file()] + + if file is None: + bases = [Path(b) for b in CoreConfig.defaultSearchLocations] + else: + bases = [file] + configFiles = [o for b in bases for o in expand_base(b)] + config = ConfigParser() # set up config defaults config.read_dict(CoreConfig.defaults) - # check for overrides - 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) + # read the allocated files + config.read(configFiles) + + CoreConfig.sharedConfig = config + + def __init__(self): + config = CoreConfig.sharedConfig self.data_directory = config.get("core", "data_directory") CoreConfig.checkDirectory(self.data_directory, "data_directory") self.temporary_directory = config.get("core", "temporary_directory")