catch manual validation

This commit is contained in:
Devaev Maxim
2020-09-09 03:26:15 +03:00
parent 081797b253
commit 9c78f4f631
2 changed files with 17 additions and 3 deletions

View File

@@ -46,6 +46,7 @@ from ..plugins.msd import get_msd_class
from ..plugins.ugpio import get_ugpio_driver_class from ..plugins.ugpio import get_ugpio_driver_class
from ..yamlconf import ConfigError from ..yamlconf import ConfigError
from ..yamlconf import manual_validated
from ..yamlconf import make_config from ..yamlconf import make_config
from ..yamlconf import Section from ..yamlconf import Section
from ..yamlconf import Option from ..yamlconf import Option
@@ -185,6 +186,7 @@ def _patch_dynamic( # pylint: disable=too-many-locals
"__gpio__": {}, "__gpio__": {},
**tools.rget(raw_config, "kvmd", "gpio", "drivers"), **tools.rget(raw_config, "kvmd", "gpio", "drivers"),
}.items(): }.items():
with manual_validated(driver, "kvmd", "gpio", "drivers", "<key>"):
driver = valid_ugpio_driver(driver) driver = valid_ugpio_driver(driver)
driver_type = valid_stripped_string_not_empty(params.get("type", "gpio")) driver_type = valid_stripped_string_not_empty(params.get("type", "gpio"))
scheme["kvmd"]["gpio"]["drivers"][driver] = { scheme["kvmd"]["gpio"]["drivers"][driver] = {
@@ -194,7 +196,9 @@ def _patch_dynamic( # pylint: disable=too-many-locals
drivers.add(driver) drivers.add(driver)
for (channel, params) in tools.rget(raw_config, "kvmd", "gpio", "scheme").items(): for (channel, params) in tools.rget(raw_config, "kvmd", "gpio", "scheme").items():
with manual_validated(channel, "kvmd", "gpio", "scheme", "<key>"):
channel = valid_ugpio_channel(channel) channel = valid_ugpio_channel(channel)
with manual_validated(params.get("mode", ""), "kvmd", "gpio", "scheme", channel, "mode"):
mode = valid_ugpio_mode(params.get("mode", "")) mode = valid_ugpio_mode(params.get("mode", ""))
scheme["kvmd"]["gpio"]["scheme"][channel] = { scheme["kvmd"]["gpio"]["scheme"][channel] = {
"driver": Option("__gpio__", type=(lambda arg: valid_ugpio_driver(arg, drivers))), "driver": Option("__gpio__", type=(lambda arg: valid_ugpio_driver(arg, drivers))),

View File

@@ -20,11 +20,13 @@
# ========================================================================== # # ========================================================================== #
import contextlib
import json import json
from typing import Tuple from typing import Tuple
from typing import List from typing import List
from typing import Dict from typing import Dict
from typing import Generator
from typing import Callable from typing import Callable
from typing import Optional from typing import Optional
from typing import Any from typing import Any
@@ -129,6 +131,14 @@ class Option:
# ===== # =====
@contextlib.contextmanager
def manual_validated(value: Any, *path: str) -> Generator[None, None, None]:
try:
yield
except (TypeError, ValueError) as err:
raise ConfigError(f"Invalid value {value!r} for key {'/'.join(path)!r}: {err}")
def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, ...]=()) -> Section: def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, ...]=()) -> Section:
if not isinstance(raw, dict): if not isinstance(raw, dict):
raise ConfigError(f"The node {('/'.join(_keys) or '/')!r} must be a dictionary") raise ConfigError(f"The node {('/'.join(_keys) or '/')!r} must be a dictionary")