diff options
author | Maxim Devaev <[email protected]> | 2021-07-23 20:52:47 +0300 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2021-07-23 20:52:47 +0300 |
commit | 189ff593792918d3bf95c1d61c66a80fcb67744d (patch) | |
tree | e478216a6f53e0caa2c410002f61d589b3d260c2 /kvmd | |
parent | a8b3a99b97e6b103fda0550f4b50ab029ea2ad35 (diff) |
refactoring
Diffstat (limited to 'kvmd')
-rw-r--r-- | kvmd/apps/__init__.py | 24 | ||||
-rw-r--r-- | kvmd/plugins/msd/otg/__init__.py | 2 | ||||
-rw-r--r-- | kvmd/plugins/ugpio/wol.py | 5 | ||||
-rw-r--r-- | kvmd/yamlconf/__init__.py | 22 |
4 files changed, 31 insertions, 22 deletions
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py index 5b07aae5..be11ae45 100644 --- a/kvmd/apps/__init__.py +++ b/kvmd/apps/__init__.py @@ -331,10 +331,6 @@ def _dump_config(config: Section) -> None: print(dump) -def _make_ifarg(validator, unless): # type: ignore - return (lambda arg: (validator(arg) if arg else unless)) - - def _get_config_scheme() -> Dict: return { "logging": Option({}), @@ -410,10 +406,10 @@ def _get_config_scheme() -> Dict: "shutdown_delay": Option(10.0, type=valid_float_f01), "state_poll": Option(1.0, type=valid_float_f01), - "quality": Option(80, type=_make_ifarg(valid_stream_quality, 0)), + "quality": Option(80, type=valid_stream_quality, if_empty=0), "resolution": { - "default": Option("", type=_make_ifarg(valid_stream_resolution, ""), unpack_as="resolution"), + "default": Option("", type=valid_stream_resolution, if_empty="", unpack_as="resolution"), "available": Option( [], type=functools.partial(valid_string_list, subval=valid_stream_resolution), @@ -428,7 +424,7 @@ def _get_config_scheme() -> Dict: }, "h264_bitrate": { - "default": Option(0, type=_make_ifarg(valid_stream_h264_bitrate, 0), unpack_as="h264_bitrate"), + "default": Option(0, type=valid_stream_h264_bitrate, if_empty=0, unpack_as="h264_bitrate"), "min": Option(100, type=valid_stream_h264_bitrate, unpack_as="h264_bitrate_min"), "max": Option(16000, type=valid_stream_h264_bitrate, unpack_as="h264_bitrate_max"), }, @@ -455,7 +451,7 @@ def _get_config_scheme() -> Dict: "idle_interval": Option(0.0, type=valid_float_f0), "live_interval": Option(0.0, type=valid_float_f0), - "wakeup_key": Option("", type=_make_ifarg(valid_hid_key, "")), + "wakeup_key": Option("", type=valid_hid_key, if_empty=""), "wakeup_move": Option(0, type=valid_hid_mouse_move), "online_delay": Option(5.0, type=valid_float_f0), @@ -509,8 +505,8 @@ def _get_config_scheme() -> Dict: "ethernet": { "enabled": Option(False, type=valid_bool), "driver": Option("ecm", type=valid_otg_ethernet), - "host_mac": Option("", type=_make_ifarg(valid_mac, "")), - "kvm_mac": Option("", type=_make_ifarg(valid_mac, "")), + "host_mac": Option("", type=valid_mac, if_empty=""), + "kvm_mac": Option("", type=valid_mac, if_empty=""), }, "drives": { @@ -597,7 +593,7 @@ def _get_config_scheme() -> Dict: }, "sol": { - "device": Option("", type=_make_ifarg(valid_abs_path, ""), unpack_as="sol_device_path"), + "device": Option("", type=valid_abs_path, if_empty="", unpack_as="sol_device_path"), "speed": Option(115200, type=valid_tty_speed, unpack_as="sol_speed"), "select_timeout": Option(0.1, type=valid_float_f01, unpack_as="sol_select_timeout"), "proxy_port": Option(0, type=valid_port, unpack_as="sol_proxy_port"), @@ -622,11 +618,11 @@ def _get_config_scheme() -> Dict: }, "tls": { - "ciphers": Option("ALL:@SECLEVEL=0", type=_make_ifarg(valid_ssl_ciphers, "")), + "ciphers": Option("ALL:@SECLEVEL=0", type=valid_ssl_ciphers, if_empty=""), "timeout": Option(30.0, type=valid_float_f01), "x509": { - "cert": Option("", type=_make_ifarg(valid_abs_file, "")), - "key": Option("", type=_make_ifarg(valid_abs_file, "")), + "cert": Option("", type=valid_abs_file, if_empty=""), + "key": Option("", type=valid_abs_file, if_empty=""), }, }, }, diff --git a/kvmd/plugins/msd/otg/__init__.py b/kvmd/plugins/msd/otg/__init__.py index bb5129a3..03e06650 100644 --- a/kvmd/plugins/msd/otg/__init__.py +++ b/kvmd/plugins/msd/otg/__init__.py @@ -185,7 +185,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes "unlock_cmd": Option([*sudo, "/usr/bin/kvmd-helper-otgmsd-unlock", "unlock"], type=valid_command), "initial": { - "image": Option("", type=(lambda arg: (valid_printable_filename(arg) if arg else ""))), + "image": Option("", type=valid_printable_filename, if_empty=""), "cdrom": Option(False, type=valid_bool), }, } diff --git a/kvmd/plugins/ugpio/wol.py b/kvmd/plugins/ugpio/wol.py index deeb06c6..02eb9a8c 100644 --- a/kvmd/plugins/ugpio/wol.py +++ b/kvmd/plugins/ugpio/wol.py @@ -21,6 +21,7 @@ import socket +import functools from typing import Dict from typing import Optional @@ -60,9 +61,9 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute @classmethod def get_plugin_options(cls) -> Dict: return { - "ip": Option("255.255.255.255", type=(lambda arg: valid_ip(arg, v6=False))), + "ip": Option("255.255.255.255", type=functools.partial(valid_ip, v6=False)), "port": Option(9, type=valid_port), - "mac": Option("", type=(lambda arg: (valid_mac(arg) if arg else ""))), + "mac": Option("", type=valid_mac, if_empty=""), } def register_input(self, pin: int, debounce: float) -> None: diff --git a/kvmd/yamlconf/__init__.py b/kvmd/yamlconf/__init__.py index c5e7011a..4b339986 100644 --- a/kvmd/yamlconf/__init__.py +++ b/kvmd/yamlconf/__init__.py @@ -108,6 +108,10 @@ class Section(dict): return dict.__getattribute__(self, key) +class Stub: + pass + + class Option: __type = type @@ -115,6 +119,7 @@ class Option: self, default: Any, type: Optional[Callable[[Any], Any]]=None, # pylint: disable=redefined-builtin + if_empty: Any=Stub, only_if: str="", unpack_as: str="", help: str="", # pylint: disable=redefined-builtin @@ -122,12 +127,16 @@ class Option: self.default = default self.type: Callable[[Any], Any] = (type or (self.__type(default) if default is not None else str)) # type: ignore + self.if_empty = if_empty self.only_if = only_if self.unpack_as = unpack_as self.help = help def __repr__(self) -> str: - return f"<Option(default={self.default}, type={self.type}, only_if={self.only_if}, unpack_as={self.unpack_as})>" + return ( + f"<Option(default={self.default}, type={self.type}, if_empty={self.if_empty}," + f" only_if={self.only_if}, unpack_as={self.unpack_as})>" + ) # ===== @@ -170,10 +179,13 @@ def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, . value = option.default else: value = raw.get(key, option.default) - try: - value = option.type(value) - except (TypeError, ValueError) as err: - raise ConfigError(f"Invalid value {value!r} for key {make_full_name(key)!r}: {err}") + if option.if_empty != Stub and not value: + value = option.if_empty + else: + try: + value = option.type(value) + except (TypeError, ValueError) as err: + raise ConfigError(f"Invalid value {value!r} for key {make_full_name(key)!r}: {err}") config[key] = value config._set_meta( # pylint: disable=protected-access |