feat: can get params value easily

This commit is contained in:
egarette@silique.fr 2025-02-10 08:45:21 +01:00
parent 90947b5578
commit 6e4b22aea2
7 changed files with 61 additions and 49 deletions

View file

@ -55,12 +55,17 @@ class DomainnameOption(StrOption):
allow_without_dot: bool = False, allow_without_dot: bool = False,
allow_startswith_dot: bool = False, allow_startswith_dot: bool = False,
test_existence: bool = False, test_existence: bool = False,
_extra: dict = None,
**kwargs, **kwargs,
) -> None: ) -> None:
# pylint: disable=too-many-branches,too-many-locals,too-many-arguments # pylint: disable=too-many-branches,too-many-locals,too-many-arguments
if _extra is None:
extra = {}
else:
extra = _extra
if type not in ["netbios", "hostname", "domainname"]: if type not in ["netbios", "hostname", "domainname"]:
raise ValueError(_("unknown type {0} for hostname").format(type)) raise ValueError(_("unknown type {0} for hostname").format(type))
extra = {"_dom_type": type} extra["type"] = type
if not isinstance(allow_ip, bool): if not isinstance(allow_ip, bool):
raise ValueError(_("allow_ip must be a boolean")) raise ValueError(_("allow_ip must be a boolean"))
if not isinstance(allow_cidr_network, bool): if not isinstance(allow_cidr_network, bool):
@ -69,8 +74,8 @@ class DomainnameOption(StrOption):
raise ValueError(_("allow_without_dot must be a boolean")) raise ValueError(_("allow_without_dot must be a boolean"))
if not isinstance(allow_startswith_dot, bool): if not isinstance(allow_startswith_dot, bool):
raise ValueError(_("allow_startswith_dot must be a boolean")) raise ValueError(_("allow_startswith_dot must be a boolean"))
extra["_allow_without_dot"] = allow_without_dot extra["allow_without_dot"] = allow_without_dot
extra["_test_existence"] = test_existence extra["test_existence"] = test_existence
if type == "domainname": if type == "domainname":
if allow_without_dot: if allow_without_dot:
min_time = 0 min_time = 0
@ -114,15 +119,15 @@ class DomainnameOption(StrOption):
name, name,
doc, doc,
) )
extra["_allow_ip"] = allow_ip extra["allow_ip"] = allow_ip
if allow_cidr_network: if allow_cidr_network:
extra["_network"] = NetworkOption( extra["_network"] = NetworkOption(
name, name,
doc, doc,
cidr=True, cidr=True,
) )
extra["_allow_cidr_network"] = allow_cidr_network extra["allow_cidr_network"] = allow_cidr_network
extra["_allow_startswith_dot"] = allow_startswith_dot extra["allow_startswith_dot"] = allow_startswith_dot
super().__init__( super().__init__(
name, name,
@ -146,13 +151,13 @@ class DomainnameOption(StrOption):
_("invalid length (max {0})" "").format(part_name_length) _("invalid length (max {0})" "").format(part_name_length)
) )
part_name_length = self._get_len(self.impl_get_extra("_dom_type")) part_name_length = self._get_len(self.impl_get_extra("type"))
if self.impl_get_extra("_dom_type") == "domainname": if self.impl_get_extra("type") == "domainname":
if not self.impl_get_extra("_allow_without_dot") and not "." in value: if not self.impl_get_extra("allow_without_dot") and not "." in value:
raise ValueError(_("must have dot")) raise ValueError(_("must have dot"))
if len(value) > 255: if len(value) > 255:
raise ValueError(_("invalid length (max 255)")) raise ValueError(_("invalid length (max 255)"))
if self.impl_get_extra("_allow_startswith_dot") and value.startswith("."): if self.impl_get_extra("allow_startswith_dot") and value.startswith("."):
val = value[1:] val = value[1:]
else: else:
val = value val = value
@ -167,7 +172,7 @@ class DomainnameOption(StrOption):
self._validate_domain_resolution(value) self._validate_domain_resolution(value)
def _validate_domain_resolution(self, value: str) -> None: def _validate_domain_resolution(self, value: str) -> None:
if not value.startswith(".") and self.impl_get_extra("_test_existence") is True: if not value.startswith(".") and self.impl_get_extra("test_existence") is True:
try: try:
socket.gethostbyname(value) socket.gethostbyname(value)
except socket.gaierror as err: except socket.gaierror as err:
@ -180,8 +185,8 @@ class DomainnameOption(StrOption):
) from err ) from err
def _validate_ip_network(self, value: str) -> None: def _validate_ip_network(self, value: str) -> None:
allow_ip = self.impl_get_extra("_allow_ip") allow_ip = self.impl_get_extra("allow_ip")
allow_cidr_network = self.impl_get_extra("_allow_cidr_network") allow_cidr_network = self.impl_get_extra("allow_cidr_network")
if allow_ip is False and allow_cidr_network is False: if allow_ip is False and allow_cidr_network is False:
raise ValueError(_("must not be an IP")) raise ValueError(_("must not be an IP"))
if allow_ip is True: if allow_ip is True:
@ -207,7 +212,7 @@ class DomainnameOption(StrOption):
def _second_level_validation_domain(self, value: str, warnings_only: bool) -> None: def _second_level_validation_domain(self, value: str, warnings_only: bool) -> None:
if self.impl_get_extra("_has_upper").search(value): if self.impl_get_extra("_has_upper").search(value):
raise ValueError(_("some characters are uppercase")) raise ValueError(_("some characters are uppercase"))
if self.impl_get_extra("_allow_startswith_dot") and value.startswith("."): if self.impl_get_extra("allow_startswith_dot") and value.startswith("."):
val = value[1:] val = value[1:]
else: else:
val = value val = value
@ -223,8 +228,8 @@ class DomainnameOption(StrOption):
def _second_level_validation_ip_network( def _second_level_validation_ip_network(
self, value: str, warnings_only: bool self, value: str, warnings_only: bool
) -> None: ) -> None:
allow_ip = self.impl_get_extra("_allow_ip") allow_ip = self.impl_get_extra("allow_ip")
allow_cidr_network = self.impl_get_extra("_allow_cidr_network") allow_cidr_network = self.impl_get_extra("allow_cidr_network")
# it's an IP so validate with IPOption # it's an IP so validate with IPOption
if allow_ip is True and allow_cidr_network is False: if allow_ip is True and allow_cidr_network is False:
try: try:

