tiramisu/tiramisu/error.py

190 lines
6.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-26 07:33:47 +01:00
# Copyright (C) 2012-2018 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"
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-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)
2017-12-23 20:21:07 +01:00
if add_quote:
ret = '"{}"'.format(ret)
return ret
2016-09-14 20:17:25 +02:00
else:
if isinstance(lst, tuple):
lst = list(lst)
2017-07-09 09:49:03 +02:00
lst.sort()
2016-09-14 20:17:25 +02:00
lst_ = []
for l in lst[:-1]:
2017-12-13 22:15:34 +01:00
if not isinstance(l, str):
l = str(l)
2017-12-23 20:21:07 +01:00
if add_quote:
l = '"{}"'.format(l)
lst_.append(_(l))
last = lst[-1]
2017-02-03 23:39:24 +01:00
if not isinstance(last, str):
2017-12-19 23:11:45 +01:00
last = str(_(last))
2017-12-23 20:21:07 +01:00
if add_quote:
last = '"{}"'.format(last)
return ', '.join(lst_) + _(' {} ').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,
requires=None,
name=None,
orig_opt=None):
if opt_type:
self._opt_type = opt_type
self._requires = requires
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._requires = option_bag.option.impl_getrequires()
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
2016-09-14 20:17:25 +02:00
self._settings = settings
self.msg = None
super(PropertiesOptionError, self).__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):
#this part is a bit slow, so only execute when display
if self.msg:
return self.msg
2018-08-01 08:37:58 +02:00
req = self._settings.apply_requires(self._option_bag,
True)
2017-11-23 16:56:14 +01:00
#if req != {} or self._orig_opt is not None:
if req != {}:
only_one = len(req) == 1
msg = []
for action, msg_ in req.items():
2017-12-23 20:21:07 +01:00
msg.append('"{0}" ({1})'.format(action, display_list(msg_)))
2017-12-28 11:47:29 +01:00
msg = display_list(msg)
2016-09-14 20:17:25 +02:00
else:
2017-11-23 16:56:14 +01:00
only_one = len(self.proptype) == 1
2017-12-28 11:47:29 +01:00
msg = display_list(list(self.proptype), add_quote=True)
2017-11-23 16:56:14 +01:00
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))
2018-04-10 22:46:53 +02:00
else:
self.msg = str(_('cannot access to {0} "{1}" because has {2} {3}'
'').format(self._opt_type,
self._name,
prop_msg,
msg))
2018-08-01 08:37:58 +02:00
del self._requires, self._opt_type, self._name, self._option_bag
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"""
pass
class ConflictError(Exception):
"duplicate options are present in a single config"
pass
#____________________________________________________________
# miscellaneous exceptions
class RequirementError(Exception):
"""a recursive loop occurs in the requirements tree
requires
"""
pass
2013-04-19 20:10:55 +02:00
class SlaveError(Exception):
"problem with a slave's value length"
2013-04-19 20:10:55 +02:00
pass
class ConstError(TypeError):
"no uniq value in _NameSpace"
pass
#Warning
2018-09-29 21:58:41 +02:00
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
2018-08-14 22:15:40 +02:00
>>> from tiramisu import Config
>>> warnings.simplefilter("always", ValueWarning)
>>> def a(val):
... raise ValueError('pouet')
...
2013-09-27 09:52:18 +02:00
>>> 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'
...
2018-03-24 22:37:48 +01:00
>>> w[0].message.opt() == s
True
2017-11-20 17:01:36 +01:00
>>> print(str(w[0].message))
invalid value val for option s: pouet
"""
def __init__(self, msg, opt):
self.opt = opt
super(ValueWarning, self).__init__(msg)
2017-10-22 09:48:08 +02:00
class APIError(Exception):
pass