support new special property demoting_error_warning
This commit is contained in:
parent
81490b75d9
commit
601c3fc54d
@ -15,6 +15,7 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
# ____________________________________________________________
|
# ____________________________________________________________
|
||||||
"user defined exceptions"
|
"user defined exceptions"
|
||||||
|
import weakref
|
||||||
from .i18n import _
|
from .i18n import _
|
||||||
|
|
||||||
|
|
||||||
@ -180,21 +181,31 @@ class ValueWarning(UserWarning):
|
|||||||
>>> print(str(w[0].message))
|
>>> print(str(w[0].message))
|
||||||
invalid value val for option s: pouet
|
invalid value val for option s: pouet
|
||||||
"""
|
"""
|
||||||
def __init__(self, msg, opt):
|
def __init__(self,
|
||||||
|
msg,
|
||||||
|
opt):
|
||||||
self.opt = opt
|
self.opt = opt
|
||||||
|
self.value_error = msg
|
||||||
super(ValueWarning, self).__init__(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):
|
class ValueOptionError(ValueError):
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
val,
|
val,
|
||||||
display_type,
|
display_type,
|
||||||
display_name,
|
opt,
|
||||||
err_msg):
|
err_msg):
|
||||||
self.prefix = _('"{0}" is an invalid {1} for "{2}"'
|
self.prefix = _('"{0}" is an invalid {1} for "{2}"'
|
||||||
'').format(val,
|
'').format(val,
|
||||||
display_type,
|
display_type,
|
||||||
display_name)
|
opt.impl_get_display_name())
|
||||||
|
self.opt = weakref.ref(opt)
|
||||||
self.err_msg = err_msg
|
self.err_msg = err_msg
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -27,7 +27,7 @@ from .baseoption import BaseOption, submulti, STATIC_TUPLE
|
|||||||
from ..i18n import _
|
from ..i18n import _
|
||||||
from ..setting import log, undefined, OptionBag, Undefined
|
from ..setting import log, undefined, OptionBag, Undefined
|
||||||
from ..autolib import carry_out_calculation
|
from ..autolib import carry_out_calculation
|
||||||
from ..error import (ConfigError, ValueWarning, PropertiesOptionError,
|
from ..error import (ConfigError, ValueWarning, ValueErrorWarning, PropertiesOptionError,
|
||||||
ValueOptionError, display_list)
|
ValueOptionError, display_list)
|
||||||
from ..function import Params, ParamValue
|
from ..function import Params, ParamValue
|
||||||
from .syndynoption import SynDynOption
|
from .syndynoption import SynDynOption
|
||||||
@ -346,10 +346,16 @@ class Option(BaseOption):
|
|||||||
check_error,
|
check_error,
|
||||||
is_warnings_only)
|
is_warnings_only)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
raise ValueOptionError(val,
|
val_err = ValueOptionError(val,
|
||||||
self._display_name,
|
self._display_name,
|
||||||
option_bag.ori_option.impl_get_display_name(),
|
option_bag.ori_option,
|
||||||
'{0}'.format(err))
|
'{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,
|
def _validate_calculator(self,
|
||||||
callback: Callable,
|
callback: Callable,
|
||||||
@ -715,17 +721,3 @@ class Option(BaseOption):
|
|||||||
return SynDynOption(self,
|
return SynDynOption(self,
|
||||||
rootpath,
|
rootpath,
|
||||||
suffix)
|
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()
|
|
||||||
|
@ -83,6 +83,9 @@ validator
|
|||||||
|
|
||||||
warnings
|
warnings
|
||||||
display warnings during validation
|
display warnings during validation
|
||||||
|
|
||||||
|
demoting_error_warning
|
||||||
|
all value errors are convert to warning (ValueErrorWarning)
|
||||||
"""
|
"""
|
||||||
default_properties = ('cache', 'validator', 'warnings')
|
default_properties = ('cache', 'validator', 'warnings')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user