tiramisu/tiramisu/error.py

436 lines
14 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
# Copyright (C) 2012-2026 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 _
2025-12-19 13:30:30 +01:00
from typing import Literal, Union, Optional
2025-05-12 08:53:39 +02:00
TiramisuErrorCode = Literal[
"option-dynamic",
"option-not-found",
"property-frozen",
"property-error",
"property-mandatory",
"leadership-group_type",
"leadership-wrong_property",
"leadership-force_default_on_freeze",
"leadership-greater",
"leadership-follower-greater",
"leadership-follower-callback-list",
]
2016-09-14 20:17:25 +02:00
2024-10-31 08:53:58 +01:00
def display_list(
lst,
*,
separator="and",
add_quote=False,
) -> str():
if not lst:
return '""'
2024-10-31 08:53:58 +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]
2024-10-31 08:53:58 +01:00
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"
2024-10-31 08:53:58 +01:00
def __init__(
self,
subconfig,
proptype,
settings,
opt_type=None,
name=None,
help_properties=None,
):
if opt_type:
self._opt_type = opt_type
self._name = name
else:
2024-04-24 15:39:17 +02:00
if subconfig.option.impl_is_optiondescription():
2024-10-31 08:53:58 +01:00
self._opt_type = "optiondescription"
else:
2024-10-31 08:53:58 +01:00
self._opt_type = "option"
self._name = subconfig.option.impl_get_display_name(
subconfig, with_quote=True
)
self._orig_opt = None
2025-12-19 13:30:30 +01:00
self.subconfig = subconfig
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
2025-05-12 08:53:39 +02:00
if not self.help_properties:
self.help_properties = self.proptype
properties = list(self.help_properties)
if properties == ["frozen"]:
self.code = "property-frozen"
elif properties == ["mandatory"]:
self.code = "property-mandatory"
else:
self.code = "property-error"
2019-06-21 23:04:04 +02:00
super().__init__(None)
2013-04-19 20:10:55 +02:00
2025-05-12 08:53:39 +02:00
def display_properties(self, force_property=False, add_quote=True):
if force_property:
properties = self.proptype
else:
properties = self.help_properties
return display_list(list(properties), add_quote=add_quote)
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:
2024-10-31 08:53:58 +01:00
return "error"
2025-05-12 08:53:39 +02:00
arguments = [self._opt_type]
if self._orig_opt:
arguments.append(
self._orig_opt.impl_get_display_name(subconfig, with_quote=True)
)
arguments.append(self._name)
2025-12-19 13:30:30 +01:00
index = self.subconfig.index
2025-05-12 08:53:39 +02:00
if index is not None:
arguments.append(index)
if self.code == "property-frozen":
if index is not None:
if self._orig_opt:
msg = _(
'cannot modify the {0} {1} at index "{2}" because {3} is frozen'
)
else:
msg = _(
'cannot modify the {0} {1} at index "{2}" because is frozen'
)
2019-11-20 08:27:33 +01:00
else:
2025-05-12 08:53:39 +02:00
if self._orig_opt:
msg = _("cannot modify the {0} {1} because {2} is frozen")
else:
msg = _("cannot modify the {0} {1} because is frozen")
elif self.code == "property-mandatory":
if index is not None:
if self._orig_opt:
msg = _(
2025-05-12 09:05:06 +02:00
'cannot access to {0} {1} at index "{2}" because {3} hasn\'t value'
2025-05-12 08:53:39 +02:00
)
else:
msg = _('{0} {1} at index "{2}" is mandatory but hasn\'t value')
2025-03-19 09:57:03 +01:00
else:
2025-05-12 08:53:39 +02:00
if self._orig_opt:
msg = _("cannot access to {0} {1} because {2} hasn't value")
else:
msg = _("{0} {1} is mandatory but hasn't value")
2019-11-20 08:27:33 +01:00
else:
2025-05-12 08:53:39 +02:00
if index is not None:
if self._orig_opt:
msg = _(
'cannot access to {0} {1} at index "{2}" because {3} has {4} {5}'
)
else:
msg = _(
'cannot access to {0} {1} at index "{2}" because has {3} {4}'
)
2019-11-20 08:27:33 +01:00
else:
2025-05-12 08:53:39 +02:00
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}")
only_one = len(self.help_properties) == 1
if only_one:
arguments.append(_("property"))
else:
arguments.append(_("properties"))
arguments.append(self.display_properties())
self.msg = msg.format(*arguments)
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
2025-05-12 08:53:39 +02:00
class AttributeOptionError(AttributeError):
def __init__(self, path: str, code: TiramisuErrorCode) -> None:
self.path = path
self.code = code
def __str__(self) -> str:
if self.code == "option-dynamic":
return _('cannot access to "{0}" it\'s a dynamic option').format(self.path)
return _('"{0}" is not an option').format(self.path)
2024-10-31 08:53:58 +01:00
# ____________________________________________________________
# Exceptions for a Config
class ConfigError(Exception):
"""attempt to change an option's owner without a value
or in case of `_descr` is None
or if a calculation cannot be carried out"""
2024-10-31 08:53:58 +01:00
def __init__(
self,
exp,
2025-12-19 13:30:30 +01:00
*,
prefix: Optional[str] = None,
subconfig: Optional["Subconfig"]=None,
2024-10-31 08:53:58 +01:00
):
2019-03-13 08:49:18 +01:00
super().__init__(exp)
2025-12-19 13:30:30 +01:00
self.err_msg = exp
self.subconfig = subconfig
self.prefix = prefix
def __str__(self):
msg = self.prefix
if msg:
msg += ", {}".format(self.err_msg)
else:
msg = self.err_msg
return msg
class ConflictError(Exception):
"duplicate options are present in a single config"
pass
2024-10-31 08:53:58 +01:00
# ____________________________________________________________
# miscellaneous exceptions
2019-02-23 19:06:23 +01:00
class LeadershipError(Exception):
2025-05-12 08:53:39 +02:00
def __init__(
self,
subconfig: Union[str, "SubConfig"],
code,
*,
prop=None,
index=None,
length=None,
callback=None,
args=None,
kwargs=None,
ret=None,
):
if isinstance(subconfig, str):
self.path = self.display_name = subconfig
else:
self.path = subconfig.path
option = subconfig.option
self.display_name = option.impl_get_display_name(subconfig, with_quote=True)
self.code = code
2025-09-11 22:07:53 +02:00
if prop is not None:
2025-05-12 08:53:39 +02:00
self.prop = prop
2025-09-11 22:07:53 +02:00
if index is not None:
2025-05-12 08:53:39 +02:00
self.index = index
2025-09-11 22:07:53 +02:00
if length is not None:
2025-05-12 08:53:39 +02:00
self.length = length
2025-09-11 22:07:53 +02:00
if callback is not None:
2025-05-12 08:53:39 +02:00
self.callback = callback
2025-09-11 22:07:53 +02:00
if args is not None:
2025-05-12 08:53:39 +02:00
self.args = args
2025-09-11 22:07:53 +02:00
if kwargs is not None:
2025-05-12 08:53:39 +02:00
self.kwargs = kwargs
2025-09-11 22:07:53 +02:00
if ret is not None:
2025-05-12 08:53:39 +02:00
self.ret = ret
def __str__(self):
if self.code == "leadership-group_type":
return _('cannot set "group_type" attribute for the Leadership {0}').format(
self.display_name
)
if self.code == "leadership-wrong_property":
return _('the leader {0} cannot have "{1}" property').format(
self.display_name, self.prop
)
if self.code == "leadership-force_default_on_freeze":
return _(
'the leader {0} cannot have "force_default_on_freeze" or "force_metaconfig_on_freeze" property without "frozen"'
).format(self.display_name)
if self.code == "leadership-reduce":
return _("cannot reduce length of the leader {0}").format(self.display_name)
if self.code == "leadership-greater":
return _(
'index "{0}" is greater than the leadership length "{1}" for option {2}'
).format(self.index, self.length, self.display_name)
if self.code == "leadership-follower-greater":
return _(
"the follower option {0} has greater length ({1}) than the leader length ({2})"
).format(self.display_name, self.index, self.length)
if self.code == "leadership-follower-callback-list":
if self.args or self.kwargs:
return _(
'the "{0}" function with positional arguments "{1}" and keyword arguments "{2}" must not return a list ("{3}") for the follower option {4}'
).format(
self.callback, self.args, self.kwargs, self.ret, self.display_name
)
return _(
'the "{0}" function must not return a list ("{1}") for the follower option {2}'
).format(self.callback, self.ret, self.display_name)
class ConstError(TypeError):
"no uniq value in _NameSpace"
pass
class _CommonError:
2024-10-31 08:53:58 +01:00
def __init__(self, subconfig, val, display_type, opt, err_msg, index):
self.val = val
self.display_type = display_type
self.opt = weakref.ref(opt)
2025-04-29 22:56:34 +02:00
self.name = opt.impl_get_display_name(subconfig, with_quote=True)
2025-09-11 22:07:53 +02:00
if subconfig:
self.path = subconfig.path
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:
2025-05-12 08:53:39 +02:00
self.prefix = self.tmpl.format(
self.val, _(self.display_type), self.name, self.index
)
msg = self.prefix
2018-10-29 21:01:01 +01:00
if self.err_msg:
if msg:
2024-10-31 08:53:58 +01:00
msg += ", {}".format(self.err_msg)
2018-10-29 21:01:01 +01:00
else:
msg = self.err_msg
if not msg:
2024-10-31 08:53:58 +01:00
msg = _("invalid value")
2018-10-29 21:01:01 +01:00
return msg
class ValueWarning(_CommonError, UserWarning):
2024-10-31 08:53:58 +01:00
tmpl = None
2025-01-04 17:39:11 +01:00
def __init__(self, **kwargs):
2024-10-31 08:53:58 +01:00
if ValueWarning.tmpl is None:
2025-05-12 08:53:39 +02:00
if kwargs.get("index") is None:
ValueWarning.tmpl = _(
'attention, "{0}" could be an invalid {1} for {2}'
)
2025-01-04 17:39:11 +01:00
else:
2025-05-12 08:53:39 +02:00
ValueWarning.tmpl = _(
'attention, "{0}" could be an invalid {1} for {2} at index "{3}"'
)
if list(kwargs) == ["msg"]:
self.msg = kwargs["msg"]
2019-11-20 08:27:33 +01:00
else:
2025-01-04 17:39:11 +01:00
super().__init__(**kwargs)
2019-11-20 08:27:33 +01:00
self.msg = None
def __str__(self):
if self.msg is None:
return super().__str__()
return self.msg
class ValueOptionError(_CommonError, ValueError):
2024-10-31 08:53:58 +01:00
tmpl = None
2025-01-04 17:39:11 +01:00
def __init__(self, **kwargs):
2024-10-31 08:53:58 +01:00
if ValueOptionError.tmpl is None:
2025-10-15 09:43:56 +02:00
opt = kwargs.get("opt")
2025-10-05 20:41:00 +02:00
if opt and opt._do_not_display_value_in_error:
if kwargs.get("index") is None:
2025-10-15 09:43:56 +02:00
self.tmpl = _("{2} has an invalid {1}")
2025-10-05 20:41:00 +02:00
else:
self.tmpl = _('{2} at index "{3}" has an invalid {1}')
2025-01-04 17:39:11 +01:00
else:
2025-10-05 20:41:00 +02:00
if kwargs.get("index") is None:
self.tmpl = _('"{0}" is an invalid {1} for {2}')
else:
self.tmpl = _('"{0}" is an invalid {1} for {2} at index "{3}"')
2025-01-04 17:39:11 +01:00
super().__init__(**kwargs)
class ValueErrorWarning(ValueWarning):
2024-10-31 08:53:58 +01:00
tmpl = None
def __init__(self, *args, **kwargs):
if ValueErrorWarning.tmpl is None:
2025-04-29 22:56:34 +02:00
ValueErrorWarning.tmpl = _('"{0}" is an invalid {1} for {2}')
2024-10-31 08:53:58 +01:00
super().__init__(*args, **kwargs)
class CancelParam(Exception):
def __init__(self, origin_path, current_path):
super().__init__()
self.origin_path = origin_path
self.current_path = current_path
2025-02-07 07:46:53 +01:00
def __ne__(self, value):
return value is None or value == ""
def __eq__(self, value):
return value is None or value == ""
def __bool__(self):
return False
2025-09-11 22:07:53 +02:00
class ValueErrorIndexes(ValueError):
def __init__(self, msg, indexes):
super().__init__(msg)
self.indexes = indexes
class Errors:
@staticmethod
2025-05-12 08:53:39 +02:00
def raise_carry_out_calculation_error(
subconfig, message, original_error, option=None, extra_keys=[]
):
if option is None:
option = subconfig.option
2025-05-12 08:53:39 +02:00
display_name = option.impl_get_display_name(subconfig, with_quote=True)
if original_error:
raise ConfigError(
2025-12-19 13:30:30 +01:00
message.format(display_name, original_error, *extra_keys), subconfig=subconfig,
) from original_error
2025-12-19 13:30:30 +01:00
raise ConfigError(message.format(display_name, extra_keys), subconfig=subconfig)
errors = Errors()