From e9266113074041592e8174b0213b406a9930cd58 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Thu, 11 Feb 2021 13:55:06 +0100 Subject: [PATCH] break config module apart --- owrx/{config.py => config/__init__.py} | 52 +++----------------------- owrx/config/error.py | 7 ++++ owrx/config/migration.py | 36 ++++++++++++++++++ setup.py | 1 + 4 files changed, 49 insertions(+), 47 deletions(-) rename owrx/{config.py => config/__init__.py} (76%) create mode 100644 owrx/config/error.py create mode 100644 owrx/config/migration.py diff --git a/owrx/config.py b/owrx/config/__init__.py similarity index 76% rename from owrx/config.py rename to owrx/config/__init__.py index b634477..d3ef341 100644 --- a/owrx/config.py +++ b/owrx/config/__init__.py @@ -1,59 +1,17 @@ +from configparser import ConfigParser from owrx.property import PropertyLayer import importlib.util import os -import logging import json from glob import glob -from abc import ABC, abstractmethod -from configparser import ConfigParser +from owrx.config.error import ConfigError, ConfigNotFoundException +from owrx.config.migration import ConfigMigratorVersion1, ConfigMigratorVersion2 + +import logging logger = logging.getLogger(__name__) -class ConfigNotFoundException(Exception): - pass - - -class ConfigError(Exception): - def __init__(self, key, message): - super().__init__("Configuration Error (key: {0}): {1}".format(key, message)) - - -class ConfigMigrator(ABC): - @abstractmethod - def migrate(self, config): - pass - - def renameKey(self, config, old, new): - if old in config and new not in config: - config[new] = config[old] - del config[old] - - -class ConfigMigratorVersion1(ConfigMigrator): - def migrate(self, config): - 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]} - - self.renameKey(config, "wsjt_queue_workers", "decoding_queue_workers") - self.renameKey(config, "wsjt_queue_length", "decoding_queue_length") - - config["version"] = 2 - return config - - -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 - - class CoreConfig(object): defaults = { "core": { diff --git a/owrx/config/error.py b/owrx/config/error.py new file mode 100644 index 0000000..9a77bcc --- /dev/null +++ b/owrx/config/error.py @@ -0,0 +1,7 @@ +class ConfigNotFoundException(Exception): + pass + + +class ConfigError(Exception): + def __init__(self, key, message): + super().__init__("Configuration Error (key: {0}): {1}".format(key, message)) diff --git a/owrx/config/migration.py b/owrx/config/migration.py new file mode 100644 index 0000000..39ded6d --- /dev/null +++ b/owrx/config/migration.py @@ -0,0 +1,36 @@ +from abc import ABC, abstractmethod + + +class ConfigMigrator(ABC): + @abstractmethod + def migrate(self, config): + pass + + def renameKey(self, config, old, new): + if old in config and new not in config: + config[new] = config[old] + del config[old] + + +class ConfigMigratorVersion1(ConfigMigrator): + def migrate(self, config): + 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]} + + self.renameKey(config, "wsjt_queue_workers", "decoding_queue_workers") + self.renameKey(config, "wsjt_queue_length", "decoding_queue_length") + + config["version"] = 2 + return config + + +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 diff --git a/setup.py b/setup.py index 695e658..f15522a 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ setup( "owrx.controllers", "owrx.property", "owrx.form", + "owrx.config", "csdr", "htdocs", "owrxadmin",