support new special property demoting_error_warning

This commit is contained in:
Emmanuel Garette 2018-12-07 23:32:28 +01:00
parent 81490b75d9
commit 601c3fc54d
3 changed files with 28 additions and 22 deletions

View file

@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________
"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):

View file

@ -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()

View file

@ -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')