From d959020eed8db61b57ecde6234389f15aa31774e Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 18 Apr 2015 23:11:57 +0200 Subject: [PATCH] ability to disable warnings validation --- ChangeLog | 1 + test/test_option_setting.py | 12 ++++++------ test/test_option_validator.py | 32 ++++++++++++++++++++++++++++++++ tiramisu/option/baseoption.py | 8 +++++--- tiramisu/setting.py | 5 ++++- 5 files changed, 48 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6daa435..602e3cc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ Sat Apr 18 22:42:53 2015 +0200 Emmanuel Garette * refactor validation, build a fake context (with new Values and Settings) to validate value with those object. Now value with callback and consistency are correctly validate + * ability to disable warnings validation Sun Mar 8 12:02:17 2015 +0200 Emmanuel Garette * valid default/callback value in consistencies diff --git a/test/test_option_setting.py b/test/test_option_setting.py index 193955e..9b4e511 100644 --- a/test/test_option_setting.py +++ b/test/test_option_setting.py @@ -310,9 +310,9 @@ def test_extend_config_properties(): descr = make_description() cfg = Config(descr) 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']) - 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(): @@ -334,7 +334,7 @@ def test_reset_properties(): option = cfg.cfgimpl_get_description().gc.dummy assert setting._p_.get_modified_properties() == {} 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() assert setting._p_.get_modified_properties() == {} setting[option].append('test') @@ -342,11 +342,11 @@ def test_reset_properties(): setting.reset() assert setting._p_.get_modified_properties() == {'gc.dummy': set(('test',))} 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) - 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') - 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) assert setting._p_.get_modified_properties() == {} raises(ValueError, 'setting.reset(all_properties=True, opt=option)') diff --git a/test/test_option_validator.py b/test/test_option_validator.py index c997f21..c30bcaa 100644 --- a/test/test_option_validator.py +++ b/test/test_option_validator.py @@ -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') +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(): 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) diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index 4197250..dba4ba6 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -581,9 +581,11 @@ class Option(OnlyOption): if warning: msg = _("warning on the value of the option {0}: {1}").format( self.impl_getname(), warning) - warnings.warn_explicit(ValueWarning(msg, self), - ValueWarning, - self.__class__.__name__, 0) + if context is None or 'warnings' in \ + context.cfgimpl_get_settings(): + warnings.warn_explicit(ValueWarning(msg, self), + ValueWarning, + self.__class__.__name__, 0) elif error: raise ValueError(_("invalid value for option {0}: {1}").format( self.impl_getname(), error)) diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 0973d7c..399b22f 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -78,8 +78,11 @@ everything_frozen validator launch validator set by user in option (this property has no effect 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: