better error message

This commit is contained in:
Emmanuel Garette 2019-11-20 08:27:33 +01:00
parent ce297ed804
commit 13c0c0e256
1 changed files with 50 additions and 29 deletions

View File

@ -33,21 +33,19 @@ def display_list(lst, separator='and', add_quote=False):
if add_quote and not ret.startswith('"'):
ret = '"{}"'.format(ret)
return ret
else:
lst.sort()
lst_ = []
for l in lst[:-1]:
if not isinstance(l, str):
l = str(l)
if add_quote and not l.startswith('"'):
l = '"{}"'.format(l)
lst_.append(_(l))
last = lst[-1]
if not isinstance(last, str):
last = str(_(last))
if add_quote and not last.startswith('"'):
last = '"{}"'.format(last)
return ', '.join(lst_) + _(' {} ').format(separator) + '{}'.format(last)
lst_ = []
for l in lst:
if not isinstance(l, str):
l = str(l)
lst_.append(_(l))
lst__ = []
for l in lst_:
if add_quote and not l.startswith('"'):
l = '"{}"'.format(l)
lst__.append(l)
lst__.sort()
last = lst__[-1]
return ', '.join(lst__[:-1]) + _(' {} ').format(separator) + '{}'.format(last)
# Exceptions for an Option
@ -89,30 +87,40 @@ class PropertiesOptionError(AttributeError):
properties = list(self._settings.calc_raises_properties(self._option_bag,
apply_requires=False))
for property_ in self._settings.get_calculated_properties(self._option_bag):
properties.append(property_.help(self._option_bag))
prop = property_.help(self._option_bag)
if prop is not None:
properties.append(prop)
if not properties:
# if proptype == ['mandatory']
properties = self.proptype
only_one = len(properties) == 1
msg = display_list(properties, add_quote=True)
properties_msg = display_list(properties, add_quote=True)
if only_one:
prop_msg = _('property')
else:
prop_msg = _('properties')
if self._orig_opt:
self.msg = str(_('cannot access to {0} "{1}" because "{2}" has {3} {4}'
'').format(self._opt_type,
self._orig_opt.impl_get_display_name(),
self._name,
prop_msg,
msg))
if properties == ['frozen']:
if self._orig_opt:
msg = 'cannot modify the {0} "{1}" because "{2}" has {3} {4}'
else:
msg = 'cannot modify the {0} "{1}" because has {2} {3}'
else:
self.msg = str(_('cannot access to {0} "{1}" because has {2} {3}'
'').format(self._opt_type,
self._name,
prop_msg,
msg))
if self._orig_opt:
msg = 'cannot access to {0} "{1}" because "{2}" has {3} {4}'
else:
msg = 'cannot access to {0} "{1}" because has {2} {3}'
if self._orig_opt:
self.msg = _(msg).format(self._opt_type,
self._orig_opt.impl_get_display_name(),
self._name,
prop_msg,
properties_msg)
else:
self.msg = _(msg).format(self._opt_type,
self._name,
prop_msg,
properties_msg)
del self._opt_type, self._name
del self._settings, self._orig_opt
return self.msg
@ -184,6 +192,19 @@ class _CommonError:
class ValueWarning(_CommonError, UserWarning):
tmpl = _('attention, "{0}" could be an invalid {1} for "{2}"')
def __init__(self, *args, **kwargs):
if len(args) == 1 and not kwargs:
self.msg = args[0]
pass
else:
super().__init__(*args, **kwargs)
self.msg = None
def __str__(self):
if self.msg is None:
return super().__str__()
return self.msg
class ValueOptionError(_CommonError, ValueError):
tmpl = _('"{0}" is an invalid {1} for "{2}"')