merge ValueWarning | ValueOptionError | ValueErrorWarning

This commit is contained in:
Emmanuel Garette 2019-02-12 22:00:59 +01:00
parent 09b25bdd8c
commit e73e6322ae
2 changed files with 27 additions and 51 deletions

View File

@ -178,7 +178,7 @@ class _Value:
def _display_warnings(self, path, value, type, name, withwarning=True): def _display_warnings(self, path, value, type, name, withwarning=True):
for err in self.model.get(path, {}).get('error', []): for err in self.model.get(path, {}).get('error', []):
warnings.warn_explicit(ValueOptionError(value, warnings.warn_explicit(ValueErrorWarning(value,
type, type,
Option(path), Option(path),
'{0}'.format(err)), '{0}'.format(err)),
@ -187,10 +187,11 @@ class _Value:
if withwarning and self.model.get(path, {}).get('warnings'): if withwarning and self.model.get(path, {}).get('warnings'):
for warn in self.model.get(path, {}).get('warnings'): for warn in self.model.get(path, {}).get('warnings'):
# FIXME ValueWarning needs value and type attribute! warnings.warn_explicit(ValueErrorWarning(value,
warnings.warn_explicit(ValueWarning('{0}'.format(warn), type,
Option(path)), Option(path),
ValueWarning, '{0}'.format(err)),
ValueErrorWarning,
self.__class__.__name__, 0) self.__class__.__name__, 0)

View File

@ -37,54 +37,25 @@ except ModuleNotFoundError:
class APIError(Exception): class APIError(Exception):
pass pass
# Warning class _CommonError:
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 ValueOptionError(ValueError):
def __init__(self, def __init__(self,
val, val,
display_type, display_type,
opt, opt,
err_msg): err_msg):
self.prefix = _('"{0}" is an invalid {1} for "{2}"' self.val = val
'').format(val, self.display_type = display_type
display_type,
opt.impl_get_display_name())
self.opt = weakref.ref(opt) self.opt = weakref.ref(opt)
self.err_msg = err_msg self.err_msg = err_msg
super().__init__(self.err_msg)
def __str__(self): def __str__(self):
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 msg = self.prefix
if self.err_msg: if self.err_msg:
if msg: if msg:
@ -95,10 +66,14 @@ except ModuleNotFoundError:
msg = _('invalid value') msg = _('invalid value')
return msg 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): class ValueErrorWarning(ValueWarning):
def __init__(self, tmpl = _('"{0}" is an invalid {1} for "{2}"')
value_error):
super(ValueErrorWarning, self).__init__(value_error, value_error.opt)
# Exceptions for an Option # Exceptions for an Option
class PropertiesOptionError(AttributeError): class PropertiesOptionError(AttributeError):