refactor (warnings_only)
This commit is contained in:
parent
3073940ca4
commit
162ae02df8
|
@ -230,3 +230,9 @@ def test_duplicated_option():
|
||||||
root = OptionDescription('root', '', [d1, d2])
|
root = OptionDescription('root', '', [d1, d2])
|
||||||
#in different OptionDescription
|
#in different OptionDescription
|
||||||
raises(ConflictError, "config = Config(root)")
|
raises(ConflictError, "config = Config(root)")
|
||||||
|
|
||||||
|
def test_cannot_assign_value_to_option_description():
|
||||||
|
descr = make_description()
|
||||||
|
cfg = Config(descr)
|
||||||
|
raises(TypeError, "cfg.gc = 3")
|
||||||
|
|
||||||
|
|
|
@ -72,9 +72,9 @@ def test_validator_multi():
|
||||||
|
|
||||||
|
|
||||||
def test_validator_warning():
|
def test_validator_warning():
|
||||||
opt1 = StrOption('opt1', '', validator=return_true, default='val', only_warning=True)
|
opt1 = StrOption('opt1', '', validator=return_true, default='val', warnings_only=True)
|
||||||
opt2 = StrOption('opt2', '', validator=return_false, only_warning=True)
|
opt2 = StrOption('opt2', '', validator=return_false, warnings_only=True)
|
||||||
opt3 = StrOption('opt3', '', validator=return_if_val, multi=True, only_warning=True)
|
opt3 = StrOption('opt3', '', validator=return_if_val, multi=True, warnings_only=True)
|
||||||
root = OptionDescription('root', '', [opt1, opt2, opt3])
|
root = OptionDescription('root', '', [opt1, opt2, opt3])
|
||||||
cfg = Config(root)
|
cfg = Config(root)
|
||||||
assert cfg.opt1 == 'val'
|
assert cfg.opt1 == 'val'
|
||||||
|
@ -111,8 +111,8 @@ def test_validator_warning():
|
||||||
|
|
||||||
|
|
||||||
def test_validator_warning_master_slave():
|
def test_validator_warning_master_slave():
|
||||||
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True, validator=return_false, only_warning=True)
|
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip reseau autorise", multi=True, validator=return_false, warnings_only=True)
|
||||||
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True, validator=return_if_val, only_warning=True)
|
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True, validator=return_if_val, warnings_only=True)
|
||||||
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
|
interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
|
||||||
interface1.impl_set_group_type(groups.master)
|
interface1.impl_set_group_type(groups.master)
|
||||||
assert interface1.impl_get_group_type() == groups.master
|
assert interface1.impl_get_group_type() == groups.master
|
||||||
|
|
|
@ -186,7 +186,7 @@ class SubConfig(object):
|
||||||
return homeconfig.__setattr__(name, value)
|
return homeconfig.__setattr__(name, value)
|
||||||
child = getattr(self.cfgimpl_get_description(), name)
|
child = getattr(self.cfgimpl_get_description(), name)
|
||||||
if isinstance(child, OptionDescription):
|
if isinstance(child, OptionDescription):
|
||||||
raise SyntaxError(_("can't assign to an OptionDescription"))
|
raise TypeError(_("can't assign to an OptionDescription"))
|
||||||
elif not isinstance(child, SymLinkOption):
|
elif not isinstance(child, SymLinkOption):
|
||||||
if self._impl_path is None:
|
if self._impl_path is None:
|
||||||
path = name
|
path = name
|
||||||
|
|
|
@ -77,7 +77,7 @@ class ValueWarning(UserWarning):
|
||||||
>>> def a(val):
|
>>> def a(val):
|
||||||
... raise ValueError('pouet')
|
... raise ValueError('pouet')
|
||||||
...
|
...
|
||||||
>>> s=StrOption('s', '', validator=a, only_warning=True)
|
>>> s=StrOption('s', '', validator=a, warnings_only=True)
|
||||||
>>> o=OptionDescription('o', '', [s])
|
>>> o=OptionDescription('o', '', [s])
|
||||||
>>> c=Config(o)
|
>>> c=Config(o)
|
||||||
>>> c.s = 'val'
|
>>> c.s = 'val'
|
||||||
|
|
|
@ -328,13 +328,13 @@ class Option(BaseOption):
|
||||||
"""
|
"""
|
||||||
__slots__ = ('_multi', '_validator', '_default_multi', '_default',
|
__slots__ = ('_multi', '_validator', '_default_multi', '_default',
|
||||||
'_state_callback', '_callback', '_multitype',
|
'_state_callback', '_callback', '_multitype',
|
||||||
'_only_warning', '_master_slaves', '__weakref__')
|
'_warnings_only', '_master_slaves', '__weakref__')
|
||||||
_empty = ''
|
_empty = ''
|
||||||
|
|
||||||
def __init__(self, name, doc, default=None, default_multi=None,
|
def __init__(self, name, doc, default=None, default_multi=None,
|
||||||
requires=None, multi=False, callback=None,
|
requires=None, multi=False, callback=None,
|
||||||
callback_params=None, validator=None, validator_params=None,
|
callback_params=None, validator=None, validator_params=None,
|
||||||
properties=None, only_warning=False):
|
properties=None, warnings_only=False):
|
||||||
"""
|
"""
|
||||||
:param name: the option's name
|
:param name: the option's name
|
||||||
:param doc: the option's description
|
:param doc: the option's description
|
||||||
|
@ -352,7 +352,7 @@ class Option(BaseOption):
|
||||||
validation of the value
|
validation of the value
|
||||||
:param validator_params: the validator's parameters
|
:param validator_params: the validator's parameters
|
||||||
:param properties: tuple of default properties
|
:param properties: tuple of default properties
|
||||||
:param only_warning: _validator and _consistencies don't raise if True
|
:param warnings_only: _validator and _consistencies don't raise if True
|
||||||
Values()._warning contain message
|
Values()._warning contain message
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -391,7 +391,7 @@ class Option(BaseOption):
|
||||||
default = []
|
default = []
|
||||||
self._multitype = multitypes.default
|
self._multitype = multitypes.default
|
||||||
self._default_multi = default_multi
|
self._default_multi = default_multi
|
||||||
self._only_warning = only_warning
|
self._warnings_only = warnings_only
|
||||||
self.impl_validate(default)
|
self.impl_validate(default)
|
||||||
self._default = default
|
self._default = default
|
||||||
|
|
||||||
|
@ -475,6 +475,8 @@ class Option(BaseOption):
|
||||||
def do_validation(_value, _index=None):
|
def do_validation(_value, _index=None):
|
||||||
if _value is None:
|
if _value is None:
|
||||||
return
|
return
|
||||||
|
# option validation
|
||||||
|
self._validate(_value)
|
||||||
try:
|
try:
|
||||||
# valid with self._validator
|
# valid with self._validator
|
||||||
val_validator(_value)
|
val_validator(_value)
|
||||||
|
@ -485,32 +487,26 @@ class Option(BaseOption):
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
msg = _("invalid value {0} for option {1}: {2}").format(
|
msg = _("invalid value {0} for option {1}: {2}").format(
|
||||||
_value, self._name, err)
|
_value, self._name, err)
|
||||||
if self._only_warning:
|
if self._warnings_only:
|
||||||
warnings.warn_explicit(ValueWarning(msg, self),
|
warnings.warn_explicit(ValueWarning(msg, self),
|
||||||
ValueWarning,
|
ValueWarning,
|
||||||
self.__class__.__name__, 0)
|
self.__class__.__name__, 0)
|
||||||
else:
|
else:
|
||||||
raise ValueError(msg)
|
raise ValueError(msg)
|
||||||
# option validation
|
|
||||||
self._validate(_value)
|
|
||||||
|
|
||||||
# generic calculation
|
# generic calculation
|
||||||
if context is not None:
|
if context is not None:
|
||||||
descr = context.cfgimpl_get_description()
|
descr = context.cfgimpl_get_description()
|
||||||
|
|
||||||
ret = None
|
|
||||||
if not self._multi or force_no_multi:
|
if not self._multi or force_no_multi:
|
||||||
ret = do_validation(value)
|
do_validation(value)
|
||||||
else:
|
else:
|
||||||
if not isinstance(value, list):
|
if not isinstance(value, list):
|
||||||
raise ValueError(_("invalid value {0} for option {1} "
|
raise ValueError(_("invalid value {0} for option {1} "
|
||||||
"which must be a list").format(value,
|
"which must be a list").format(value,
|
||||||
self._name))
|
self._name))
|
||||||
for index, val in enumerate(value):
|
for index, val in enumerate(value):
|
||||||
ret_ = do_validation(val, index)
|
do_validation(val, index)
|
||||||
if ret_ is not None:
|
|
||||||
ret = ret_
|
|
||||||
return ret
|
|
||||||
|
|
||||||
def impl_getdefault(self, default_multi=False):
|
def impl_getdefault(self, default_multi=False):
|
||||||
"accessing the default value"
|
"accessing the default value"
|
||||||
|
@ -628,7 +624,7 @@ class ChoiceOption(Option):
|
||||||
def __init__(self, name, doc, values, default=None, default_multi=None,
|
def __init__(self, name, doc, values, default=None, default_multi=None,
|
||||||
requires=None, multi=False, callback=None,
|
requires=None, multi=False, callback=None,
|
||||||
callback_params=None, open_values=False, validator=None,
|
callback_params=None, open_values=False, validator=None,
|
||||||
validator_params=None, properties=None, only_warning=False):
|
validator_params=None, properties=None, warnings_only=False):
|
||||||
"""
|
"""
|
||||||
:param values: is a list of values the option can possibly take
|
:param values: is a list of values the option can possibly take
|
||||||
"""
|
"""
|
||||||
|
@ -648,7 +644,7 @@ class ChoiceOption(Option):
|
||||||
validator=validator,
|
validator=validator,
|
||||||
validator_params=validator_params,
|
validator_params=validator_params,
|
||||||
properties=properties,
|
properties=properties,
|
||||||
only_warning=only_warning)
|
warnings_only=warnings_only)
|
||||||
|
|
||||||
def impl_get_values(self):
|
def impl_get_values(self):
|
||||||
return self._values
|
return self._values
|
||||||
|
@ -767,7 +763,7 @@ class IPOption(Option):
|
||||||
requires=None, multi=False, callback=None,
|
requires=None, multi=False, callback=None,
|
||||||
callback_params=None, validator=None, validator_params=None,
|
callback_params=None, validator=None, validator_params=None,
|
||||||
properties=None, only_private=False, allow_reserved=False,
|
properties=None, only_private=False, allow_reserved=False,
|
||||||
only_warning=False):
|
warnings_only=False):
|
||||||
self._only_private = only_private
|
self._only_private = only_private
|
||||||
self._allow_reserved = allow_reserved
|
self._allow_reserved = allow_reserved
|
||||||
super(IPOption, self).__init__(name, doc, default=default,
|
super(IPOption, self).__init__(name, doc, default=default,
|
||||||
|
@ -779,7 +775,7 @@ class IPOption(Option):
|
||||||
validator=validator,
|
validator=validator,
|
||||||
validator_params=validator_params,
|
validator_params=validator_params,
|
||||||
properties=properties,
|
properties=properties,
|
||||||
only_warning=only_warning)
|
warnings_only=warnings_only)
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
try:
|
try:
|
||||||
|
@ -813,7 +809,7 @@ class PortOption(Option):
|
||||||
callback_params=None, validator=None, validator_params=None,
|
callback_params=None, validator=None, validator_params=None,
|
||||||
properties=None, allow_range=False, allow_zero=False,
|
properties=None, allow_range=False, allow_zero=False,
|
||||||
allow_wellknown=True, allow_registred=True,
|
allow_wellknown=True, allow_registred=True,
|
||||||
allow_private=False, only_warning=False):
|
allow_private=False, warnings_only=False):
|
||||||
self._allow_range = allow_range
|
self._allow_range = allow_range
|
||||||
self._min_value = None
|
self._min_value = None
|
||||||
self._max_value = None
|
self._max_value = None
|
||||||
|
@ -846,7 +842,7 @@ class PortOption(Option):
|
||||||
validator=validator,
|
validator=validator,
|
||||||
validator_params=validator_params,
|
validator_params=validator_params,
|
||||||
properties=properties,
|
properties=properties,
|
||||||
only_warning=only_warning)
|
warnings_only=warnings_only)
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
if self._allow_range and ":" in str(value):
|
if self._allow_range and ":" in str(value):
|
||||||
|
@ -948,7 +944,7 @@ class DomainnameOption(Option):
|
||||||
requires=None, multi=False, callback=None,
|
requires=None, multi=False, callback=None,
|
||||||
callback_params=None, validator=None, validator_params=None,
|
callback_params=None, validator=None, validator_params=None,
|
||||||
properties=None, allow_ip=False, type_='domainname',
|
properties=None, allow_ip=False, type_='domainname',
|
||||||
only_warning=False):
|
warnings_only=False):
|
||||||
#netbios: for MS domain
|
#netbios: for MS domain
|
||||||
#hostname: to identify the device
|
#hostname: to identify the device
|
||||||
#domainname:
|
#domainname:
|
||||||
|
@ -968,7 +964,7 @@ class DomainnameOption(Option):
|
||||||
validator=validator,
|
validator=validator,
|
||||||
validator_params=validator_params,
|
validator_params=validator_params,
|
||||||
properties=properties,
|
properties=properties,
|
||||||
only_warning=only_warning)
|
warnings_only=warnings_only)
|
||||||
|
|
||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
if self._allow_ip is True:
|
if self._allow_ip is True:
|
||||||
|
|
Loading…
Reference in New Issue