summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2020-09-08 12:32:16 +0300
committerDevaev Maxim <[email protected]>2020-09-08 12:32:16 +0300
commitd528a3002e53bb7a5b5e6d9b5385252372738fbf (patch)
tree9746fb7061c82ec101f01151a440e1659799df6e
parentd932a937caede7f54c0f4384ce08f7ecc291af23 (diff)
refactoring
-rw-r--r--kvmd/apps/__init__.py13
-rw-r--r--kvmd/validators/__init__.py11
-rw-r--r--kvmd/validators/kvm.py9
3 files changed, 20 insertions, 13 deletions
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py
index 4afd6b3a..b3197306 100644
--- a/kvmd/apps/__init__.py
+++ b/kvmd/apps/__init__.py
@@ -29,6 +29,7 @@ import logging.config
from typing import Tuple
from typing import List
from typing import Dict
+from typing import Set
from typing import Optional
import pygments
@@ -52,8 +53,6 @@ from ..yamlconf import build_raw_from_options
from ..yamlconf.dumper import make_config_dump
from ..yamlconf.loader import load_yaml_file
-from ..validators import check_string_in_list
-
from ..validators.basic import valid_stripped_string
from ..validators.basic import valid_stripped_string_not_empty
from ..validators.basic import valid_bool
@@ -181,7 +180,7 @@ def _patch_dynamic( # pylint: disable=too-many-locals
rebuild = True
if load_gpio:
- drivers: List[str] = []
+ drivers: Set[str] = set()
for (driver, params) in { # type: ignore
"__gpio__": {},
**tools.rget(raw_config, "kvmd", "gpio", "drivers"),
@@ -192,17 +191,13 @@ def _patch_dynamic( # pylint: disable=too-many-locals
"type": Option(driver_type, type=valid_stripped_string_not_empty),
**get_ugpio_driver_class(driver_type).get_plugin_options()
}
- drivers.append(driver)
+ drivers.add(driver)
for (channel, params) in tools.rget(raw_config, "kvmd", "gpio", "scheme").items():
channel = valid_ugpio_channel(channel)
mode = valid_ugpio_mode(params.get("mode", ""))
scheme["kvmd"]["gpio"]["scheme"][channel] = {
- "driver": Option("__gpio__", type=(lambda arg: check_string_in_list(
- arg=valid_stripped_string_not_empty(arg),
- name="configured GPIO driver",
- variants=drivers,
- ))),
+ "driver": Option("__gpio__", type=(lambda arg: valid_ugpio_driver(arg, drivers))),
"pin": Option(-1, type=valid_gpio_pin),
"mode": Option("", type=valid_ugpio_mode),
"inverted": Option(False, type=valid_bool),
diff --git a/kvmd/validators/__init__.py b/kvmd/validators/__init__.py
index d6caa835..d91b37bf 100644
--- a/kvmd/validators/__init__.py
+++ b/kvmd/validators/__init__.py
@@ -23,6 +23,7 @@
import re
from typing import List
+from typing import Set
from typing import Mapping
from typing import Sequence
from typing import Callable
@@ -62,13 +63,19 @@ def check_not_none_string(arg: Any, name: str, strip: bool=True) -> str:
return arg
-def check_in_list(arg: Any, name: str, variants: Union[Sequence, Mapping]) -> Any:
+def check_in_list(arg: Any, name: str, variants: Union[Sequence, Mapping, Set]) -> Any:
if arg not in variants:
raise_error(arg, name)
return arg
-def check_string_in_list(arg: Any, name: str, variants: Union[Sequence[str], Mapping[str, Any]], lower: bool=True) -> str:
+def check_string_in_list(
+ arg: Any,
+ name: str,
+ variants: Union[Sequence[str], Mapping[str, Any], Set[str]],
+ lower: bool=True,
+) -> str:
+
arg = check_not_none_string(arg, name)
if lower:
arg = arg.lower()
diff --git a/kvmd/validators/kvm.py b/kvmd/validators/kvm.py
index 17895c47..5a4dd6c8 100644
--- a/kvmd/validators/kvm.py
+++ b/kvmd/validators/kvm.py
@@ -21,6 +21,8 @@
from typing import List
+from typing import Set
+from typing import Optional
from typing import Any
from ..keyboard.mappings import KEYMAP
@@ -92,9 +94,12 @@ def valid_hid_mouse_wheel(arg: Any) -> int:
# =====
-def valid_ugpio_driver(arg: Any) -> str:
+def valid_ugpio_driver(arg: Any, variants: Optional[Set[str]]=None) -> str:
name = "GPIO driver"
- return check_len(check_re_match(arg, name, r"^[a-zA-Z_][a-zA-Z0-9_-]*$"), name, 255)
+ arg = check_len(check_re_match(arg, name, r"^[a-zA-Z_][a-zA-Z0-9_-]*$"), name, 255)
+ if variants is not None:
+ arg = check_string_in_list(arg, f"configured {name}", variants)
+ return arg
def valid_ugpio_mode(arg: Any) -> str: