2013-04-16 22:44:16 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-26 07:33:47 +01:00
|
|
|
|
# Copyright (C) 2012-2018 Team tiramisu (see AUTHORS for all contributors)
|
2013-04-16 22:44:16 +02:00
|
|
|
|
#
|
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-04-16 22:44:16 +02:00
|
|
|
|
#
|
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-04-16 22:44:16 +02:00
|
|
|
|
#
|
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-04-16 22:44:16 +02:00
|
|
|
|
# ____________________________________________________________
|
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:
|
2017-02-03 15:56:00 +01:00
|
|
|
|
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:
|
2017-07-19 20:44:38 +02:00
|
|
|
|
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):
|
2017-02-02 08:56:01 +01:00
|
|
|
|
l = str(l)
|
2017-12-23 20:21:07 +01:00
|
|
|
|
if add_quote:
|
|
|
|
|
l = '"{}"'.format(l)
|
|
|
|
|
lst_.append(_(l))
|
2017-02-02 08:56:01 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2013-05-15 17:35:49 +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-04-10 22:42:20 +02:00
|
|
|
|
path,
|
|
|
|
|
index,
|
|
|
|
|
config_bag,
|
2017-11-23 16:56:14 +01:00
|
|
|
|
proptype,
|
2018-04-10 22:42:20 +02:00
|
|
|
|
settings,
|
|
|
|
|
opt_type=None,
|
|
|
|
|
requires=None,
|
|
|
|
|
name=None,
|
|
|
|
|
orig_opt=None):
|
|
|
|
|
self._path = path
|
|
|
|
|
self._index = index
|
|
|
|
|
if opt_type:
|
|
|
|
|
self._opt_type = opt_type
|
|
|
|
|
self._requires = requires
|
|
|
|
|
self._name = name
|
|
|
|
|
self._orig_opt = orig_opt
|
|
|
|
|
else:
|
|
|
|
|
if config_bag.option.impl_is_optiondescription():
|
|
|
|
|
self._opt_type = 'optiondescription'
|
|
|
|
|
else:
|
|
|
|
|
self._opt_type = 'option'
|
|
|
|
|
self._requires = config_bag.option.impl_getrequires()
|
|
|
|
|
self._name = config_bag.option.impl_get_display_name()
|
|
|
|
|
self._orig_opt = None
|
2018-04-25 19:12:54 +02:00
|
|
|
|
self._config_bag = config_bag.copy()
|
2013-04-19 20:10:55 +02:00
|
|
|
|
self.proptype = proptype
|
2016-09-14 20:17:25 +02:00
|
|
|
|
self._settings = settings
|
2018-04-10 22:42:20 +02:00
|
|
|
|
self.msg = None
|
|
|
|
|
super(PropertiesOptionError, self).__init__(None)
|
2013-04-19 20:10:55 +02:00
|
|
|
|
|
2017-02-04 14:34:56 +01: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
|
2018-04-10 22:42:20 +02:00
|
|
|
|
if self.msg:
|
|
|
|
|
return self.msg
|
|
|
|
|
req = self._settings.apply_requires(self._path,
|
|
|
|
|
self._requires,
|
|
|
|
|
self._index,
|
|
|
|
|
True,
|
2018-04-11 08:40:59 +02:00
|
|
|
|
self._config_bag,
|
|
|
|
|
self._name)
|
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:
|
2018-04-10 22:42:20 +02:00
|
|
|
|
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-04-10 22:42:20 +02:00
|
|
|
|
del self._path, self._index, self._requires, self._opt_type, self._name, self._config_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
|
|
|
|
|
2013-05-15 17:35:49 +02:00
|
|
|
|
#____________________________________________________________
|
|
|
|
|
# Exceptions for a Config
|
2013-08-28 11:33:43 +02:00
|
|
|
|
class ConfigError(Exception):
|
2013-05-15 17:35:49 +02:00
|
|
|
|
"""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"""
|
2012-07-13 09:37:35 +02:00
|
|
|
|
pass
|
2013-04-13 22:50:55 +02:00
|
|
|
|
|
|
|
|
|
|
2013-08-28 11:33:43 +02:00
|
|
|
|
class ConflictError(Exception):
|
2013-05-15 17:35:49 +02:00
|
|
|
|
"duplicate options are present in a single config"
|
2012-07-13 09:37:35 +02:00
|
|
|
|
pass
|
2013-04-13 22:50:55 +02:00
|
|
|
|
|
|
|
|
|
|
2013-05-15 17:35:49 +02:00
|
|
|
|
#____________________________________________________________
|
|
|
|
|
# miscellaneous exceptions
|
2013-08-28 11:33:43 +02:00
|
|
|
|
class RequirementError(Exception):
|
2013-05-31 23:29:20 +02:00
|
|
|
|
"""a recursive loop occurs in the requirements tree
|
|
|
|
|
requires
|
|
|
|
|
"""
|
2012-07-26 10:54:57 +02:00
|
|
|
|
pass
|
2013-04-19 20:10:55 +02:00
|
|
|
|
|
|
|
|
|
|
2013-08-28 11:33:43 +02:00
|
|
|
|
class SlaveError(Exception):
|
2013-05-15 17:35:49 +02:00
|
|
|
|
"problem with a slave's value length"
|
2013-04-19 20:10:55 +02:00
|
|
|
|
pass
|
2013-08-26 21:48:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConstError(TypeError):
|
|
|
|
|
"no uniq value in _NameSpace"
|
|
|
|
|
pass
|
2013-09-26 21:56:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Warning
|
2014-06-19 23:22:39 +02:00
|
|
|
|
class ValueWarning(UserWarning): # pragma: optional cover
|
2013-09-26 21:56:06 +02:00
|
|
|
|
"""Option could warn user and not raise ValueError.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
>>> import warnings
|
|
|
|
|
>>> from tiramisu.error import ValueWarning
|
|
|
|
|
>>> from tiramisu.option import StrOption, OptionDescription
|
|
|
|
|
>>> from tiramisu.config 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)
|
2013-09-26 21:56:06 +02:00
|
|
|
|
>>> 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
|
2013-09-26 21:56:06 +02:00
|
|
|
|
True
|
2017-11-20 17:01:36 +01:00
|
|
|
|
>>> print(str(w[0].message))
|
2013-09-26 21:56:06 +02:00
|
|
|
|
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
|