merge ValueWarning | ValueOptionError | ValueErrorWarning

This commit is contained in:
2019-02-12 21:08:53 +01:00
parent a59d25fc14
commit 526fe892d0
4 changed files with 135 additions and 62 deletions

View File

@ -154,62 +154,26 @@ class ConstError(TypeError):
pass
#Warning
class ValueWarning(UserWarning):
"""Option could warn user and not raise ValueError.
Example:
>>> import warnings
>>> from tiramisu.error import ValueWarning
>>> from tiramisu.option import StrOption, OptionDescription
>>> from tiramisu import Config
>>> warnings.simplefilter("always", ValueWarning)
>>> def a(val):
... raise ValueError('pouet')
...
>>> s=StrOption('s', '', validator=a, warnings_only=True)
>>> o=OptionDescription('o', '', [s])
>>> c=Config(o)
>>> c.s = 'val'
StrOption:0: ValueWarning: invalid value val for option s: pouet
>>> with warnings.catch_warnings(record=True) as w:
... c.s = 'val'
...
>>> w[0].message.opt() == s
True
>>> print(str(w[0].message))
invalid value val for option s: pouet
"""
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(ValueErrorWarning, self).__init__(value_error, value_error.opt)
class ValueOptionError(ValueError):
class _CommonError:
def __init__(self,
val,
display_type,
opt,
err_msg):
self.prefix = _('"{0}" is an invalid {1} for "{2}"'
'').format(val,
display_type,
opt.impl_get_display_name())
self.val = val
self.display_type = display_type
self.opt = weakref.ref(opt)
self.err_msg = err_msg
super().__init__(self.err_msg)
def __str__(self):
msg = self.prefix
try:
msg = self.prefix
except AttributeError:
self.prefix = self.tmpl.format(self.val,
self.display_type,
self.opt().impl_get_display_name())
msg = self.prefix
if self.err_msg:
if msg:
msg += ', {}'.format(self.err_msg)
@ -220,5 +184,17 @@ class ValueOptionError(ValueError):
return msg
class ValueWarning(_CommonError, UserWarning):
tmpl = _('attention, "{0}" could be an invalid {1} for "{2}"')
class ValueOptionError(_CommonError, ValueError):
tmpl = _('"{0}" is an invalid {1} for "{2}"')
class ValueErrorWarning(ValueWarning):
tmpl = _('"{0}" is an invalid {1} for "{2}"')
class APIError(Exception):
pass

View File

@ -299,14 +299,10 @@ class Option(BaseOption):
is_warnings_only)
except ValueError as err:
if is_warnings_only:
msg = _('attention, "{0}" could be an invalid {1} for "{2}"'
'').format(val,
self._display_name,
self.impl_get_display_name())
err_msg = '{0}'.format(err)
if err_msg:
msg += ', {}'.format(err_msg)
warnings.warn_explicit(ValueWarning(msg, weakref.ref(self)),
warnings.warn_explicit(ValueWarning(_value,
self._display_name,
self,
err_msg = '{0}'.format(err)),
ValueWarning,
self.__class__.__name__, 0)
else:
@ -355,7 +351,7 @@ class Option(BaseOption):
self._display_name,
option_bag.ori_option,
'{0}'.format(err))
warnings.warn_explicit(ValueOptionError(val,
warnings.warn_explicit(ValueErrorWarning(val,
self._display_name,
option_bag.ori_option,
'{0}'.format(err)),
@ -636,12 +632,10 @@ class Option(BaseOption):
context)
except ValueError as err:
if warnings_only:
msg = _('attention, "{0}" could be an invalid {1} for "{2}", {3}'
'').format(value,
self._display_name,
current_opt.impl_get_display_name(),
err)
warnings.warn_explicit(ValueWarning(msg, weakref.ref(current_opt)),
warnings.warn_explicit(ValueWarning(value,
self._display_name,
current_opt,
"{}".format(err)),
ValueWarning,
self.__class__.__name__, 0)
else: