ability to disable warnings validation

This commit is contained in:
Emmanuel Garette 2015-04-18 23:11:57 +02:00
parent 10768a6067
commit d959020eed
5 changed files with 48 additions and 10 deletions

View File

@ -2,6 +2,7 @@ Sat Apr 18 22:42:53 2015 +0200 Emmanuel Garette <egarette@cadoles.com>
* refactor validation, build a fake context (with new Values and * refactor validation, build a fake context (with new Values and
Settings) to validate value with those object. Now value with Settings) to validate value with those object. Now value with
callback and consistency are correctly validate callback and consistency are correctly validate
* ability to disable warnings validation
Sun Mar 8 12:02:17 2015 +0200 Emmanuel Garette <egarette@cadoles.com> Sun Mar 8 12:02:17 2015 +0200 Emmanuel Garette <egarette@cadoles.com>
* valid default/callback value in consistencies * valid default/callback value in consistencies

View File

@ -310,9 +310,9 @@ def test_extend_config_properties():
descr = make_description() descr = make_description()
cfg = Config(descr) cfg = Config(descr)
setting = cfg.cfgimpl_get_settings() setting = cfg.cfgimpl_get_settings()
assert str(setting) == str(['cache', 'expire', 'validator']) assert set(eval(str(setting))) == set(['cache', 'expire', 'validator', 'warnings'])
setting.extend(['test', 'test2']) setting.extend(['test', 'test2'])
assert str(setting) == str(['test', 'cache', 'test2', 'expire', 'validator']) assert set(eval(str(setting))) == set(['test', 'cache', 'test2', 'expire', 'validator', 'warnings'])
def test_append_properties(): def test_append_properties():
@ -334,7 +334,7 @@ def test_reset_properties():
option = cfg.cfgimpl_get_description().gc.dummy option = cfg.cfgimpl_get_description().gc.dummy
assert setting._p_.get_modified_properties() == {} assert setting._p_.get_modified_properties() == {}
setting.append('frozen') setting.append('frozen')
assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'cache', 'validator'))} assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'cache', 'validator', 'warnings'))}
setting.reset() setting.reset()
assert setting._p_.get_modified_properties() == {} assert setting._p_.get_modified_properties() == {}
setting[option].append('test') setting[option].append('test')
@ -342,11 +342,11 @@ def test_reset_properties():
setting.reset() setting.reset()
assert setting._p_.get_modified_properties() == {'gc.dummy': set(('test',))} assert setting._p_.get_modified_properties() == {'gc.dummy': set(('test',))}
setting.append('frozen') setting.append('frozen')
assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache')), 'gc.dummy': set(('test',))} assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache', 'warnings')), 'gc.dummy': set(('test',))}
setting.reset(option) setting.reset(option)
assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache'))} assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache', 'warnings'))}
setting[option].append('test') setting[option].append('test')
assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache')), 'gc.dummy': set(('test',))} assert setting._p_.get_modified_properties() == {None: set(('frozen', 'expire', 'validator', 'cache', 'warnings')), 'gc.dummy': set(('test',))}
setting.reset(all_properties=True) setting.reset(all_properties=True)
assert setting._p_.get_modified_properties() == {} assert setting._p_.get_modified_properties() == {}
raises(ValueError, 'setting.reset(all_properties=True, opt=option)') raises(ValueError, 'setting.reset(all_properties=True, opt=option)')

View File

@ -113,6 +113,38 @@ def test_validator_warning():
assert str(w[1].message) == _("warning on the value of the option {0}: {1}").format('opt3', 'error') assert str(w[1].message) == _("warning on the value of the option {0}: {1}").format('opt3', 'error')
def test_validator_warning_disabled():
opt1 = StrOption('opt1', '', validator=return_true, default='val', warnings_only=True)
opt2 = StrOption('opt2', '', validator=return_false, warnings_only=True)
opt3 = StrOption('opt3', '', validator=return_if_val, multi=True, warnings_only=True)
root = OptionDescription('root', '', [opt1, opt2, opt3])
cfg = Config(root)
cfg.cfgimpl_get_settings().remove('warnings')
assert cfg.opt1 == 'val'
warnings.simplefilter("always", ValueWarning)
with warnings.catch_warnings(record=True) as w:
cfg.opt1 = 'val'
assert w == []
#
with warnings.catch_warnings(record=True) as w:
cfg.opt2 = 'val'
assert w == []
#
with warnings.catch_warnings(record=True) as w:
cfg.opt3.append('val')
assert w == []
#
with warnings.catch_warnings(record=True) as w:
cfg.opt3.append('val1')
assert w == []
raises(ValueError, "cfg.opt2 = 1")
#
with warnings.catch_warnings(record=True) as w:
cfg.opt2 = 'val'
cfg.opt3.append('val')
assert w == []
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, warnings_only=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, warnings_only=True) netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-reseau", multi=True, validator=return_if_val, warnings_only=True)

View File

@ -581,9 +581,11 @@ class Option(OnlyOption):
if warning: if warning:
msg = _("warning on the value of the option {0}: {1}").format( msg = _("warning on the value of the option {0}: {1}").format(
self.impl_getname(), warning) self.impl_getname(), warning)
warnings.warn_explicit(ValueWarning(msg, self), if context is None or 'warnings' in \
ValueWarning, context.cfgimpl_get_settings():
self.__class__.__name__, 0) warnings.warn_explicit(ValueWarning(msg, self),
ValueWarning,
self.__class__.__name__, 0)
elif error: elif error:
raise ValueError(_("invalid value for option {0}: {1}").format( raise ValueError(_("invalid value for option {0}: {1}").format(
self.impl_getname(), error)) self.impl_getname(), error))

View File

@ -78,8 +78,11 @@ everything_frozen
validator validator
launch validator set by user in option (this property has no effect launch validator set by user in option (this property has no effect
for internal validator) for internal validator)
warnings
display warnings during validation
""" """
default_properties = ('cache', 'expire', 'validator') default_properties = ('cache', 'expire', 'validator', 'warnings')
"""Config can be in two defaut mode: """Config can be in two defaut mode: