tiramisu/tiramisu/option/choiceoption.py

123 lines
5.1 KiB
Python
Raw Normal View History

2017-07-24 19:04:18 +02:00
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Team tiramisu (see AUTHORS for all contributors)
#
# 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 .baseoption import validate_callback
from .option import Option
2017-07-24 19:04:18 +02:00
from ..autolib import carry_out_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()
_display_name = _('choice')
2017-12-13 22:15:34 +01:00
def __init__(self,
name,
doc,
values,
default=None,
values_params=None,
default_multi=None,
requires=None,
multi=False,
callback=None,
callback_params=None,
validator=None,
validator_params=None,
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
"""
if isinstance(values, FunctionType):
2017-12-13 22:15:34 +01:00
validate_callback(values,
values_params,
'values',
self)
2017-07-24 19:04:18 +02:00
else:
if values_params is not None:
raise ValueError(_('values is not a function, so values_params must be None'))
if not isinstance(values, tuple):
raise TypeError(_('values must be a tuple or a function for {0}'
).format(name))
self._choice_values = values
if values_params is not None:
self._choice_values_params = values_params
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,
callback=callback,
callback_params=callback_params,
requires=requires,
multi=multi,
validator=validator,
validator_params=validator_params,
properties=properties,
warnings_only=warnings_only)
2017-12-13 22:15:34 +01:00
def impl_get_values(self,
setting_properties,
context,
current_opt=undefined):
2017-07-24 19:04:18 +02:00
if current_opt is undefined:
current_opt = self
#FIXME cache? but in context...
values = self._choice_values
if isinstance(values, FunctionType):
if context is None:
values = []
else:
2017-12-13 22:15:34 +01:00
values = carry_out_calculation(current_opt,
setting_properties=setting_properties,
context=context,
2017-07-24 19:04:18 +02:00
callback=values,
callback_params=getattr(self, '_choice_values_params', {}))
if values is not undefined and not isinstance(values, list):
raise ConfigError(_('calculated values for {0} is not a list'
'').format(self.impl_getname()))
return values
2017-12-13 22:15:34 +01:00
def _validate(self,
value,
setting_properties,
context=undefined,
current_opt=undefined):
values = self.impl_get_values(setting_properties,
context,
current_opt=current_opt)
2017-07-24 19:04:18 +02:00
if values is not undefined and not value in values:
if len(values) == 1:
2017-12-13 22:15:34 +01:00
raise ValueError(_('only {0} is allowed'
'').format(values[0]))
2017-07-24 19:04:18 +02:00
else:
2017-12-13 22:15:34 +01:00
raise ValueError(_('only {0} are allowed'
'').format(display_list(values)))