From 7c6a55d3f14aacce2df5505582c296964d071052 Mon Sep 17 00:00:00 2001 From: Michael Murton <6764025+CrazyIvan359@users.noreply.github.com> Date: Sun, 6 Dec 2020 13:49:45 -0500 Subject: [PATCH] improve config handling of invalid conditionals --- CHANGELOG.md | 8 ++++++++ mqttany/config.py | 17 ++++++----------- mqttany/modules/gpio/pin/counter.py | 1 - mqttany/modules/gpio/pin/digital.py | 1 - mqttany/modules/i2c/device/mcp230xx.py | 1 - mqttany/modules/led/array/e131.py | 1 - mqttany/modules/led/array/rpi.py | 1 - mqttany/modules/onewire/device/ds18x20.py | 1 - 8 files changed, 14 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61f04aa..f39189d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Development +* **Added** + * Config parser now considers required sections with defaults for all options to be + valid and will populate the section in the returned configuration. + +* **Fixed** + * Config parser was reporting valid configuration for optional conditional sections + containing invalid values. Fixes problem 1 in [#80](https://github.com/CrazyIvan359/mqttany/issues/80). + ## 0.12.0 ### Major Core Rewrite diff --git a/mqttany/config.py b/mqttany/config.py index 4464084..76a34de 100644 --- a/mqttany/config.py +++ b/mqttany/config.py @@ -125,18 +125,13 @@ def process_option(name, value, option, config): return False if condition_matched != False: - if value == "**NO DATA**" and option.get("type", None) == "section": - if option.get("required", True): - log.error("Missing required section '%s'", name) - return False - elif condition_matched: - log.trace("Descending into section '%s'", name) - section_valid, config[name] = parse_dict({}, option) - return True - - elif isinstance(value, dict): + if isinstance(value, dict) or ( + value == "**NO DATA**" and option.get("type", None) == "section" + ): log.trace("Descending into section '%s'", name) - section_valid, section_config = parse_dict(value, option) + section_valid, section_config = parse_dict( + {} if value == "**NO DATA**" else value, option + ) if not section_valid and option.get("required", True): log.error("Required section '%s' is not valid", name) return False diff --git a/mqttany/modules/gpio/pin/counter.py b/mqttany/modules/gpio/pin/counter.py index 6389ee6..8458004 100644 --- a/mqttany/modules/gpio/pin/counter.py +++ b/mqttany/modules/gpio/pin/counter.py @@ -77,7 +77,6 @@ class Function(enum.Enum): }, CONF_KEY_COUNTER: { "type": "section", - "required": False, "conditions": [(CONF_KEY_PIN_MODE, "COUNTER")], CONF_KEY_INTERVAL: {"default": 60, "type": int}, CONF_KEY_INTERRUPT: { diff --git a/mqttany/modules/gpio/pin/digital.py b/mqttany/modules/gpio/pin/digital.py index cfe3406..5a33627 100644 --- a/mqttany/modules/gpio/pin/digital.py +++ b/mqttany/modules/gpio/pin/digital.py @@ -73,7 +73,6 @@ }, CONF_KEY_DIGITAL: { "type": "section", - "required": False, "conditions": [ (CONF_KEY_PIN_MODE, PinMode.INPUT), (CONF_KEY_PIN_MODE, PinMode.OUTPUT), diff --git a/mqttany/modules/i2c/device/mcp230xx.py b/mqttany/modules/i2c/device/mcp230xx.py index 59f22db..1f8f2ed 100644 --- a/mqttany/modules/i2c/device/mcp230xx.py +++ b/mqttany/modules/i2c/device/mcp230xx.py @@ -70,7 +70,6 @@ class Resistor(Enum): CONF_KEY_MCP: OrderedDict( [ ("type", "section"), - ("required", True), ( "conditions", [(CONF_KEY_DEVICE, "mcp23008"), (CONF_KEY_DEVICE, "mcp23017")], diff --git a/mqttany/modules/led/array/e131.py b/mqttany/modules/led/array/e131.py index 7983489..bfc39c5 100644 --- a/mqttany/modules/led/array/e131.py +++ b/mqttany/modules/led/array/e131.py @@ -46,7 +46,6 @@ CONF_KEY_OUTPUT: {"selection": {"sacn": "sacn", "sACN": "sacn"}}, CONF_KEY_SACN: { "type": "section", - "required": False, "conditions": [(CONF_KEY_OUTPUT, "sacn")], CONF_KEY_UNIVERSE: {"type": int, "default": 1}, CONF_KEY_ADDRESS: {"type": str, "default": None}, diff --git a/mqttany/modules/led/array/rpi.py b/mqttany/modules/led/array/rpi.py index 6510cc4..a3d9ae6 100644 --- a/mqttany/modules/led/array/rpi.py +++ b/mqttany/modules/led/array/rpi.py @@ -47,7 +47,6 @@ CONF_KEY_OUTPUT: {"selection": {"rpi": "rpi", "RPi": "rpi"}}, CONF_KEY_RPI: { "type": "section", - "required": True, "conditions": [(CONF_KEY_OUTPUT, "rpi")], CONF_KEY_GPIO: {"type": int}, CONF_KEY_CHIP: { diff --git a/mqttany/modules/onewire/device/ds18x20.py b/mqttany/modules/onewire/device/ds18x20.py index fb8e3b6..083f060 100644 --- a/mqttany/modules/onewire/device/ds18x20.py +++ b/mqttany/modules/onewire/device/ds18x20.py @@ -40,7 +40,6 @@ CONF_OPTIONS = { # will be added to device section of core CONF_OPTIONS CONF_KEY_DS18X20: { "type": "section", - "required": False, CONF_KEY_UNIT: { "default": "C", "selection": ["C", "c", "F", "f"],