View file

@ -52,9 +52,9 @@ class FilenameOption(StrOption):
if typ not in ["file", "directory"]: if typ not in ["file", "directory"]:
raise ValueError(f'unknown type "{typ}" for "{name}"') raise ValueError(f'unknown type "{typ}" for "{name}"')
extra = { extra = {
"_allow_relative": allow_relative, "allow_relative": allow_relative,
"_test_existence": test_existence, "test_existence": test_existence,
"_types": types, "types": types,
} }
super().__init__(name, *args, extra=extra, **kwargs) super().__init__(name, *args, extra=extra, **kwargs)
@ -63,10 +63,10 @@ class FilenameOption(StrOption):
value: str, value: str,
) -> None: ) -> None:
super().validate(value) super().validate(value)
if not self.impl_get_extra("_allow_relative") and not value.startswith("/"): if not self.impl_get_extra("allow_relative") and not value.startswith("/"):
raise ValueError(_('must starts with "/"')) raise ValueError(_('must starts with "/"'))
if value is not None and self.impl_get_extra("_test_existence"): if value is not None and self.impl_get_extra("test_existence"):
types = self.impl_get_extra("_types") types = self.impl_get_extra("types")
file = Path(value) file = Path(value)
found = False found = False
if "file" in types and file.is_file(): if "file" in types and file.is_file():

View file

@ -43,9 +43,9 @@ class IPOption(StrOption):
): ):
if extra is None: if extra is None:
extra = {} extra = {}
extra["_private_only"] = private_only extra["private_only"] = private_only
extra["_allow_reserved"] = allow_reserved extra["allow_reserved"] = allow_reserved
extra["_cidr"] = cidr extra["cidr"] = cidr
super().__init__(*args, extra=extra, **kwargs) super().__init__(*args, extra=extra, **kwargs)
def _validate_cidr(self, value): def _validate_cidr(self, value):
@ -66,7 +66,7 @@ class IPOption(StrOption):
def validate(self, value: str) -> None: def validate(self, value: str) -> None:
super().validate(value) super().validate(value)
if self.impl_get_extra("_cidr"): if self.impl_get_extra("cidr"):
if "/" not in value: if "/" not in value:
raise ValueError(_('CIDR address must have a "/"')) raise ValueError(_('CIDR address must have a "/"'))
self._validate_cidr(value) self._validate_cidr(value)
@ -75,13 +75,13 @@ class IPOption(StrOption):
def second_level_validation(self, value: str, warnings_only: bool) -> None: def second_level_validation(self, value: str, warnings_only: bool) -> None:
ip_obj = ip_interface(value) ip_obj = ip_interface(value)
if not self.impl_get_extra("_allow_reserved") and ip_obj.is_reserved: if not self.impl_get_extra("allow_reserved") and ip_obj.is_reserved:
if warnings_only: if warnings_only:
msg = _("shouldn't be reserved IP") msg = _("shouldn't be reserved IP")
else: else:
msg = _("mustn't be reserved IP") msg = _("mustn't be reserved IP")
raise ValueError(msg) raise ValueError(msg)
if self.impl_get_extra("_private_only") and not ip_obj.is_private: if self.impl_get_extra("private_only") and not ip_obj.is_private:
if warnings_only: if warnings_only:
msg = _("should be private IP") msg = _("should be private IP")
else: else:

View file

