mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 17:20:30 +08:00
refactoring
This commit is contained in:
parent
a8b3a99b97
commit
189ff59379
@ -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=""),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@ -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),
|
||||
},
|
||||
}
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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,6 +179,9 @@ def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, .
|
||||
value = option.default
|
||||
else:
|
||||
value = raw.get(key, option.default)
|
||||
if option.if_empty != Stub and not value:
|
||||
value = option.if_empty
|
||||
else:
|
||||
try:
|
||||
value = option.type(value)
|
||||
except (TypeError, ValueError) as err:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user