2013-04-16 22:44:16 +02:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2017-07-08 15:59:56 +02:00
|
|
|
|
# Copyright (C) 2012-2017 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-07-09 09:49:03 +02:00
|
|
|
|
import sys
|
2016-09-14 20:17:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def display_list(lst, separator='and'):
|
2017-01-19 21:38:16 +01:00
|
|
|
|
if separator == 'and':
|
|
|
|
|
separator = _('and')
|
|
|
|
|
elif separator == 'or':
|
|
|
|
|
separator = _('or')
|
2017-01-06 21:01:24 +01:00
|
|
|
|
if len(lst) == 0:
|
|
|
|
|
return ''
|
|
|
|
|
elif len(lst) == 1:
|
2017-02-03 15:56:00 +01:00
|
|
|
|
ret = lst[0]
|
2017-07-09 09:49:03 +02:00
|
|
|
|
if sys.version_info[0] < 3 and isinstance(ret, unicode):
|
2017-02-03 15:56:00 +01:00
|
|
|
|
ret = ret.encode('utf8')
|
2017-02-03 23:39:24 +01:00
|
|
|
|
if not isinstance(ret, str):
|
|
|
|
|
ret = str(ret)
|
2017-02-03 15:56:00 +01:00
|
|
|
|
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-07-09 09:49:03 +02:00
|
|
|
|
if sys.version_info[0] < 3 and isinstance(l, unicode):
|
2017-02-02 08:56:01 +01:00
|
|
|
|
l = l.encode('utf8')
|
|
|
|
|
elif not isinstance(l, str):
|
|
|
|
|
l = str(l)
|
|
|
|
|
lst_.append(l)
|
|
|
|
|
last = lst[-1]
|
2017-07-09 09:49:03 +02:00
|
|
|
|
if sys.version_info[0] < 3 and isinstance(last, unicode):
|
2017-02-02 08:56:01 +01:00
|
|
|
|
last = last.encode('utf8')
|
2017-02-03 23:39:24 +01:00
|
|
|
|
if not isinstance(last, str):
|
|
|
|
|
last = str(last)
|
2017-02-02 08:56:01 +01:00
|
|
|
|
return ', '.join(lst_) + _(' {} ').format(separator) + 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"
|
2016-11-15 16:38:25 +01:00
|
|
|
|
def __init__(self, msg, proptype, settings=None, datas=None, option_type=None):
|
2013-04-19 20:10:55 +02:00
|
|
|
|
self.proptype = proptype
|
2016-09-14 20:17:25 +02:00
|
|
|
|
self._settings = settings
|
|
|
|
|
self._datas = datas
|
|
|
|
|
self._type = option_type
|
2017-02-04 14:34:56 +01:00
|
|
|
|
self._orig_opt = None
|
2013-04-19 20:10:55 +02:00
|
|
|
|
super(PropertiesOptionError, self).__init__(msg)
|
|
|
|
|
|
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
|
2016-11-15 16:38:25 +01:00
|
|
|
|
if self._settings is None:
|
|
|
|
|
req = {}
|
|
|
|
|
else:
|
|
|
|
|
req = self._settings.apply_requires(**self._datas)
|
2017-02-04 14:34:56 +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():
|
|
|
|
|
msg.append('{0} ({1})'.format(action, display_list(msg_)))
|
|
|
|
|
else:
|
|
|
|
|
only_one = len(self.proptype) == 1
|
|
|
|
|
msg = self.proptype
|
|
|
|
|
if only_one:
|
2016-09-14 20:17:25 +02:00
|
|
|
|
prop_msg = _('property')
|
|
|
|
|
else:
|
|
|
|
|
prop_msg = _('properties')
|
|
|
|
|
msg = display_list(msg)
|
2017-02-04 14:34:56 +01:00
|
|
|
|
if self._orig_opt:
|
|
|
|
|
return str(_('cannot access to {0} "{1}" because "{2}" has {3} {4}').format(self._type,
|
|
|
|
|
self._orig_opt.impl_get_display_name(),
|
|
|
|
|
self._datas['opt'].impl_get_display_name(),
|
|
|
|
|
prop_msg,
|
|
|
|
|
msg))
|
|
|
|
|
else:
|
|
|
|
|
return str(_('cannot access to {0} "{1}" because has {2} {3}').format(self._type, self._datas['opt'].impl_get_display_name(), prop_msg, msg))
|
2016-09-14 20:17:25 +02:00
|
|
|
|
else:
|
2016-10-21 10:00:44 +02:00
|
|
|
|
return super(PropertiesOptionError, self).__str__()
|
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'
|
|
|
|
|
...
|
|
|
|
|
>>> w[0].message.opt == s
|
|
|
|
|
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
|