summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-10-24 03:09:06 +0300
committerDevaev Maxim <[email protected]>2019-10-24 03:09:06 +0300
commitb4ef91838e16cdb70ae1152c320a3973f07c5f89 (patch)
treea98b222952940dedb07cdb1ebe89025af3c50e46
parentc124e63d6d6cdd07b6d0fa4439fed878ed164fbb (diff)
improved validation
-rw-r--r--kvmd/apps/__init__.py12
-rw-r--r--kvmd/validators/basic.py18
-rw-r--r--kvmd/validators/net.py6
-rw-r--r--kvmd/validators/os.py10
4 files changed, 29 insertions, 17 deletions
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py
index 0906c016..1654ec4c 100644
--- a/kvmd/apps/__init__.py
+++ b/kvmd/apps/__init__.py
@@ -49,6 +49,8 @@ from ..yamlconf import build_raw_from_options
from ..yamlconf.dumper import make_config_dump
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_number
from ..validators.basic import valid_float_f0
@@ -201,7 +203,7 @@ def _get_config_scheme() -> Dict:
},
"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": {
- "type": Option("", type=(lambda arg: str(arg).strip())),
+ "type": Option("", type=valid_stripped_string_not_empty),
# Dynamic content
},
"atx": {
- "type": Option("", type=(lambda arg: str(arg).strip())),
+ "type": Option("", type=valid_stripped_string_not_empty),
# Dynamic content
},
"msd": {
- "type": Option("", type=(lambda arg: str(arg).strip())),
+ "type": Option("", type=valid_stripped_string_not_empty),
# Dynamic content
},
@@ -258,7 +260,7 @@ def _get_config_scheme() -> Dict:
"serial": Option("CAFEBABE"),
"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),
"msd": {
diff --git a/kvmd/validators/basic.py b/kvmd/validators/basic.py
index 0328490d..24387be4 100644
--- a/kvmd/validators/basic.py
+++ b/kvmd/validators/basic.py
@@ -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:
true_args = ["1", "true", "yes"]
false_args = ["0", "false", "no"]
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)
return (arg in true_args)
@@ -57,7 +71,7 @@ def valid_number(
name = (name or type.__name__)
- arg = check_not_none_string(arg, name)
+ arg = valid_stripped_string_not_empty(arg, name)
try:
arg = type(arg)
except Exception:
diff --git a/kvmd/validators/net.py b/kvmd/validators/net.py
index cad87f07..ce875242 100644
--- a/kvmd/validators/net.py
+++ b/kvmd/validators/net.py
@@ -24,18 +24,18 @@ import socket
from typing import Any
-from . import check_not_none_string
from . import check_re_match
from . import check_any
from .basic import valid_number
+from .basic import valid_stripped_string_not_empty
# =====
def valid_ip_or_host(arg: Any) -> str:
name = "IP address or RFC-1123 hostname"
return check_any(
- arg=check_not_none_string(arg, name),
+ arg=valid_stripped_string_not_empty(arg, name),
name=name,
validators=[
valid_ip,
@@ -47,7 +47,7 @@ def valid_ip_or_host(arg: Any) -> str:
def valid_ip(arg: Any) -> str:
name = "IP address"
return check_any(
- arg=check_not_none_string(arg, name),
+ arg=valid_stripped_string_not_empty(arg, name),
name=name,
validators=[
lambda arg: (arg, socket.inet_pton(socket.AF_INET, arg))[0],
diff --git a/kvmd/validators/os.py b/kvmd/validators/os.py
index 3fcca5c1..ec8d47fa 100644
--- a/kvmd/validators/os.py
+++ b/kvmd/validators/os.py
@@ -27,10 +27,10 @@ from typing import List
from typing import Any
from . import raise_error
-from . import check_not_none_string
from .basic import valid_number
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:
name = "absolute path"
- if len(str(arg).strip()) == 0:
- arg = None
- arg = os.path.abspath(check_not_none_string(arg, name))
+ arg = os.path.abspath(valid_stripped_string_not_empty(arg, name))
if type:
try:
@@ -79,9 +77,7 @@ def valid_printable_filename(arg: Any, name: str="") -> str:
if not name:
name = "printable filename"
- if len(str(arg).strip()) == 0:
- arg = None
- arg = check_not_none_string(arg, name)
+ arg = valid_stripped_string_not_empty(arg, name)
if "/" in arg or "\0" in arg or arg in [".", ".."]:
raise_error(arg, name)