break config module apart

This commit is contained in:
Jakob Ketterl 2021-02-11 13:55:06 +01:00
parent 1cc4b13ba6
commit e926611307
4 changed files with 49 additions and 47 deletions

View File

@ -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": {

7
owrx/config/error.py Normal file
View File

@ -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))

36
owrx/config/migration.py Normal file
View File

@ -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

View File

@ -20,6 +20,7 @@ setup(
"owrx.controllers",
"owrx.property",
"owrx.form",
"owrx.config",
"csdr",
"htdocs",
"owrxadmin",