add cidr notation to domainnameoption if allow_ip is True (fixes #5)
This commit is contained in:
parent
2a6df8c8d8
commit
33c1666cc9
|
@ -4,8 +4,7 @@ do_autopath()
|
||||||
import warnings, sys
|
import warnings, sys
|
||||||
from py.test import raises
|
from py.test import raises
|
||||||
|
|
||||||
from tiramisu import Config
|
from tiramisu import Config, DomainnameOption, EmailOption, URLOption, OptionDescription
|
||||||
from tiramisu.option import DomainnameOption, EmailOption, URLOption, OptionDescription
|
|
||||||
from tiramisu.error import ValueWarning
|
from tiramisu.error import ValueWarning
|
||||||
from tiramisu.i18n import _
|
from tiramisu.i18n import _
|
||||||
from tiramisu.storage import list_sessions
|
from tiramisu.storage import list_sessions
|
||||||
|
@ -19,9 +18,11 @@ def test_domainname():
|
||||||
d = DomainnameOption('d', '')
|
d = DomainnameOption('d', '')
|
||||||
f = DomainnameOption('f', '', allow_without_dot=True)
|
f = DomainnameOption('f', '', allow_without_dot=True)
|
||||||
g = DomainnameOption('g', '', allow_ip=True)
|
g = DomainnameOption('g', '', allow_ip=True)
|
||||||
od = OptionDescription('a', '', [d, f, g])
|
h = DomainnameOption('h', '', allow_ip=True, cidr=True)
|
||||||
|
od = OptionDescription('a', '', [d, f, g, h])
|
||||||
cfg = Config(od)
|
cfg = Config(od)
|
||||||
cfg.property.read_write()
|
cfg.property.read_write()
|
||||||
|
#
|
||||||
cfg.option('d').value.set('toto.com')
|
cfg.option('d').value.set('toto.com')
|
||||||
raises(ValueError, "cfg.option('d').value.set('toto')")
|
raises(ValueError, "cfg.option('d').value.set('toto')")
|
||||||
cfg.option('d').value.set('toto3.com')
|
cfg.option('d').value.set('toto3.com')
|
||||||
|
@ -40,9 +41,17 @@ def test_domainname():
|
||||||
cfg.option('f').value.set('d.t')
|
cfg.option('f').value.set('d.t')
|
||||||
#
|
#
|
||||||
raises(ValueError, "cfg.option('f').value.set('192.168.1.1')")
|
raises(ValueError, "cfg.option('f').value.set('192.168.1.1')")
|
||||||
|
raises(ValueError, "cfg.option('f').value.set('192.168.1.0/24')")
|
||||||
|
#
|
||||||
cfg.option('g').value.set('toto.com')
|
cfg.option('g').value.set('toto.com')
|
||||||
cfg.option('g').value.set('192.168.1.0')
|
cfg.option('g').value.set('192.168.1.0')
|
||||||
cfg.option('g').value.set('192.168.1.29')
|
cfg.option('g').value.set('192.168.1.29')
|
||||||
|
raises(ValueError, "cfg.option('g').value.set('192.168.1.0/24')")
|
||||||
|
#
|
||||||
|
cfg.option('h').value.set('toto.com')
|
||||||
|
raises(ValueError, "cfg.option('h').value.set('192.168.1.0')")
|
||||||
|
raises(ValueError, "cfg.option('h').value.set('192.168.1.29')")
|
||||||
|
cfg.option('h').value.set('192.168.1.0/24')
|
||||||
|
|
||||||
|
|
||||||
def test_domainname_upper():
|
def test_domainname_upper():
|
||||||
|
|
|
@ -19,15 +19,14 @@
|
||||||
# the whole pypy projet is under MIT licence
|
# the whole pypy projet is under MIT licence
|
||||||
# ____________________________________________________________
|
# ____________________________________________________________
|
||||||
import re
|
import re
|
||||||
from ipaddress import ip_address, IPv4Address
|
from ipaddress import ip_address
|
||||||
|
|
||||||
from ..setting import undefined, Undefined, OptionBag
|
from ..setting import undefined, Undefined, OptionBag
|
||||||
from ..i18n import _
|
from ..i18n import _
|
||||||
from .option import Option
|
from .option import Option
|
||||||
from .stroption import StrOption
|
from .ipoption import IPOption
|
||||||
|
|
||||||
|
|
||||||
class DomainnameOption(StrOption):
|
class DomainnameOption(IPOption):
|
||||||
"""represents the choice of a domain name
|
"""represents the choice of a domain name
|
||||||
netbios: for MS domain
|
netbios: for MS domain
|
||||||
hostname: to identify the device
|
hostname: to identify the device
|
||||||
|
@ -44,16 +43,17 @@ class DomainnameOption(StrOption):
|
||||||
default=None,
|
default=None,
|
||||||
default_multi=None,
|
default_multi=None,
|
||||||
requires=None,
|
requires=None,
|
||||||
multi=False,
|
multi: bool=False,
|
||||||
callback=None,
|
callback=None,
|
||||||
callback_params=None,
|
callback_params=None,
|
||||||
validator=None,
|
validator=None,
|
||||||
validator_params=None,
|
validator_params=None,
|
||||||
properties=None,
|
properties=None,
|
||||||
allow_ip=False,
|
allow_ip: bool=False,
|
||||||
type_='domainname',
|
cidr: bool=False,
|
||||||
warnings_only=False,
|
type_: str='domainname',
|
||||||
allow_without_dot=False):
|
warnings_only: bool=False,
|
||||||
|
allow_without_dot=False) -> None:
|
||||||
|
|
||||||
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_))
|
||||||
|
@ -73,25 +73,29 @@ class DomainnameOption(StrOption):
|
||||||
else:
|
else:
|
||||||
regexp = r'((?!-)[a-z0-9-]{{1,{0}}})'.format(self._get_len(type_))
|
regexp = r'((?!-)[a-z0-9-]{{1,{0}}})'.format(self._get_len(type_))
|
||||||
if allow_ip:
|
if allow_ip:
|
||||||
regexp = r'^(?:{0}|(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){{3}}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))$'.format(regexp)
|
if not cidr:
|
||||||
|
regexp = r'^(?:{0}|(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){{3}}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))$'.format(regexp)
|
||||||
|
else:
|
||||||
|
regexp = r'^(?:{0}|(?:(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){{3}}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/[0-9][0-9]))$'.format(regexp)
|
||||||
else:
|
else:
|
||||||
regexp = r'^{0}$'.format(regexp)
|
regexp = r'^{0}$'.format(regexp)
|
||||||
extra['_domain_re'] = re.compile(regexp)
|
extra['_domain_re'] = re.compile(regexp)
|
||||||
extra['_has_upper'] = re.compile('[A-Z]')
|
extra['_has_upper'] = re.compile('[A-Z]')
|
||||||
|
|
||||||
super(DomainnameOption, self).__init__(name,
|
super().__init__(name,
|
||||||
doc,
|
doc,
|
||||||
default=default,
|
default=default,
|
||||||
default_multi=default_multi,
|
default_multi=default_multi,
|
||||||
callback=callback,
|
callback=callback,
|
||||||
callback_params=callback_params,
|
callback_params=callback_params,
|
||||||
requires=requires,
|
requires=requires,
|
||||||
multi=multi,
|
multi=multi,
|
||||||
validator=validator,
|
validator=validator,
|
||||||
validator_params=validator_params,
|
validator_params=validator_params,
|
||||||
properties=properties,
|
properties=properties,
|
||||||
warnings_only=warnings_only,
|
warnings_only=warnings_only,
|
||||||
extra=extra)
|
cidr=cidr,
|
||||||
|
_extra=extra)
|
||||||
|
|
||||||
def _get_len(self, type_):
|
def _get_len(self, type_):
|
||||||
if type_ == 'netbios':
|
if type_ == 'netbios':
|
||||||
|
@ -117,9 +121,10 @@ class DomainnameOption(StrOption):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if self.impl_get_extra('_allow_ip') is True:
|
if self.impl_get_extra('_allow_ip') is False:
|
||||||
return
|
raise ValueError(_('must not be an IP'))
|
||||||
raise ValueError(_('must not be an IP'))
|
# it's an IP so validate with IPOption
|
||||||
|
return super()._validate(value, option_bag, current_opt)
|
||||||
part_name_length = self._get_len(self.impl_get_extra('_dom_type'))
|
part_name_length = self._get_len(self.impl_get_extra('_dom_type'))
|
||||||
if self.impl_get_extra('_dom_type') == 'domainname':
|
if self.impl_get_extra('_dom_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:
|
||||||
|
|
|
@ -50,10 +50,15 @@ class IPOption(StrOption):
|
||||||
private_only=False,
|
private_only=False,
|
||||||
allow_reserved=False,
|
allow_reserved=False,
|
||||||
warnings_only=False,
|
warnings_only=False,
|
||||||
cidr=False):
|
cidr=False,
|
||||||
extra = {'_private_only': private_only,
|
_extra=None):
|
||||||
'_allow_reserved': allow_reserved,
|
if _extra is None:
|
||||||
'_cidr': cidr}
|
extra = {}
|
||||||
|
else:
|
||||||
|
extra = _extra
|
||||||
|
extra['_private_only'] = private_only
|
||||||
|
extra['_allow_reserved'] = allow_reserved
|
||||||
|
extra['_cidr'] = cidr
|
||||||
super().__init__(name,
|
super().__init__(name,
|
||||||
doc,
|
doc,
|
||||||
default=default,
|
default=default,
|
||||||
|
|
Loading…
Reference in New Issue