tiramisu/tiramisu/error.py

215 lines
6.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2021-02-24 20:30:04 +01:00
# Copyright (C) 2012-2021 Team tiramisu (see AUTHORS for all contributors)
#
2013-09-22 22:33:09 +02:00
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
2013-09-22 22:33:09 +02:00
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
2013-09-22 22:33:09 +02:00
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ____________________________________________________________
2013-05-23 14:55:52 +02:00
"user defined exceptions"
import weakref
2016-09-14 20:17:25 +02:00
from .i18n import _
2017-12-23 20:21:07 +01:00
def display_list(lst, separator='and', add_quote=False):
2017-01-19 21:38:16 +01:00
if separator == 'and':
separator = _('and')
elif separator == 'or':
separator = _('or')
2018-11-15 16:17:39 +01:00
if isinstance(lst, tuple) or isinstance(lst, frozenset):
lst = list(lst)
2018-04-11 16:36:15 +02:00
if len(lst) == 1:
ret = lst[0]
2017-02-03 23:39:24 +01:00
if not isinstance(ret, str):
ret = str(ret)
2019-09-01 09:41:53 +02:00
if add_quote and not ret.startswith('"'):
2017-12-23 20:21:07 +01:00
ret = '"{}"'.format(ret)
return ret
2019-11-20 08:27:33 +01:00
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)
2013-08-20 12:08:02 +02:00
# Exceptions for an Option
2013-04-19 20:10:55 +02:00
class PropertiesOptionError(AttributeError):
2013-05-21 18:42:56 +02:00
"attempt to access to an option with a property that is not allowed"
2017-11-23 16:56:14 +01:00
def __init__(self,
2018-08-01 08:37:58 +02:00
option_bag,
2017-11-23 16:56:14 +01:00
proptype,
settings,
opt_type=None,
name=None,
2020-01-22 20:46:18 +01:00
orig_opt=None,
help_properties=None):
if opt_type:
self._opt_type = opt_type
self._name = name
self._orig_opt = orig_opt
else:
2018-08-01 08:37:58 +02:00
if option_bag.option.impl_is_optiondescription():
self._opt_type = 'optiondescription'
else:
self._opt_type = 'option'
2018-08-01 08:37:58 +02:00
self._name = option_bag.option.impl_get_display_name()
self._orig_opt = None
2018-08-01 08:37:58 +02:00
self._option_bag = option_bag
2013-04-19 20:10:55 +02:00
self.proptype = proptype
2020-01-22 20:46:18 +01:00
self.help_properties = help_properties
2016-09-14 20:17:25 +02:00
self._settings = settings
self.msg = None
2019-06-21 23:04:04 +02:00
super().__init__(None)
2013-04-19 20:10:55 +02:00
def set_orig_opt(self, opt):
self._orig_opt = opt
2016-09-14 20:17:25 +02:00
def __str__(self):
2019-10-27 11:09:15 +01:00
# this part is a bit slow, so only execute when display
2019-07-04 20:43:47 +02:00
if self.msg is not None:
return self.msg
2019-07-04 20:43:47 +02:00
if self._settings is None:
return 'error'
2020-01-22 20:46:18 +01:00
if self.help_properties:
properties = list(self.help_properties)
else:
properties = list(self.proptype)
2019-10-27 11:09:15 +01:00
only_one = len(properties) == 1
2019-11-20 08:27:33 +01:00
properties_msg = display_list(properties, add_quote=True)
2017-11-23 16:56:14 +01:00
if only_one:
prop_msg = _('property')
else:
prop_msg = _('properties')
2019-11-20 08:27:33 +01:00
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:
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}'
2017-11-23 16:56:14 +01:00
if self._orig_opt:
2019-11-20 08:27:33 +01:00
self.msg = _(msg).format(self._opt_type,
self._orig_opt.impl_get_display_name(),
self._name,
prop_msg,
properties_msg)
2018-04-10 22:46:53 +02:00
else:
2019-11-20 08:27:33 +01:00
self.msg = _(msg).format(self._opt_type,
self._name,
prop_msg,
properties_msg)
2019-10-27 11:09:15 +01:00
del self._opt_type, self._name
del self._settings, self._orig_opt
return self.msg
2016-09-14 20:17:25 +02:00
2013-04-19 20:10:55 +02:00
#____________________________________________________________
# Exceptions for a Config
class ConfigError(Exception):
"""attempt to change an option's owner without a value
or in case of `_cfgimpl_descr` is None
or if a calculation cannot be carried out"""
2019-03-13 08:49:18 +01:00
def __init__(self,
exp,
ori_err=None):
super().__init__(exp)
self.ori_err = ori_err
class ConflictError(Exception):
"duplicate options are present in a single config"
pass
#____________________________________________________________
# miscellaneous exceptions
2019-02-23 19:06:23 +01:00
class LeadershipError(Exception):
"problem with a leadership's value length"
2013-04-19 20:10:55 +02:00
pass
class ConstError(TypeError):
"no uniq value in _NameSpace"
pass
class _CommonError:
2018-10-29 21:01:01 +01:00
def __init__(self,
val,
display_type,
opt,
2019-02-13 22:49:27 +01:00
err_msg,
index):
self.val = val
self.display_type = display_type
self.opt = weakref.ref(opt)
2019-06-21 23:04:04 +02:00
self.name = opt.impl_get_display_name()
2018-10-29 21:01:01 +01:00
self.err_msg = err_msg
2019-02-13 22:49:27 +01:00
self.index = index
super().__init__(self.err_msg)
2018-10-29 21:01:01 +01:00
def __str__(self):
try:
msg = self.prefix
except AttributeError:
self.prefix = self.tmpl.format(self.val,
self.display_type,
2019-06-21 23:04:04 +02:00
self.name)
msg = self.prefix
2018-10-29 21:01:01 +01:00
if self.err_msg:
if msg:
msg += ', {}'.format(self.err_msg)
else:
msg = self.err_msg
if not msg:
msg = _('invalid value')
return msg
class ValueWarning(_CommonError, UserWarning):
tmpl = _('attention, "{0}" could be an invalid {1} for "{2}"')
2019-11-20 08:27:33 +01:00
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}"')
class ValueErrorWarning(ValueWarning):
tmpl = _('"{0}" is an invalid {1} for "{2}"')
2017-10-22 09:48:08 +02:00
class APIError(Exception):
pass