diff options
Diffstat (limited to 'kvmd/validators')
-rw-r--r-- | kvmd/validators/__init__.py | 11 | ||||
-rw-r--r-- | kvmd/validators/hw.py | 4 | ||||
-rw-r--r-- | kvmd/validators/kvm.py | 7 |
3 files changed, 19 insertions, 3 deletions
diff --git a/kvmd/validators/__init__.py b/kvmd/validators/__init__.py index 9a372002..d6caa835 100644 --- a/kvmd/validators/__init__.py +++ b/kvmd/validators/__init__.py @@ -26,6 +26,7 @@ from typing import List from typing import Mapping from typing import Sequence from typing import Callable +from typing import TypeVar from typing import NoReturn from typing import Union from typing import Any @@ -37,6 +38,10 @@ class ValidatorError(ValueError): # ===== +_RetvalSeqT = TypeVar("_RetvalSeqT", bound=Sequence) + + +# ===== def raise_error(arg: Any, name: str, hide: bool=False) -> NoReturn: arg_str = " " if not hide: @@ -77,6 +82,12 @@ def check_re_match(arg: Any, name: str, pattern: str, strip: bool=True, hide: bo return arg +def check_len(arg: _RetvalSeqT, name: str, limit: int) -> _RetvalSeqT: + if len(arg) > limit: + raise_error(arg, name) + return arg + + def check_any(arg: Any, name: str, validators: List[Callable[[Any], Any]]) -> Any: for validator in validators: try: diff --git a/kvmd/validators/hw.py b/kvmd/validators/hw.py index 82d1221a..59a0363d 100644 --- a/kvmd/validators/hw.py +++ b/kvmd/validators/hw.py @@ -24,6 +24,7 @@ from typing import Any from . import check_in_list from . import check_re_match +from . import check_len from .basic import valid_number @@ -44,7 +45,8 @@ def valid_gpio_pin_optional(arg: Any) -> int: def valid_otg_gadget(arg: Any) -> str: - return check_re_match(arg, "OTG gadget name", r"^[a-z_][a-z0-9_-]*$")[:255] + name = "OTG gadget name" + return check_len(check_re_match(arg, name, r"^[a-z_][a-z0-9_-]*$"), name, 255) def valid_otg_id(arg: Any) -> int: diff --git a/kvmd/validators/kvm.py b/kvmd/validators/kvm.py index fe9a8a8e..17895c47 100644 --- a/kvmd/validators/kvm.py +++ b/kvmd/validators/kvm.py @@ -28,6 +28,7 @@ from ..keyboard.mappings import KEYMAP from . import raise_error from . import check_string_in_list from . import check_re_match +from . import check_len from .basic import valid_stripped_string_not_empty from .basic import valid_number @@ -92,7 +93,8 @@ def valid_hid_mouse_wheel(arg: Any) -> int: # ===== def valid_ugpio_driver(arg: Any) -> str: - return check_re_match(arg, "GPIO driver", r"^[a-zA-Z_][a-zA-Z0-9_-]*$")[:255] + name = "GPIO driver" + return check_len(check_re_match(arg, name, r"^[a-zA-Z_][a-zA-Z0-9_-]*$"), name, 255) def valid_ugpio_mode(arg: Any) -> str: @@ -100,7 +102,8 @@ def valid_ugpio_mode(arg: Any) -> str: def valid_ugpio_channel(arg: Any) -> str: - return check_re_match(arg, "GPIO channel", r"^[a-zA-Z_][a-zA-Z0-9_-]*$")[:255] + name = "GPIO channel" + return check_len(check_re_match(arg, name, r"^[a-zA-Z_][a-zA-Z0-9_-]*$"), name, 255) def valid_ugpio_view_table(arg: Any) -> List[List[str]]: |