@ -32,14 +32,14 @@ class NetworkOption(StrOption):
_type = "network address" _type = "network address"
def __init__(self, *args, cidr=False, **kwargs): def __init__(self, *args, cidr=False, **kwargs):
extra = {"_cidr": cidr} extra = {"cidr": cidr}
super().__init__(*args, extra=extra, **kwargs) super().__init__(*args, extra=extra, **kwargs)
def validate(self, value: str) -> None: def validate(self, value: str) -> None:
super().validate(value) super().validate(value)
if value.count(".") != 3: if value.count(".") != 3:
raise ValueError() raise ValueError()
cidr = self.impl_get_extra("_cidr") cidr = self.impl_get_extra("cidr")
if cidr: if cidr:
if "/" not in value: if "/" not in value:
raise ValueError(_("must use CIDR notation")) raise ValueError(_("must use CIDR notation"))

View file

@ -50,15 +50,21 @@ class PortOption(StrOption):
allow_registred: bool = True, allow_registred: bool = True,
allow_protocol: bool = False, allow_protocol: bool = False,
allow_private: bool = False, allow_private: bool = False,
_extra: dict = None,
**kwargs, **kwargs,
) -> None: ) -> None:
if _extra is None:
extra = { extra = {}
"_allow_range": allow_range, else:
"_allow_protocol": allow_protocol, extra = _extra
"_min_value": None, extra["allow_range"] = allow_range
"_max_value": None, extra["allow_protocol"] = allow_protocol
} extra["allow_zero"] = allow_zero
extra["allow_wellknown"] = allow_wellknown
extra["allow_registred"] = allow_registred
extra["allow_private"] = allow_private
extra["_min_value"] = None
extra["_max_value"] = None
ports_min = [0, 1, 1024, 49152] ports_min = [0, 1, 1024, 49152]
ports_max = [0, 1023, 49151, 65535] ports_max = [0, 1023, 49151, 65535]
is_finally = False is_finally = False
@ -82,11 +88,11 @@ class PortOption(StrOption):
def validate(self, value: str) -> None: def validate(self, value: str) -> None:
super().validate(value) super().validate(value)
if self.impl_get_extra("_allow_protocol") and ( if self.impl_get_extra("allow_protocol") and (
value.startswith("tcp:") or value.startswith("udp:") value.startswith("tcp:") or value.startswith("udp:")
): ):
value = [value[4:]] value = [value[4:]]
elif self.impl_get_extra("_allow_range") and ":" in str(value): elif self.impl_get_extra("allow_range") and ":" in str(value):
value = value.split(":") value = value.split(":")
if len(value) != 2: if len(value) != 2:
raise ValueError(_("range must have two values only")) raise ValueError(_("range must have two values only"))
@ -102,7 +108,7 @@ class PortOption(StrOption):
raise ValueError() raise ValueError()
def second_level_validation(self, value: str, warnings_only: bool) -> None: def second_level_validation(self, value: str, warnings_only: bool) -> None:
if self.impl_get_extra("_allow_protocol") and ( if self.impl_get_extra("allow_protocol") and (
value.startswith("tcp:") or value.startswith("udp:") value.startswith("tcp:") or value.startswith("udp:")
): ):
value = [value[4:]] value = [value[4:]]

View file

@ -54,15 +54,16 @@ class URLOption(StrOption):
**kwargs, **kwargs,
) -> None: ) -> None:
# pylint: disable=too-many-arguments,too-many-locals,redefined-builtin # pylint: disable=too-many-arguments,too-many-locals,redefined-builtin
extra = { extra = {}
"_domainname": DomainnameOption( extra["_domainname"] = DomainnameOption(
name, name,
doc, doc,
allow_ip=allow_ip, allow_ip=allow_ip,
type=type, type=type,
allow_without_dot=allow_without_dot, allow_without_dot=allow_without_dot,
), _extra=extra,
"_port": PortOption( )
extra["_port"] = PortOption(
name, name,
doc, doc,
allow_range=allow_range, allow_range=allow_range,
@ -70,8 +71,8 @@ class URLOption(StrOption):
allow_wellknown=allow_wellknown, allow_wellknown=allow_wellknown,
allow_registred=allow_registred, allow_registred=allow_registred,
allow_private=allow_private, allow_private=allow_private,
), _extra=extra,
} )
super().__init__( super().__init__(
name, name,
doc, doc,

View file

@ -646,9 +646,9 @@ class TiramisuDict:
if self.remotable == "all" or childapi.has_dependency(): if self.remotable == "all" or childapi.has_dependency():
obj_form["remote"] = True obj_form["remote"] = True
if childtype == "IPOption" and ( if childtype == "IPOption" and (
child.impl_get_extra("_private_only") child.impl_get_extra("private_only")
or not child.impl_get_extra("_allow_reserved") or not child.impl_get_extra("allow_reserved")
or child.impl_get_extra("_cidr") or child.impl_get_extra("cidr")
): ):
obj_form["remote"] = True obj_form["remote"] = True
if childtype == "DateOption": if childtype == "DateOption":