summaryrefslogtreecommitdiff
path: root/kvmd/validators
diff options
context:
space:
mode:
authorMaxim Devaev <[email protected]>2024-07-08 03:41:29 +0300
committerMaxim Devaev <[email protected]>2024-12-17 18:20:04 +0200
commit630610bc532299f15ff7ee12d40f617de450aae0 (patch)
treeca0a83f1aa5848a4605034c0394f1edfd0bea7ce /kvmd/validators
parente0bbf6968ef8295274793a564e717f95f42983d7 (diff)
switch
Diffstat (limited to 'kvmd/validators')
-rw-r--r--kvmd/validators/__init__.py8
-rw-r--r--kvmd/validators/os.py10
-rw-r--r--kvmd/validators/switch.py67
3 files changed, 77 insertions, 8 deletions
diff --git a/kvmd/validators/__init__.py b/kvmd/validators/__init__.py
index 39ff60aa..aa997ab9 100644
--- a/kvmd/validators/__init__.py
+++ b/kvmd/validators/__init__.py
@@ -99,3 +99,11 @@ def check_any(arg: Any, name: str, validators: list[Callable[[Any], Any]]) -> An
except Exception:
pass
raise_error(arg, name)
+
+
+# =====
+def filter_printable(arg: str, replace: str, limit: int) -> str:
+ return "".join(
+ (ch if ch.isprintable() else replace)
+ for ch in arg[:limit]
+ )
diff --git a/kvmd/validators/os.py b/kvmd/validators/os.py
index 94d3a40f..b2381d0b 100644
--- a/kvmd/validators/os.py
+++ b/kvmd/validators/os.py
@@ -26,6 +26,7 @@ import stat
from typing import Any
from . import raise_error
+from . import filter_printable
from .basic import valid_number
from .basic import valid_string_list
@@ -75,9 +76,7 @@ def valid_abs_dir(arg: Any, name: str="") -> str:
def valid_printable_filename(arg: Any, name: str="") -> str:
if not name:
name = "printable filename"
-
arg = valid_stripped_string_not_empty(arg, name)
-
if (
"/" in arg
or "\0" in arg
@@ -85,12 +84,7 @@ def valid_printable_filename(arg: Any, name: str="") -> str:
or arg == "lost+found"
):
raise_error(arg, name)
-
- arg = "".join(
- (ch if ch.isprintable() else "_")
- for ch in arg[:255]
- )
- return arg
+ return filter_printable(arg, "_", 255)
# =====
diff --git a/kvmd/validators/switch.py b/kvmd/validators/switch.py
new file mode 100644
index 00000000..d4f3ab2f
--- /dev/null
+++ b/kvmd/validators/switch.py
@@ -0,0 +1,67 @@
+# ========================================================================== #
+# #
+# KVMD - The main PiKVM daemon. #
+# #
+# Copyright (C) 2018-2024 Maxim Devaev <[email protected]> #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <https://www.gnu.org/licenses/>. #
+# #
+# ========================================================================== #
+
+
+import re
+
+from typing import Any
+
+from . import filter_printable
+from . import check_re_match
+
+from .basic import valid_stripped_string
+from .basic import valid_number
+
+
+# =====
+def valid_switch_port_name(arg: Any) -> str:
+ arg = valid_stripped_string(arg, name="switch port name")
+ arg = filter_printable(arg, " ", 255)
+ arg = re.sub(r"\s+", " ", arg)
+ return arg.strip()
+
+
+def valid_switch_edid_id(arg: Any, allow_default: bool) -> str:
+ pattern = "(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
+ if allow_default:
+ pattern += "|^default$"
+ return check_re_match(arg, "switch EDID ID", pattern).lower()
+
+
+def valid_switch_edid_data(arg: Any) -> str:
+ name = "switch EDID data"
+ arg = valid_stripped_string(arg, name=name)
+ arg = re.sub(r"\s", "", arg)
+ return check_re_match(arg, name, "(?i)^[0-9a-f]{512}$").upper()
+
+
+def valid_switch_color(arg: Any, allow_default: bool) -> str:
+ pattern = "(?i)^[0-9a-f]{6}:[0-9a-f]{2}:[0-9a-f]{4}$"
+ if allow_default:
+ pattern += "|^default$"
+ arg = check_re_match(arg, "switch color", pattern).upper()
+ if arg == "DEFAULT":
+ arg = "default"
+ return arg
+
+
+def valid_switch_atx_click_delay(arg: Any) -> float:
+ return valid_number(arg, min=0, max=10, type=float, name="ATX delay")