tiramisu/tiramisu/option/choiceoption.py

89 lines
3.5 KiB
Python
Raw Normal View History

2017-07-24 19:04:18 +02:00
# -*- coding: utf-8 -*-
2019-02-12 06:55:47 +01:00
# Copyright (C) 2017-2019 Team tiramisu (see AUTHORS for all contributors)
2017-07-24 19:04:18 +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.
#
# 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.
#
# 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/>.
#
# The original `Config` design model is unproudly borrowed from
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from types import FunctionType
from ..setting import undefined
from ..i18n import _
2017-07-24 20:39:01 +02:00
from .option import Option
2019-10-27 11:09:15 +01:00
from ..autolib import carry_out_calculation, Calculation
2017-07-24 20:39:01 +02:00
from ..error import ConfigError, display_list
2017-07-24 19:04:18 +02:00
class ChoiceOption(Option):
"""represents a choice out of several objects.
The option can also have the value ``None``
"""
__slots__ = tuple()
2019-03-06 21:50:28 +01:00
_type = 'choice'
2017-07-24 19:04:18 +02:00
_display_name = _('choice')
2017-12-13 22:15:34 +01:00
def __init__(self,
name,
doc,
values,
default=None,
default_multi=None,
multi=False,
2019-10-27 11:09:15 +01:00
validators=None,
2017-12-13 22:15:34 +01:00
properties=None,
warnings_only=False):
2017-07-24 19:04:18 +02:00
"""
:param values: is a list of values the option can possibly take
"""
2019-10-27 11:09:15 +01:00
if not isinstance(values, (Calculation, tuple)):
raise TypeError(_('values must be a tuple or a calculation for {0}'
).format(name))
2017-07-24 19:04:18 +02:00
self._choice_values = values
2017-12-13 22:15:34 +01:00
super(ChoiceOption, self).__init__(name,
doc,
default=default,
2017-07-24 19:04:18 +02:00
default_multi=default_multi,
multi=multi,
2019-10-27 11:09:15 +01:00
validators=validators,
2017-07-24 19:04:18 +02:00
properties=properties,
warnings_only=warnings_only)
2017-12-13 22:15:34 +01:00
def impl_get_values(self,
2019-10-27 11:09:15 +01:00
option_bag):
if isinstance(self._choice_values, Calculation):
values = self._choice_values.execute(option_bag)
if values is not undefined and not isinstance(values, list):
raise ConfigError(_('calculated values for {0} is not a list'
'').format(self.impl_getname()))
2019-06-21 23:04:04 +02:00
else:
values = self._choice_values
2017-07-24 19:04:18 +02:00
return values
2017-12-13 22:15:34 +01:00
def _validate(self,
value,
2018-08-01 08:37:58 +02:00
option_bag,
2017-12-13 22:15:34 +01:00
current_opt=undefined):
2019-10-27 11:09:15 +01:00
values = self.impl_get_values(option_bag)
2018-10-31 08:00:19 +01:00
if values is not undefined and value not in values:
2017-07-24 19:04:18 +02:00
if len(values) == 1:
2017-12-19 23:11:45 +01:00
raise ValueError(_('only "{0}" is allowed'
2017-12-13 22:15:34 +01:00
'').format(values[0]))
2017-07-24 19:04:18 +02:00
else:
2019-04-13 22:04:49 +02:00
raise ValueError(_('only {0} are allowed'
'').format(display_list(values, add_quote=True)))