From ca5889f925875c3ab5d84f1cf972016f1d9bcf3b Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 15 Mar 2020 23:32:19 +0100 Subject: [PATCH] introduce config checking infrastructure --- owrx/__main__.py | 16 +++++++++++++--- owrx/config.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/owrx/__main__.py b/owrx/__main__.py index d452e4a..109bf87 100644 --- a/owrx/__main__.py +++ b/owrx/__main__.py @@ -1,6 +1,6 @@ from http.server import HTTPServer from owrx.http import RequestHandler -from owrx.config import PropertyManager +from owrx.config import PropertyManager, Config from owrx.feature import FeatureDetector from owrx.sdr import SdrService from socketserver import ThreadingMixIn @@ -12,6 +12,7 @@ from owrx.pskreporter import PskReporter import logging logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") +logger = logging.getLogger(__name__) class ThreadedHttpServer(ThreadingMixIn, HTTPServer): @@ -32,13 +33,22 @@ Author contact info: Jakob Ketterl, DD5JFK pm = PropertyManager.getSharedInstance().loadConfig() + configErrors = Config.validateConfig() + if configErrors: + logger.error( + "your configuration contains errors. please address the following errors:" + ) + for e in configErrors: + logger.error(e) + return + featureDetector = FeatureDetector() if not featureDetector.is_available("core"): - print( + logger.error( "you are missing required dependencies to run openwebrx. " "please check that the following core requirements are installed:" ) - print(", ".join(featureDetector.get_requirements("core"))) + logger.error(", ".join(featureDetector.get_requirements("core"))) return # Get error messages about unknown / unavailable features as soon as possible diff --git a/owrx/config.py b/owrx/config.py index 09bbd8f..b027f48 100644 --- a/owrx/config.py +++ b/owrx/config.py @@ -1,5 +1,6 @@ import importlib.util import logging +import os logger = logging.getLogger(__name__) @@ -147,3 +148,36 @@ class PropertyManager(object): except FileNotFoundError: pass raise ConfigNotFoundException("no usable config found! please make sure you have a valid configuration file!") + + +class ConfigError(object): + def __init__(self, key, message): + self.key = key + self.message = message + + def __str__(self): + return "Configuration Error (key: {0}): {1}".format(self.key, self.message) + + +class Config: + @staticmethod + def validateConfig(): + pm = PropertyManager.getSharedInstance() + errors = [ + Config.checkTempDirectory(pm) + ] + + return [e for e in errors if e is not None] + + @staticmethod + def checkTempDirectory(pm: PropertyManager): + key = "temporary_directory" + if not key 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