improved pins validation

This commit is contained in:
Maxim Devaev 2021-09-08 06:08:11 +03:00
parent 5d1228eb9e
commit ca812117e4
10 changed files with 20 additions and 16 deletions

View File

@ -77,7 +77,7 @@ class _GpioInput:
) -> None: ) -> None:
self.__channel = channel self.__channel = channel
self.__pin: str = config.pin self.__pin: str = str(config.pin)
self.__inverted: bool = config.inverted self.__inverted: bool = config.inverted
self.__driver = driver self.__driver = driver
@ -118,7 +118,7 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
) -> None: ) -> None:
self.__channel = channel self.__channel = channel
self.__pin: str = config.pin self.__pin: str = str(config.pin)
self.__inverted: bool = config.inverted self.__inverted: bool = config.inverted
self.__switch: bool = config.switch self.__switch: bool = config.switch

View File

@ -75,7 +75,9 @@ class BaseUserGpioDriver(BasePlugin):
return set(UserGpioModes.ALL) return set(UserGpioModes.ALL)
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
# XXX: The returned value will be forcibly converted to a string
# in kvmd/apps/kvmd/ugpio.py, i.e. AFTER validation.
raise NotImplementedError raise NotImplementedError
def register_input(self, pin: str, debounce: float) -> None: def register_input(self, pin: str, debounce: float) -> None:

View File

@ -88,8 +88,8 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
} }
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
return (lambda arg: str(valid_number(arg, min=0, max=3, name="Ezcoo channel"))) return functools.partial(valid_number, min=0, max=3, name="Ezcoo channel")
def prepare(self) -> None: def prepare(self) -> None:
assert self.__proc is None assert self.__proc is None

View File

@ -67,8 +67,8 @@ class Plugin(BaseUserGpioDriver):
} }
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
return (lambda arg: str(valid_gpio_pin(arg))) return valid_gpio_pin
def register_input(self, pin: str, debounce: float) -> None: def register_input(self, pin: str, debounce: float) -> None:
self.__input_pins[int(pin)] = aiogp.AioReaderPinParams(False, debounce) self.__input_pins[int(pin)] = aiogp.AioReaderPinParams(False, debounce)

View File

@ -22,6 +22,7 @@
import asyncio import asyncio
import contextlib import contextlib
import functools
from typing import Dict from typing import Dict
from typing import Set from typing import Set
@ -83,8 +84,8 @@ class Plugin(BaseUserGpioDriver):
return set([UserGpioModes.OUTPUT]) return set([UserGpioModes.OUTPUT])
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
return (lambda arg: str(valid_number(arg, min=0, max=7, name="HID relay channel"))) return functools.partial(valid_number, min=0, max=7, name="HID relay channel")
def register_output(self, pin: str, initial: Optional[bool]) -> None: def register_output(self, pin: str, initial: Optional[bool]) -> None:
self.__initials[int(pin)] = initial self.__initials[int(pin)] = initial

View File

@ -112,7 +112,7 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
} }
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
actions = ["0", *_OUTPUTS, "status", *_OUTPUTS.values()] actions = ["0", *_OUTPUTS, "status", *_OUTPUTS.values()]
return (lambda arg: check_string_in_list(arg, "IPMI action", actions)) return (lambda arg: check_string_in_list(arg, "IPMI action", actions))

View File

@ -54,7 +54,7 @@ class Plugin(BaseUserGpioDriver):
self.__driver = "" self.__driver = ""
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
return str return str
def prepare(self) -> None: def prepare(self) -> None:

View File

@ -81,8 +81,8 @@ class Plugin(BaseUserGpioDriver):
return set([UserGpioModes.OUTPUT]) return set([UserGpioModes.OUTPUT])
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
return (lambda arg: str(valid_gpio_pin(arg))) return valid_gpio_pin
def register_output(self, pin: str, initial: Optional[bool]) -> None: def register_output(self, pin: str, initial: Optional[bool]) -> None:
self.__channels[int(pin)] = initial self.__channels[int(pin)] = initial

View File

@ -21,6 +21,7 @@
import asyncio import asyncio
import functools
from typing import Tuple from typing import Tuple
from typing import Dict from typing import Dict
@ -83,8 +84,8 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
} }
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
return (lambda arg: str(valid_number(arg, min=0, max=15, name="Tesmart channel"))) return functools.partial(valid_number, min=0, max=15, name="Tesmart channel")
async def run(self) -> None: async def run(self) -> None:
prev_active = -2 prev_active = -2

View File

@ -69,7 +69,7 @@ class Plugin(BaseUserGpioDriver): # pylint: disable=too-many-instance-attribute
} }
@classmethod @classmethod
def get_pin_validator(cls) -> Callable[[Any], str]: def get_pin_validator(cls) -> Callable[[Any], Any]:
return str return str
async def read(self, pin: str) -> bool: async def read(self, pin: str) -> bool: