From 6b77b0c5ade27032924f349f464c938f0b43e02e Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 19 Nov 2019 18:48:13 +0100 Subject: [PATCH] DomainnameOption add network support --- tiramisu/option/domainnameoption.py | 35 ++++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/tiramisu/option/domainnameoption.py b/tiramisu/option/domainnameoption.py index c7b7e47..9e2cfa4 100644 --- a/tiramisu/option/domainnameoption.py +++ b/tiramisu/option/domainnameoption.py @@ -51,8 +51,7 @@ class DomainnameOption(StrOption): properties: Optional[List[str]]=None, warnings_only: bool=False, allow_ip: bool=False, - allow_network: bool=False, - network_cidr: bool=False, + allow_cidr_network: bool=False, type: str='domainname', allow_without_dot: bool=False, allow_startswith_dot: bool=False) -> None: @@ -62,8 +61,8 @@ class DomainnameOption(StrOption): extra = {'_dom_type': type} if not isinstance(allow_ip, bool): raise ValueError(_('allow_ip must be a boolean')) - if not isinstance(allow_network, bool): - raise ValueError(_('allow_network must be a boolean')) + if not isinstance(allow_cidr_network, bool): + raise ValueError(_('allow_cidr_network must be a boolean')) if not isinstance(allow_without_dot, bool): raise ValueError(_('allow_without_dot must be a boolean')) if not isinstance(allow_startswith_dot, bool): @@ -93,11 +92,11 @@ class DomainnameOption(StrOption): extra['_ip'] = IPOption(name, doc) extra['_allow_ip'] = allow_ip - if allow_network: + if allow_cidr_network: extra['_network'] = NetworkOption(name, doc, - cidr=network_cidr) - extra['_allow_network'] = allow_network + cidr=True) + extra['_allow_cidr_network'] = allow_cidr_network extra['_allow_startswith_dot'] = allow_startswith_dot super().__init__(name, @@ -143,17 +142,17 @@ class DomainnameOption(StrOption): def _validate_ip_network(self, value: str) -> None: allow_ip = self.impl_get_extra('_allow_ip') - allow_network = self.impl_get_extra('_allow_network') - if allow_ip is False and allow_network is False: + allow_cidr_network = self.impl_get_extra('_allow_cidr_network') + if allow_ip is False and allow_cidr_network is False: raise ValueError(_('must not be an IP')) if allow_ip is True: try: self.impl_get_extra('_ip').validate(value) return except ValueError as err: - if allow_network is False: + if allow_cidr_network is False: raise err - if allow_network is True: + if allow_cidr_network is True: self.impl_get_extra('_network').validate(value) def validate(self, @@ -172,7 +171,11 @@ class DomainnameOption(StrOption): warnings_only: bool) -> None: if self.impl_get_extra('_has_upper').search(value): raise ValueError(_('some characters are uppercase')) - if not self.impl_get_extra('_domain_re').search(value): + if self.impl_get_extra('_allow_startswith_dot') and value.startswith('.'): + val = value[1:] + else: + val = value + if not self.impl_get_extra('_domain_re').search(val): if warnings_only: raise ValueError(self.impl_get_extra('_domain_re_message_warning')) raise ValueError(self.impl_get_extra('_domain_re_message')) @@ -181,18 +184,18 @@ class DomainnameOption(StrOption): value: str, warnings_only: bool) -> None: allow_ip = self.impl_get_extra('_allow_ip') - allow_network = self.impl_get_extra('_allow_network') + allow_cidr_network = self.impl_get_extra('_allow_cidr_network') # it's an IP so validate with IPOption - if allow_ip is False and allow_network is False: + if allow_ip is False and allow_cidr_network is False: raise ValueError(_('must not be an IP')) if allow_ip is True: try: self.impl_get_extra('_ip').second_level_validation(value, warnings_only) return except ValueError as err: - if allow_network is False: + if allow_cidr_network is False: raise err - if allow_network is True: + if allow_cidr_network is True: self.impl_get_extra('_network').second_level_validation(value, warnings_only) def second_level_validation(self,