tiramisu/tiramisu/option/option.py

452 lines
19 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"option types and option description"
2023-04-12 12:33:44 +02:00
# Copyright (C) 2012-2023 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
# ____________________________________________________________
2017-07-24 20:39:01 +02:00
import warnings
2023-05-11 15:44:48 +02:00
from typing import Any, List, Optional, Dict
2019-09-28 16:32:48 +02:00
from itertools import chain
2023-05-11 15:44:48 +02:00
from .baseoption import BaseOption, submulti
2017-07-24 20:39:01 +02:00
from ..i18n import _
2024-04-24 15:39:17 +02:00
from ..setting import undefined
2024-06-20 12:56:27 +02:00
from ..autolib import Calculation
2023-05-11 15:44:48 +02:00
from ..error import ValueWarning, ValueErrorWarning, ValueOptionError
2017-07-24 20:39:01 +02:00
class Option(BaseOption):
2023-05-11 15:44:48 +02:00
# pylint: disable=too-many-statements,too-many-branches,too-many-arguments,too-many-locals
2017-07-24 20:39:01 +02:00
"""
Abstract base class for configuration option's.
Reminder: an Option object is **not** a container for the value.
"""
__slots__ = ('_extra',
'_warnings_only',
2019-10-27 11:09:15 +01:00
# multi
2017-07-24 20:39:01 +02:00
'_multi',
2019-10-27 11:09:15 +01:00
# value
2017-07-24 20:39:01 +02:00
'_default',
'_default_multi',
2019-10-27 11:09:15 +01:00
#
'_validators',
2017-07-24 20:39:01 +02:00
#
2019-02-23 19:06:23 +01:00
'_leadership',
2017-07-24 20:39:01 +02:00
'_choice_values',
'_choice_values_params',
)
2023-05-11 15:44:48 +02:00
_type = None
2017-12-07 21:42:04 +01:00
def __init__(self,
2018-11-15 18:35:14 +01:00
name: str,
doc: str,
default: Any=undefined,
default_multi: Any=None,
multi: bool=False,
2019-10-27 11:09:15 +01:00
validators: Optional[List[Calculation]]=None,
2018-11-15 18:35:14 +01:00
properties: Optional[List[str]]=None,
warnings_only: bool=False,
2024-06-20 12:56:27 +02:00
extra: Optional[Dict]=None,
informations: Optional[Dict]=None,
):
2017-07-24 20:39:01 +02:00
_setattr = object.__setattr__
if not multi and default_multi is not None:
raise ValueError(_("default_multi is set whereas multi is False"
" in option: {0}").format(name))
2017-10-22 09:48:08 +02:00
if default is undefined:
if multi is False:
default = None
else:
default = []
2017-07-24 20:39:01 +02:00
if multi is True:
is_multi = True
_multi = 0
elif multi is False:
is_multi = False
_multi = 1
elif multi is submulti:
is_multi = True
_multi = submulti
else:
2020-08-04 16:49:21 +02:00
raise ValueError(_('invalid multi type "{}" for "{}"').format(multi,
name,
))
2017-07-24 20:39:01 +02:00
if _multi != 1:
_setattr(self, '_multi', _multi)
if multi is not False and default is None:
default = []
2018-03-19 08:33:53 +01:00
super().__init__(name,
doc,
2024-06-20 12:56:27 +02:00
informations,
2018-03-19 08:33:53 +01:00
properties=properties,
2024-06-20 12:56:27 +02:00
is_multi=is_multi,
)
if validators is not None:
if __debug__ and not isinstance(validators, list):
2023-05-11 15:44:48 +02:00
raise ValueError(_(f'validators must be a list of Calculation for "{name}"'))
for validator in validators:
if __debug__ and not isinstance(validator, Calculation):
raise ValueError(_('validators must be a Calculation for "{}"').format(name))
self.value_dependency(validator)
2019-10-27 11:09:15 +01:00
self._validators = tuple(validators)
2018-04-22 10:43:19 +02:00
if extra is not None and extra != {}:
2017-07-24 20:39:01 +02:00
_setattr(self, '_extra', extra)
if warnings_only is True:
_setattr(self, '_warnings_only', warnings_only)
if is_multi and default_multi is not None:
2017-11-12 14:33:05 +01:00
def test_multi_value(value):
2019-09-28 16:32:48 +02:00
if isinstance(value, Calculation):
return
2024-04-24 15:39:17 +02:00
# option_bag = OptionBag(self,
# None,
# undefined,
# properties=None,
# )
2017-12-19 23:11:45 +01:00
try:
2019-11-20 08:26:41 +01:00
self.validate(value)
2023-05-11 15:44:48 +02:00
self.validate_with_option(value,
2024-04-24 15:39:17 +02:00
None,
2023-05-11 15:44:48 +02:00
loaded=True,
)
2017-12-19 23:11:45 +01:00
except ValueError as err:
2019-09-28 16:32:48 +02:00
str_err = str(err)
if not str_err:
raise ValueError(_('invalid default_multi value "{0}" '
'for option "{1}"').format(str(value),
2024-06-20 12:56:27 +02:00
self.impl_get_display_name(None))
2023-05-11 15:44:48 +02:00
) from err
raise ValueError(_(f'invalid default_multi value "{value}" for option '
2024-06-20 12:56:27 +02:00
f'"{self.impl_get_display_name(None)}", {str_err}')
2023-05-11 15:44:48 +02:00
) from err
2017-11-12 14:33:05 +01:00
if _multi is submulti:
2019-09-28 16:32:48 +02:00
if not isinstance(default_multi, Calculation):
if not isinstance(default_multi, list):
raise ValueError(_('invalid default_multi value "{0}" '
'for option "{1}", must be a list for a submulti'
'').format(str(default_multi),
2024-06-20 12:56:27 +02:00
self.impl_get_display_name(None)))
2019-09-28 16:32:48 +02:00
for value in default_multi:
test_multi_value(value)
2017-11-12 14:33:05 +01:00
else:
test_multi_value(default_multi)
2017-07-24 20:39:01 +02:00
_setattr(self, '_default_multi', default_multi)
2024-04-24 15:39:17 +02:00
# option_bag = OptionBag(self,
# None,
# undefined,
# properties=None,
# )
self.impl_validate(None,
default,
2023-05-11 15:44:48 +02:00
loaded=True,
)
2024-04-24 15:39:17 +02:00
self.impl_validate(None,
default,
2023-05-11 15:44:48 +02:00
check_error=False,
loaded=True,
)
self.value_dependencies(default)
2017-07-24 20:39:01 +02:00
if (is_multi and default != []) or \
(not is_multi and default is not None):
2019-09-28 16:32:48 +02:00
if is_multi and isinstance(default, list):
2017-07-24 20:39:01 +02:00
default = tuple(default)
_setattr(self, '_default', default)
2018-09-30 11:36:09 +02:00
#__________________________________________________________________________
# option's information
2018-11-15 18:35:14 +01:00
def impl_is_multi(self) -> bool:
2023-05-11 15:44:48 +02:00
"""is it a multi option
"""
2017-07-24 20:39:01 +02:00
return getattr(self, '_multi', 1) != 1
2018-11-15 18:35:14 +01:00
def impl_is_submulti(self) -> bool:
2023-05-11 15:44:48 +02:00
"""is it a submulti option
"""
2018-09-30 11:36:09 +02:00
return getattr(self, '_multi', 1) == 2
2018-11-15 18:35:14 +01:00
def impl_is_dynsymlinkoption(self) -> bool:
2023-05-11 15:44:48 +02:00
"""is a dynsymlinkoption?
"""
2018-09-30 11:36:09 +02:00
return False
2019-03-06 21:50:28 +01:00
def get_type(self) -> str:
2023-05-11 15:44:48 +02:00
"""get the type of option
"""
return self._type
2018-12-24 09:30:58 +01:00
2018-11-15 18:35:14 +01:00
def impl_getdefault(self) -> Any:
2023-05-11 15:44:48 +02:00
"""accessing the default value
"""
2018-09-30 11:36:09 +02:00
is_multi = self.impl_is_multi()
default = getattr(self, '_default', undefined)
if default is undefined:
if is_multi:
default = []
else:
default = None
2023-05-11 15:44:48 +02:00
elif is_multi and isinstance(default, tuple):
default = list(default)
2018-09-30 11:36:09 +02:00
return default
2018-11-15 18:35:14 +01:00
def impl_getdefault_multi(self) -> Any:
2023-05-11 15:44:48 +02:00
"""accessing the default value for a multi
"""
2018-09-30 11:36:09 +02:00
if self.impl_is_submulti():
default_value = []
else:
default_value = None
return getattr(self, '_default_multi', default_value)
def impl_get_extra(self,
2023-05-11 15:44:48 +02:00
key: str,
) -> Any:
"""if extra parameters are store get it
"""
2019-02-25 08:46:58 +01:00
extra = getattr(self, '_extra', {})
if isinstance(extra, tuple):
if key in extra[0]:
return extra[1][extra[0].index(key)]
return None
2023-05-11 15:44:48 +02:00
return extra.get(key)
2018-09-30 11:36:09 +02:00
#__________________________________________________________________________
# validator
def impl_validate(self,
2024-04-24 15:39:17 +02:00
subconfig: Optional["SubConfig"],
2023-05-11 15:44:48 +02:00
value: Any,
2024-04-24 15:39:17 +02:00
*,
2023-05-11 15:44:48 +02:00
check_error: bool=True,
loaded: bool=False,
2023-11-15 21:44:15 +01:00
) -> bool:
"""Return True if value is really valid
If not validate or invalid return it returns False
2017-07-24 20:39:01 +02:00
"""
2024-04-24 15:39:17 +02:00
if check_error and subconfig and \
not 'validator' in subconfig.config_bag.properties:
return False
2024-04-24 15:39:17 +02:00
if subconfig:
force_index = subconfig.index
else:
force_index = None
is_warnings_only = getattr(self, '_warnings_only', False)
2017-07-24 20:39:01 +02:00
2024-04-24 15:39:17 +02:00
def _is_not_unique(value):
2018-09-12 21:05:14 +02:00
# if set(value) has not same length than value
2024-04-24 15:39:17 +02:00
if not subconfig or not check_error or \
'unique' not in subconfig.properties:
2022-10-01 19:44:23 +02:00
return
lvalue = [val for val in value if val is not None]
if len(set(lvalue)) == len(lvalue):
return
for idx, val in enumerate(value):
if val not in value[idx+1:]:
continue
raise ValueError(_('the value "{}" is not unique'
'').format(val))
2017-07-24 20:39:01 +02:00
def calculation_validator(val,
2023-05-11 15:44:48 +02:00
_index,
):
2019-10-27 11:09:15 +01:00
for validator in getattr(self, '_validators', []):
2023-05-11 15:44:48 +02:00
calc_is_warnings_only = hasattr(validator, 'warnings_only') and \
validator.warnings_only
2019-10-27 11:09:15 +01:00
if ((check_error and not calc_is_warnings_only) or
(not check_error and calc_is_warnings_only)):
try:
2020-08-04 16:49:21 +02:00
kwargs = {'allow_value_error': True,
'force_value_warning': calc_is_warnings_only,
}
2024-04-24 15:39:17 +02:00
if _index is not None and subconfig.index == _index:
lsubconfig = subconfig
2019-10-27 11:09:15 +01:00
else:
2024-04-24 15:39:17 +02:00
suffix = subconfig.suffixes
if suffix is not None:
suffix = suffix[-1]
lsubconfig = subconfig.parent.get_child(subconfig.option,
_index,
False,
properties=subconfig.properties,
suffix=suffix,
name=subconfig.path.rsplit('.', 1)[-1],
check_index=False,
)
2019-10-27 11:09:15 +01:00
kwargs['orig_value'] = value
2024-04-24 15:39:17 +02:00
validator.execute(lsubconfig,
**kwargs,
)
2019-11-20 08:26:41 +01:00
except ValueWarning as warn:
2024-06-20 12:56:27 +02:00
warnings.warn_explicit(ValueWarning(subconfig,
val,
2023-05-11 15:44:48 +02:00
self.get_type(),
2019-11-20 08:26:41 +01:00
self,
2023-05-11 15:44:48 +02:00
str(warn),
_index,
),
2019-11-20 08:26:41 +01:00
ValueWarning,
2023-05-11 15:44:48 +02:00
self.__class__.__name__, 319)
2017-07-24 20:39:01 +02:00
def do_validation(_value,
2023-05-11 15:44:48 +02:00
_index,
):
#
2024-04-24 15:39:17 +02:00
if _value is None:
return
2018-09-29 21:58:41 +02:00
if isinstance(_value, list):
2017-12-30 18:31:56 +01:00
raise ValueError(_('which must not be a list').format(_value,
2024-06-20 12:56:27 +02:00
self.impl_get_display_name(subconfig)),
2023-05-11 15:44:48 +02:00
)
2024-04-24 15:39:17 +02:00
if isinstance(_value, Calculation) and not subconfig:
2023-05-11 15:44:48 +02:00
return
2024-04-24 15:39:17 +02:00
# option validation
if check_error:
self.validate(_value)
self.validate_with_option(_value,
subconfig,
loaded=loaded,
2023-05-11 15:44:48 +02:00
)
2024-04-24 15:39:17 +02:00
# second level validation
if (check_error and not is_warnings_only) or (not check_error and is_warnings_only):
try:
self.second_level_validation(_value,
is_warnings_only)
except ValueError as err:
if is_warnings_only:
2024-06-20 12:56:27 +02:00
warnings.warn_explicit(ValueWarning(subconfig,
_value,
2024-04-24 15:39:17 +02:00
self.get_type(),
self,
str(err),
_index),
ValueWarning,
self.__class__.__name__, 0)
else:
raise err from err
# ?
if not loaded:
calculation_validator(_value,
_index,
)
2023-11-15 21:44:15 +01:00
val = value
err_index = force_index
2017-12-23 10:40:41 +01:00
try:
2018-08-01 08:37:58 +02:00
if not self.impl_is_multi():
2024-04-24 15:39:17 +02:00
do_validation(val,
None,
)
2017-12-23 10:40:41 +01:00
elif force_index is not None:
if self.impl_is_submulti():
2017-12-30 18:31:56 +01:00
if not isinstance(value, list):
raise ValueError(_('which must be a list'))
2019-02-13 22:49:27 +01:00
for val in value:
do_validation(val,
2023-05-11 15:44:48 +02:00
force_index,
)
2024-04-24 15:39:17 +02:00
_is_not_unique(value)
2017-12-23 10:40:41 +01:00
else:
do_validation(val,
2023-05-11 15:44:48 +02:00
force_index,
)
2024-04-24 15:39:17 +02:00
elif isinstance(value, Calculation) and not subconfig:
2019-09-28 16:32:48 +02:00
pass
2017-12-23 10:40:41 +01:00
elif self.impl_is_submulti():
2019-02-13 22:49:27 +01:00
for err_index, lval in enumerate(value):
2019-09-28 16:32:48 +02:00
if isinstance(lval, Calculation):
continue
2017-12-23 10:40:41 +01:00
if not isinstance(lval, list):
2017-12-30 18:31:56 +01:00
raise ValueError(_('which "{}" must be a list of list'
'').format(lval))
2017-12-23 10:40:41 +01:00
for val in lval:
do_validation(val,
2019-12-24 15:24:20 +01:00
err_index)
2024-04-24 15:39:17 +02:00
_is_not_unique(lval)
2023-11-15 21:44:15 +01:00
elif not isinstance(value, list):
raise ValueError(_('which must be a list'))
2017-12-19 23:11:45 +01:00
else:
2023-11-15 21:44:15 +01:00
# FIXME suboptimal, not several time for whole=True!
2019-02-13 22:49:27 +01:00
for err_index, val in enumerate(value):
do_validation(val,
2023-05-11 15:44:48 +02:00
err_index,
)
2024-04-24 15:39:17 +02:00
_is_not_unique(value)
2017-12-23 10:40:41 +01:00
except ValueError as err:
2024-04-24 15:39:17 +02:00
if not subconfig or \
'demoting_error_warning' not in subconfig.config_bag.properties:
2024-06-20 12:56:27 +02:00
raise ValueOptionError(subconfig,
val,
2023-05-11 15:44:48 +02:00
self.get_type(),
2024-04-24 15:39:17 +02:00
self,
2023-05-11 15:44:48 +02:00
str(err),
2021-03-06 19:23:35 +01:00
err_index) from err
2024-06-20 12:56:27 +02:00
warnings.warn_explicit(ValueErrorWarning(subconfig,
val,
2023-05-11 15:44:48 +02:00
self.get_type(),
2024-04-24 15:39:17 +02:00
self,
2023-05-11 15:44:48 +02:00
str(err),
2019-06-21 23:04:04 +02:00
err_index),
2018-12-08 00:02:23 +01:00
ValueErrorWarning,
self.__class__.__name__, 0)
return False
return True
def validate_with_option(self,
value: Any,
2024-04-24 15:39:17 +02:00
subconfig: "SubConfig",
*,
2023-05-11 15:44:48 +02:00
loaded: bool,
) -> None:
"""validation function with option
"""
2019-11-20 08:26:41 +01:00
def second_level_validation(self,
value: Any,
2023-05-11 15:44:48 +02:00
warnings_only: bool,
) -> None:
"""less import validation function
"""
2019-02-23 19:06:23 +01:00
def impl_is_leader(self):
2023-05-11 15:44:48 +02:00
"""check if option is a leader in a leadership
"""
2019-02-23 19:06:23 +01:00
leadership = self.impl_get_leadership()
if leadership is None:
return False
return leadership.is_leader(self)
2019-02-23 19:06:23 +01:00
def impl_is_follower(self):
2023-05-11 15:44:48 +02:00
"""check if option is a leader in a follower
"""
2019-02-23 19:06:23 +01:00
leadership = self.impl_get_leadership()
if leadership is None:
return False
2019-02-23 19:06:23 +01:00
return not leadership.is_leader(self)
def impl_get_leadership(self):
2023-05-11 15:44:48 +02:00
"""get leadership
"""
2019-02-23 19:06:23 +01:00
leadership = getattr(self, '_leadership', None)
if leadership is None:
return leadership
2023-05-11 15:44:48 +02:00
#pylint: disable=not-callable
2019-02-23 19:06:23 +01:00
return leadership()
2023-05-11 15:44:48 +02:00
def validate(self, value: Any):
"""option needs a validate function
"""
raise NotImplementedError()