This commit is contained in:
Maxim Devaev
2024-07-08 03:41:29 +03:00
parent e0bbf6968e
commit 630610bc53
50 changed files with 3835 additions and 77 deletions

View File

@@ -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]
)

View File

@@ -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)
# =====

67
kvmd/validators/switch.py Normal file
View File

@@ -0,0 +1,67 @@
# ========================================================================== #
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2024 Maxim Devaev <mdevaev@gmail.com> #
# #
# 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")