tiramisu/tiramisu/option/choiceoption.py

112 lines
3.5 KiB
Python
Raw Normal View History

2017-07-24 19:04:18 +02:00
# -*- coding: utf-8 -*-
# Copyright (C) 2017-2026 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
# ____________________________________________________________
2023-05-11 15:44:48 +02:00
"""ChoiceOption
"""
from typing import Any
from itertools import chain
2017-07-24 19:04:18 +02:00
2024-04-24 15:39:17 +02:00
from ..setting import undefined
2017-07-24 19:04:18 +02:00
from ..i18n import _
2017-07-24 20:39:01 +02:00
from .option import Option
from ..autolib import Calculation, get_calculated_value, ParamOption
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``
"""
2024-10-31 08:53:58 +01:00
__slots__ = tuple()
_type = "choice"
2025-10-05 20:43:42 +02:00
_t_type = _("choice")
2017-12-13 22:15:34 +01:00
2024-10-31 08:53:58 +01:00
def __init__(self, name, doc, values, *args, **kwargs):
2017-07-24 19:04:18 +02:00
"""
:param values: is a list of values the option can possibly take
"""
if isinstance(values, Calculation):
2025-12-19 13:30:30 +01:00
self.value_dependency(values, type_="choice")
elif not isinstance(values, tuple):
2024-10-31 08:53:58 +01:00
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
2024-10-31 08:53:58 +01:00
super().__init__(name, doc, *args, **kwargs)
2017-07-24 19:04:18 +02:00
2024-10-31 08:53:58 +01:00
def impl_get_values(
self,
subconfig: "SubConfig",
uncalculated: bool = False,
):
"""get values allowed by option"""
2024-06-20 12:56:27 +02:00
choices = self._choice_values
if isinstance(choices, tuple):
choices = list(choices)
if uncalculated:
return choices
2024-10-31 08:53:58 +01:00
values = get_calculated_value(
subconfig,
choices,
)[0]
2024-06-20 12:56:27 +02:00
if values != undefined and not isinstance(values, (list, tuple)):
2024-10-31 08:53:58 +01:00
raise ConfigError(
_('the calculated values "{0}" for "{1}" is not a list' "").format(
values, self.impl_getname()
2025-12-19 13:30:30 +01:00
),
subconfig=subconfig,
2024-10-31 08:53:58 +01:00
)
2017-07-24 19:04:18 +02:00
return values
2024-10-31 08:53:58 +01:00
def validate(
self,
value: Any,
) -> None:
"""nothing to valide"""
2019-12-24 15:24:20 +01:00
2024-10-31 08:53:58 +01:00
def validate_with_option(
self,
value: Any,
subconfig: "SubConfig",
loaded: bool,
) -> None:
2023-05-11 15:44:48 +02:00
if loaded and isinstance(self._choice_values, Calculation):
return
2024-04-24 15:39:17 +02:00
values = self.impl_get_values(subconfig)
2020-10-05 21:16:41 +02:00
self.validate_values(value, values)
2024-10-31 08:53:58 +01:00
def validate_values(
self,
value,
values,
) -> None:
"""validate values"""
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:
2024-10-31 08:53:58 +01:00
raise ValueError(_('only "{0}" is allowed' "").format(values[0]))
raise ValueError(
_("only {0} are allowed" "").format(
display_list(values, add_quote=True)
)
)