From 601c3fc54d052cf33855047836cdd54a59743757 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 7 Dec 2018 23:32:28 +0100 Subject: [PATCH] support new special property demoting_error_warning --- tiramisu/error.py | 17 ++++++++++++++--- tiramisu/option/option.py | 30 +++++++++++------------------- tiramisu/setting.py | 3 +++ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/tiramisu/error.py b/tiramisu/error.py index 50a3b47..1b2b723 100644 --- a/tiramisu/error.py +++ b/tiramisu/error.py @@ -15,6 +15,7 @@ # along with this program. If not, see . # ____________________________________________________________ "user defined exceptions" +import weakref from .i18n import _ @@ -180,21 +181,31 @@ class ValueWarning(UserWarning): >>> print(str(w[0].message)) invalid value val for option s: pouet """ - def __init__(self, msg, opt): + def __init__(self, + msg, + opt): self.opt = opt + self.value_error = msg super(ValueWarning, self).__init__(msg) +class ValueErrorWarning(ValueWarning): + def __init__(self, + value_error): + super(ValueWarning, self).__init__(value_error, value_error.opt()) + + class ValueOptionError(ValueError): def __init__(self, val, display_type, - display_name, + opt, err_msg): self.prefix = _('"{0}" is an invalid {1} for "{2}"' '').format(val, display_type, - display_name) + opt.impl_get_display_name()) + self.opt = weakref.ref(opt) self.err_msg = err_msg def __str__(self): diff --git a/tiramisu/option/option.py b/tiramisu/option/option.py index b10f39d..915857d 100644 --- a/tiramisu/option/option.py +++ b/tiramisu/option/option.py @@ -27,7 +27,7 @@ from .baseoption import BaseOption, submulti, STATIC_TUPLE from ..i18n import _ from ..setting import log, undefined, OptionBag, Undefined from ..autolib import carry_out_calculation -from ..error import (ConfigError, ValueWarning, PropertiesOptionError, +from ..error import (ConfigError, ValueWarning, ValueErrorWarning, PropertiesOptionError, ValueOptionError, display_list) from ..function import Params, ParamValue from .syndynoption import SynDynOption @@ -346,10 +346,16 @@ class Option(BaseOption): check_error, is_warnings_only) except ValueError as err: - raise ValueOptionError(val, - self._display_name, - option_bag.ori_option.impl_get_display_name(), - '{0}'.format(err)) + val_err = ValueOptionError(val, + self._display_name, + option_bag.ori_option, + '{0}'.format(err)) + if 'demoting_error' in config_bag.properties: + warnings.warn_explicit(val_err, + ValueErrorWarning, + self.__class__.__name__, 0) + else: + raise val_err def _validate_calculator(self, callback: Callable, @@ -715,17 +721,3 @@ class Option(BaseOption): return SynDynOption(self, rootpath, suffix) - - -class RegexpOption(Option): - __slots__ = tuple() - - def _validate(self, - value: Any, - option_bag: OptionBag, - current_opt: BaseOption=Undefined) -> None: - if not isinstance(value, str): - raise ValueError(_('invalid string')) - match = self._regexp.search(value) - if not match: - raise ValueError() diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 5a05ee6..5a4185e 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -83,6 +83,9 @@ validator warnings display warnings during validation + +demoting_error_warning + all value errors are convert to warning (ValueErrorWarning) """ default_properties = ('cache', 'validator', 'warnings')