mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 09:10:30 +08:00
improved validation
This commit is contained in:
parent
c124e63d6d
commit
b4ef91838e
@ -49,6 +49,8 @@ from ..yamlconf import build_raw_from_options
|
|||||||
from ..yamlconf.dumper import make_config_dump
|
from ..yamlconf.dumper import make_config_dump
|
||||||
from ..yamlconf.loader import load_yaml_file
|
from ..yamlconf.loader import load_yaml_file
|
||||||
|
|
||||||
|
from ..validators.basic import valid_stripped_string
|
||||||
|
from ..validators.basic import valid_stripped_string_not_empty
|
||||||
from ..validators.basic import valid_bool
|
from ..validators.basic import valid_bool
|
||||||
from ..validators.basic import valid_number
|
from ..validators.basic import valid_number
|
||||||
from ..validators.basic import valid_float_f0
|
from ..validators.basic import valid_float_f0
|
||||||
@ -201,7 +203,7 @@ def _get_config_scheme() -> Dict:
|
|||||||
},
|
},
|
||||||
|
|
||||||
"external": {
|
"external": {
|
||||||
"type": Option("", type=(lambda arg: str(arg).strip())),
|
"type": Option("", type=valid_stripped_string),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -211,17 +213,17 @@ def _get_config_scheme() -> Dict:
|
|||||||
},
|
},
|
||||||
|
|
||||||
"hid": {
|
"hid": {
|
||||||
"type": Option("", type=(lambda arg: str(arg).strip())),
|
"type": Option("", type=valid_stripped_string_not_empty),
|
||||||
# Dynamic content
|
# Dynamic content
|
||||||
},
|
},
|
||||||
|
|
||||||
"atx": {
|
"atx": {
|
||||||
"type": Option("", type=(lambda arg: str(arg).strip())),
|
"type": Option("", type=valid_stripped_string_not_empty),
|
||||||
# Dynamic content
|
# Dynamic content
|
||||||
},
|
},
|
||||||
|
|
||||||
"msd": {
|
"msd": {
|
||||||
"type": Option("", type=(lambda arg: str(arg).strip())),
|
"type": Option("", type=valid_stripped_string_not_empty),
|
||||||
# Dynamic content
|
# Dynamic content
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -258,7 +260,7 @@ def _get_config_scheme() -> Dict:
|
|||||||
"serial": Option("CAFEBABE"),
|
"serial": Option("CAFEBABE"),
|
||||||
|
|
||||||
"gadget": Option("kvmd", type=valid_otg_gadget),
|
"gadget": Option("kvmd", type=valid_otg_gadget),
|
||||||
"udc": Option("", type=(lambda arg: str(arg).strip())),
|
"udc": Option("", type=valid_stripped_string),
|
||||||
"init_delay": Option(3.0, type=valid_float_f01),
|
"init_delay": Option(3.0, type=valid_float_f01),
|
||||||
|
|
||||||
"msd": {
|
"msd": {
|
||||||
|
|||||||
@ -36,13 +36,27 @@ from . import check_in_list
|
|||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
def valid_stripped_string(arg: Any, name: str="") -> str:
|
||||||
|
if not name:
|
||||||
|
name = "stripped string"
|
||||||
|
return check_not_none_string(arg, name)
|
||||||
|
|
||||||
|
|
||||||
|
def valid_stripped_string_not_empty(arg: Any, name: str="") -> str:
|
||||||
|
if not name:
|
||||||
|
name = "not empty stripped string"
|
||||||
|
if len(str(arg).strip()) == 0:
|
||||||
|
arg = None
|
||||||
|
return valid_stripped_string(arg, name)
|
||||||
|
|
||||||
|
|
||||||
def valid_bool(arg: Any) -> bool:
|
def valid_bool(arg: Any) -> bool:
|
||||||
true_args = ["1", "true", "yes"]
|
true_args = ["1", "true", "yes"]
|
||||||
false_args = ["0", "false", "no"]
|
false_args = ["0", "false", "no"]
|
||||||
|
|
||||||
name = f"bool ({true_args!r} or {false_args!r})"
|
name = f"bool ({true_args!r} or {false_args!r})"
|
||||||
|
|
||||||
arg = check_not_none_string(arg, name).lower()
|
arg = valid_stripped_string_not_empty(arg, name).lower()
|
||||||
arg = check_in_list(arg, name, true_args + false_args)
|
arg = check_in_list(arg, name, true_args + false_args)
|
||||||
return (arg in true_args)
|
return (arg in true_args)
|
||||||
|
|
||||||
@ -57,7 +71,7 @@ def valid_number(
|
|||||||
|
|
||||||
name = (name or type.__name__)
|
name = (name or type.__name__)
|
||||||
|
|
||||||
arg = check_not_none_string(arg, name)
|
arg = valid_stripped_string_not_empty(arg, name)
|
||||||
try:
|
try:
|
||||||
arg = type(arg)
|
arg = type(arg)
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@ -24,18 +24,18 @@ import socket
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from . import check_not_none_string
|
|
||||||
from . import check_re_match
|
from . import check_re_match
|
||||||
from . import check_any
|
from . import check_any
|
||||||
|
|
||||||
from .basic import valid_number
|
from .basic import valid_number
|
||||||
|
from .basic import valid_stripped_string_not_empty
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
def valid_ip_or_host(arg: Any) -> str:
|
def valid_ip_or_host(arg: Any) -> str:
|
||||||
name = "IP address or RFC-1123 hostname"
|
name = "IP address or RFC-1123 hostname"
|
||||||
return check_any(
|
return check_any(
|
||||||
arg=check_not_none_string(arg, name),
|
arg=valid_stripped_string_not_empty(arg, name),
|
||||||
name=name,
|
name=name,
|
||||||
validators=[
|
validators=[
|
||||||
valid_ip,
|
valid_ip,
|
||||||
@ -47,7 +47,7 @@ def valid_ip_or_host(arg: Any) -> str:
|
|||||||
def valid_ip(arg: Any) -> str:
|
def valid_ip(arg: Any) -> str:
|
||||||
name = "IP address"
|
name = "IP address"
|
||||||
return check_any(
|
return check_any(
|
||||||
arg=check_not_none_string(arg, name),
|
arg=valid_stripped_string_not_empty(arg, name),
|
||||||
name=name,
|
name=name,
|
||||||
validators=[
|
validators=[
|
||||||
lambda arg: (arg, socket.inet_pton(socket.AF_INET, arg))[0],
|
lambda arg: (arg, socket.inet_pton(socket.AF_INET, arg))[0],
|
||||||
|
|||||||
@ -27,10 +27,10 @@ from typing import List
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from . import raise_error
|
from . import raise_error
|
||||||
from . import check_not_none_string
|
|
||||||
|
|
||||||
from .basic import valid_number
|
from .basic import valid_number
|
||||||
from .basic import valid_string_list
|
from .basic import valid_string_list
|
||||||
|
from .basic import valid_stripped_string_not_empty
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
@ -51,9 +51,7 @@ def valid_abs_path(arg: Any, type: str="", name: str="") -> str: # pylint: disa
|
|||||||
if not name:
|
if not name:
|
||||||
name = "absolute path"
|
name = "absolute path"
|
||||||
|
|
||||||
if len(str(arg).strip()) == 0:
|
arg = os.path.abspath(valid_stripped_string_not_empty(arg, name))
|
||||||
arg = None
|
|
||||||
arg = os.path.abspath(check_not_none_string(arg, name))
|
|
||||||
|
|
||||||
if type:
|
if type:
|
||||||
try:
|
try:
|
||||||
@ -79,9 +77,7 @@ def valid_printable_filename(arg: Any, name: str="") -> str:
|
|||||||
if not name:
|
if not name:
|
||||||
name = "printable filename"
|
name = "printable filename"
|
||||||
|
|
||||||
if len(str(arg).strip()) == 0:
|
arg = valid_stripped_string_not_empty(arg, name)
|
||||||
arg = None
|
|
||||||
arg = check_not_none_string(arg, name)
|
|
||||||
|
|
||||||
if "/" in arg or "\0" in arg or arg in [".", ".."]:
|
if "/" in arg or "\0" in arg or arg in [".", ".."]:
|
||||||
raise_error(arg, name)
|
raise_error(arg, name)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user