tiramisu/tiramisu/api.py

1715 lines
66 KiB
Python
Raw Normal View History

2017-10-22 09:48:08 +02:00
# -*- coding: utf-8 -*-
2024-07-06 14:33:25 +02:00
# Copyright (C) 2017-2024 Team tiramisu (see AUTHORS for all contributors)
2017-10-22 09:48:08 +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/>.
# ____________________________________________________________
2023-05-11 15:44:48 +02:00
from inspect import getdoc
from typing import List, Set, Any, Optional, Callable, Dict
2019-11-19 18:39:44 +01:00
from warnings import catch_warnings, simplefilter
2019-12-24 15:24:20 +01:00
from functools import wraps
from copy import deepcopy
2018-04-28 08:39:07 +02:00
2023-05-11 15:44:48 +02:00
from .error import ConfigError, LeadershipError, ValueErrorWarning
2018-04-28 08:39:07 +02:00
from .i18n import _
2024-04-24 15:39:17 +02:00
from .setting import ConfigBag, owners, groups, undefined, \
2024-06-20 12:56:27 +02:00
FORBIDDEN_SET_PROPERTIES, SPECIAL_PROPERTIES, \
DEFAULT_PROPERTIES
2024-04-24 15:39:17 +02:00
from .config import KernelConfig, KernelGroupConfig, KernelMetaConfig, KernelMixConfig, SubConfig
2023-12-11 19:38:59 +01:00
from .option import RegexpOption, OptionDescription, ChoiceOption, Leadership
from .todict import TiramisuDict
from .autolib import Calculation
2017-12-13 22:15:34 +01:00
2024-06-20 12:56:27 +02:00
TIRAMISU_VERSION = 5
2017-12-13 22:15:34 +01:00
2018-04-07 20:15:19 +02:00
class TiramisuHelp:
2018-10-07 10:55:52 +02:00
_tmpl_help = ' {0}\t{1}'
2018-04-07 20:15:19 +02:00
def help(self,
2018-10-07 10:55:52 +02:00
_display: bool=True) -> List[str]:
def display(doc=''):
if _display: # pragma: no cover
print(doc)
2024-04-24 15:39:17 +02:00
all_modules = dir(self.__class__)
2018-10-07 10:55:52 +02:00
modules = []
max_len = 0
force = False
for module_name in all_modules:
2023-05-11 15:44:48 +02:00
if module_name in ['forcepermissive', 'unrestraint', 'nowarnings']:
2018-10-07 10:55:52 +02:00
force = True
max_len = max(max_len, len('forcepermissive'))
2020-08-04 16:35:40 +02:00
elif module_name != 'help' and not module_name.startswith('_'):
2018-10-07 10:55:52 +02:00
modules.append(module_name)
max_len = max(max_len, len(module_name))
modules.sort()
display(_(getdoc(self)))
display()
if force:
display(_('Settings:'))
2023-05-11 15:44:48 +02:00
display(self._tmpl_help.format('forcepermissive',
_('Access to option without verifying permissive '
'properties'),
).expandtabs(max_len + 10))
display(self._tmpl_help.format('unrestraint',
_('Access to option without property restriction')
).expandtabs(max_len + 10))
display(self._tmpl_help.format('nowarnings',
_('Do not warnings during validation')
).expandtabs(max_len + 10))
2018-10-07 10:55:52 +02:00
display()
display(_('Commands:'))
for module_name in modules:
module = getattr(self, module_name)
doc = _(getdoc(module))
display(self._tmpl_help.format(module_name, doc).expandtabs(max_len + 10))
display()
def __dir__(self):
if '_registers' in super().__dir__():
return list(self._registers.keys())
return super().__dir__()
2018-04-07 20:15:19 +02:00
class CommonTiramisu(TiramisuHelp):
2018-12-24 09:30:58 +01:00
_validate_properties = True
2018-01-01 21:32:39 +01:00
def _set_subconfig(self) -> None:
self._subconfig = self._config_bag.context.get_sub_config(self._config_bag,
self._path,
self._index,
validate_properties=False,
)
2018-01-01 21:32:39 +01:00
2023-04-27 11:34:35 +02:00
def option_type(typ):
if not isinstance(typ, list):
types = [typ]
else:
types = typ
def wrapper(func):
@wraps(func)
def wrapped(*args, **kwargs):
2023-05-11 15:44:48 +02:00
self = args[0]
config_bag = self._config_bag
if self._config_bag.context.impl_type == 'group' and 'group' in types:
2023-04-27 11:34:35 +02:00
options_bag = [OptionBag(None,
None,
2023-05-11 15:44:48 +02:00
self._config_bag,
path=self._path,
2023-04-27 11:34:35 +02:00
)]
kwargs['is_group'] = True
2023-05-11 15:44:48 +02:00
return func(self, options_bag, *args[1:], **kwargs)
2024-04-24 15:39:17 +02:00
option = self._subconfig.option
2024-06-20 12:56:27 +02:00
error_type = None
if 'dynamic' in types:
if not self._subconfig.is_dynamic:
error_type = 'only available for dynamic option'
elif option.impl_is_optiondescription():
if 'optiondescription' not in types:
if option.impl_is_leadership():
if 'leadership' not in types:
error_type = 'not available for a Leadership'
else:
error_type = 'not available for an OptionDescription'
elif option.impl_is_symlinkoption():
if 'symlink' not in types:
error_type = 'this function is not available for a SymLinkOption'
elif 'option' not in types:
if 'choice' in types:
if not isinstance(option, ChoiceOption):
error_type = 'only available for ChoiceOption'
elif option.impl_is_leader():
if 'leader' not in types:
error_type = 'not available for a Leader'
elif option.impl_is_follower():
if 'follower' not in types:
error_type = 'not available for a Follower'
else:
error_type = 'not available for an Option'
if not error_type:
2023-05-11 15:44:48 +02:00
if not option.impl_is_optiondescription() and \
not option.impl_is_symlinkoption() and \
option.impl_is_follower():
2023-06-26 19:25:57 +02:00
# default is "without_index"
2023-05-11 15:44:48 +02:00
if 'with_index' not in types and 'with_or_without_index' not in types and \
self._index is not None:
msg = _('please do not specify index '
f'({self.__class__.__name__}.{func.__name__})')
raise ConfigError(_(msg))
2023-06-26 19:25:57 +02:00
if self._index is None and 'with_index' in types:
2023-05-11 15:44:48 +02:00
msg = _('please specify index with a follower option '
f'({self.__class__.__name__}.{func.__name__})')
raise ConfigError(msg)
2024-06-20 12:56:27 +02:00
if self._validate_properties:
settings = self._config_bag.context.get_settings()
parent = self._subconfig.parent
if parent and parent.transitive_properties:
2024-06-20 12:56:27 +02:00
while parent:
if not parent.parent.transitive_properties:
2024-06-20 12:56:27 +02:00
settings.validate_properties(parent,
need_help=True,
)
break
parent = parent.parent
settings.validate_properties(self._subconfig,
need_help=True,
)
2024-04-24 15:39:17 +02:00
return func(self, *args[1:], **kwargs)
2023-05-11 15:44:48 +02:00
msg = _('please specify a valid sub function '
2024-06-20 12:56:27 +02:00
f'({self.__class__.__name__}.{func.__name__}): {error_type}')
2023-05-11 15:44:48 +02:00
raise ConfigError(msg)
2023-04-27 11:34:35 +02:00
wrapped.func = func
return wrapped
return wrapper
2018-01-01 21:32:39 +01:00
class CommonTiramisuOption(CommonTiramisu):
2018-12-24 09:30:58 +01:00
_validate_properties = False
def __init__(self,
2023-04-27 11:34:35 +02:00
path: str,
index: Optional[int],
config_bag: ConfigBag,
) -> None:
self._path = path
self._index = index
self._config_bag = config_bag
self._set_subconfig()
2017-10-22 09:48:08 +02:00
def __getattr__(self, subfunc):
2023-06-19 17:19:07 +02:00
raise ConfigError(_(f'please specify a valid sub function ({self.__class__.__name__}.{subfunc})'))
2017-10-22 09:48:08 +02:00
2022-10-01 19:44:48 +02:00
class _TiramisuOptionWalk:
2023-05-11 15:44:48 +02:00
def _list(self,
2024-04-24 15:39:17 +02:00
subconfig: SubConfig,
validate_properties: bool,
*,
uncalculated: bool=False,
2022-10-01 19:44:48 +02:00
):
2023-04-27 11:34:35 +02:00
options = []
for sub_subconfig in subconfig.get_children(validate_properties, uncalculated=uncalculated):
2024-04-24 15:39:17 +02:00
options.append(TiramisuOption(sub_subconfig.path,
sub_subconfig.index,
self._config_bag,
subconfig=sub_subconfig,
))
2023-04-27 11:34:35 +02:00
return options
2022-10-01 19:44:48 +02:00
class _TiramisuOptionOptionDescription:
2018-10-07 10:55:52 +02:00
"""Manage option"""
_validate_properties = False
2018-01-05 23:32:00 +01:00
2023-06-19 17:19:07 +02:00
@option_type(['optiondescription', 'option', 'with_or_without_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def get(self):
2018-10-07 10:55:52 +02:00
"""Get Tiramisu option"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option
2017-10-22 09:48:08 +02:00
2024-06-20 12:56:27 +02:00
@option_type(['optiondescription', 'option', 'with_or_without_index', 'symlink'])
def isoptiondescription(self):
"""Test if option is an optiondescription"""
return self._subconfig.option.impl_is_optiondescription()
2023-05-11 15:44:48 +02:00
@option_type(['optiondescription'])
2024-04-24 15:39:17 +02:00
def isleadership(self):
2019-02-23 19:06:23 +01:00
"""Test if option is a leader or a follower"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_is_leadership()
2017-10-22 09:48:08 +02:00
2024-06-20 12:56:27 +02:00
@option_type(['optiondescription', 'option', 'with_or_without_index', 'symlink'])
def description(self,
uncalculated: bool=False,
):
"""Get option description"""
if not uncalculated:
return self._subconfig.option.impl_get_display_name(self._subconfig)
2024-06-20 12:56:27 +02:00
return self._subconfig.option._get_information(self._subconfig,
'doc',
None,
)
2023-05-11 15:44:48 +02:00
@option_type(['optiondescription', 'option', 'symlink', 'with_or_without_index'])
def name(self,
*,
uncalculated: bool=False,
) -> str:
2018-10-07 10:55:52 +02:00
"""Get option name"""
if uncalculated:
return self._subconfig.option.impl_getname()
2024-04-24 15:39:17 +02:00
return self._subconfig.true_path.rsplit('.', 1)[-1]
2018-04-05 21:20:39 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['optiondescription', 'option', 'with_or_without_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def path(self,
*,
uncalculated: bool=False,
2024-04-24 15:39:17 +02:00
) -> str:
2018-10-07 10:55:52 +02:00
"""Get option path"""
if uncalculated:
return self._subconfig.option.impl_getpath()
2024-04-24 15:39:17 +02:00
return self._subconfig.true_path
2018-10-07 10:55:52 +02:00
2023-06-26 19:25:57 +02:00
@option_type(['optiondescription', 'option', 'symlink', 'with_or_without_index'])
2023-04-27 11:34:35 +02:00
def has_dependency(self,
self_is_dep=True,
) -> bool:
2018-10-07 10:55:52 +02:00
"""Test if option has dependency"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_has_dependency(self_is_dep)
2018-10-07 10:55:52 +02:00
2024-06-20 12:56:27 +02:00
@option_type(['optiondescription', 'option', 'symlink', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def dependencies(self):
2020-08-04 16:35:40 +02:00
"""Get dependencies from this option"""
options = []
2024-04-24 15:39:17 +02:00
for option in self._subconfig.option.get_dependencies(self._config_bag.context):
2020-08-04 16:35:40 +02:00
options.append(TiramisuOption(option().impl_getpath(),
None,
2023-04-27 11:34:35 +02:00
self._config_bag,
2020-08-04 16:35:40 +02:00
))
return options
2024-06-20 12:56:27 +02:00
@option_type(['option', 'optiondescription', 'symlink', 'with_or_without_index'])
def type(self):
"""Get de option type"""
option = self._subconfig.option
if option.impl_is_optiondescription():
return 'optiondescription'
return option.get_type()
2018-10-07 10:55:52 +02:00
2024-06-20 12:56:27 +02:00
@option_type(['option', 'optiondescription', 'symlink', 'with_or_without_index'])
def isdynamic(self,
*,
only_self: bool=False):
2024-06-20 12:56:27 +02:00
"""Test if option is a dynamic optiondescription"""
if not only_self:
return self._subconfig.is_dynamic
return self._subconfig.option.impl_is_optiondescription() and \
self._subconfig.option.impl_is_dynoptiondescription()
2024-06-20 12:56:27 +02:00
@option_type(['option', 'leadership'])
def leader(self):
"""Get the leader option for a leadership or a follower option"""
2024-06-20 12:56:27 +02:00
option = self._subconfig.option
if isinstance(option, Leadership):
leadership = self._subconfig
else:
leadership = self._subconfig.parent
leader_subconfig = leadership.get_child(leadership.option.get_leader(),
None,
False,
)
return TiramisuOption(leader_subconfig.path,
None,
self._config_bag,
subconfig=leader_subconfig,
)
@option_type(['leadership'])
def followers(self):
"""Get the followers option for a leadership"""
option = self._subconfig.option
if isinstance(option, Leadership):
leadership = self._subconfig
else:
leadership = self._subconfig.parent
ret = []
for follower in leadership.option.get_followers():
follower_subconfig = leadership.get_child(follower,
None,
False,
)
ret.append(TiramisuOption(follower_subconfig.path,
None,
self._config_bag,
subconfig=follower_subconfig,
))
return ret
2024-06-20 12:56:27 +02:00
@option_type(['dynamic', 'with_or_without_index'])
def suffixes(self,
only_self: bool=False,
uncalculated: bool=False,
):
2024-06-20 12:56:27 +02:00
"""Get suffixes for dynamic option"""
if not only_self:
return self._subconfig.suffixes
if not self._subconfig.option.impl_is_optiondescription() or \
not self._subconfig.option.impl_is_dynoptiondescription():
raise ConfigError(_(f'the option {self._subconfig.path} is not a dynamic option, cannot get suffixes with only_self parameter to True'))
return self._subconfig.option.get_suffixes(self._subconfig.parent,
uncalculated=uncalculated,
)
2019-03-02 21:31:21 +01:00
class _TiramisuOptionOption(_TiramisuOptionOptionDescription):
2018-10-07 10:55:52 +02:00
"""Manage option"""
2023-05-11 15:44:48 +02:00
@option_type(['option', 'symlink', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def ismulti(self):
2018-10-07 10:55:52 +02:00
"""Test if option could have multi value"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_is_multi()
2018-10-07 10:55:52 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'symlink', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def issubmulti(self):
2018-10-07 10:55:52 +02:00
"""Test if option could have submulti value"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_is_submulti()
2018-10-07 10:55:52 +02:00
2024-06-20 12:56:27 +02:00
@option_type(['option', 'with_or_without_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def isleader(self):
2019-02-23 19:06:23 +01:00
"""Test if option is a leader"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_is_leader()
2018-10-07 10:55:52 +02:00
2023-06-19 17:19:07 +02:00
@option_type(['option', 'with_or_without_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def isfollower(self):
2019-02-23 19:06:23 +01:00
"""Test if option is a follower"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_is_follower()
2023-05-11 15:44:48 +02:00
@option_type(['option', 'symlink', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def issymlinkoption(self) -> bool:
2023-04-27 11:34:35 +02:00
"""Test if option is a symlink option"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_is_symlinkoption()
2018-12-24 09:30:58 +01:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'with_or_without_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def default(self):
2018-10-07 10:55:52 +02:00
"""Get default value for an option (not for optiondescription)"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_getdefault()
2017-10-22 09:48:08 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'with_or_without_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def defaultmulti(self):
2018-10-07 10:55:52 +02:00
"""Get default value when added a value for a multi option (not for optiondescription)"""
2024-04-24 15:39:17 +02:00
if not self._subconfig.option.impl_is_multi():
2023-05-11 15:44:48 +02:00
raise ConfigError(_('only multi value has defaultmulti'))
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_getdefault_multi()
2023-06-26 19:25:57 +02:00
@option_type(['option', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def pattern(self) -> str:
2023-04-27 11:34:35 +02:00
"""Get the option pattern"""
2024-04-24 15:39:17 +02:00
option = self._subconfig.option
2019-04-22 10:51:44 +02:00
type = option.get_type()
if isinstance(option, RegexpOption):
return option._regexp.pattern
if type == 'integer':
2019-05-09 20:32:43 +02:00
# FIXME negative too!
2019-04-22 10:51:44 +02:00
return r'^[0-9]+$'
if type == 'domain name':
2019-04-22 10:51:44 +02:00
return option.impl_get_extra('_domain_re').pattern
if type in ['ip', 'network', 'netmask']:
2019-04-22 10:51:44 +02:00
#FIXME only from 0.0.0.0 to 255.255.255.255
return r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
2024-06-20 12:56:27 +02:00
@option_type(['option', 'with_or_without_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def index(self):
2024-06-20 12:56:27 +02:00
"""Get index of option"""
2024-04-24 15:39:17 +02:00
return self._subconfig.index
2023-04-12 11:14:40 +02:00
2024-06-20 12:56:27 +02:00
@option_type(['symlink', 'optiondescription'])
def option(self, *args, **kwargs):
"""For OptionDescription get sub option, for symlinkoption get the linked option"""
if self._subconfig.option.impl_is_optiondescription():
return self._option_description(*args, **kwargs)
return self._option_symlink(*args, **kwargs)
def _option_description(self,
path,
index=None,
):
sub_path = self._path + '.' + path
return TiramisuOption(sub_path,
index,
self._config_bag,
)
2024-06-20 12:56:27 +02:00
def _option_symlink(self):
subconfig = self._subconfig.config_bag.context._get(self._subconfig,
need_help=True,
validate_properties=self._validate_properties,
)
2024-08-07 08:55:43 +02:00
if isinstance(subconfig, list):
raise ConfigError(_('cannot get option from a follower symlink without index'))
subconfig.true_path = subconfig.path
2024-06-20 12:56:27 +02:00
return TiramisuOption(subconfig.path,
subconfig.index,
self._config_bag,
subconfig=subconfig,
)
class TiramisuOptionOwner(CommonTiramisuOption):
2018-10-07 10:55:52 +02:00
"""Manage option's owner"""
2024-06-20 12:56:27 +02:00
_validate_properties=True
2017-10-22 09:48:08 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['symlink', 'option', 'with_index'])
2024-04-24 15:39:17 +02:00
def get(self):
2018-10-07 10:55:52 +02:00
"""Get owner for a specified option"""
2024-04-24 15:39:17 +02:00
return self._config_bag.context.get_owner(self._subconfig)
2023-05-11 15:44:48 +02:00
@option_type(['symlink', 'option', 'with_index'])
2024-04-24 15:39:17 +02:00
def isdefault(self):
2018-10-07 10:55:52 +02:00
"""Is option has defaut value"""
2024-04-24 15:39:17 +02:00
return self._config_bag.context.get_owner(self._subconfig) == owners.default
2017-10-22 09:48:08 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'with_index'])
2023-04-27 11:34:35 +02:00
def set(self,
owner: str,
) -> None:
2018-10-07 10:55:52 +02:00
"""Get owner for a specified option"""
2017-10-22 09:48:08 +02:00
try:
obj_owner = getattr(owners, owner)
except AttributeError:
owners.addowner(owner)
obj_owner = getattr(owners, owner)
2024-04-24 15:39:17 +02:00
self._config_bag.context.get_values().set_owner(self._subconfig,
2023-04-27 11:34:35 +02:00
obj_owner,
)
2017-10-22 09:48:08 +02:00
class TiramisuOptionProperty(CommonTiramisuOption):
2018-10-07 10:55:52 +02:00
"""Manage option's property"""
2019-12-24 15:24:20 +01:00
_validate_properties = False
2024-06-20 12:56:27 +02:00
@option_type(['option', 'optiondescription', 'with_index', 'symlink'])
def get(self,
*,
2024-06-20 12:56:27 +02:00
only_raises: bool=False,
apply_requires: bool=True,
uncalculated: bool=False,
2023-04-27 11:34:35 +02:00
):
2018-10-07 10:55:52 +02:00
"""Get properties for an option"""
2023-04-27 11:34:35 +02:00
settings = self._config_bag.context.get_settings()
2024-02-08 08:32:24 +01:00
if not only_raises:
2024-06-20 12:56:27 +02:00
return settings.getproperties(self._subconfig,
uncalculated=uncalculated,
apply_requires=apply_requires,
)
2024-04-24 15:39:17 +02:00
return settings.calc_raises_properties(self._subconfig,
2024-02-08 08:32:24 +01:00
uncalculated=uncalculated,
2024-06-20 12:56:27 +02:00
apply_requires=apply_requires,
2024-02-08 08:32:24 +01:00
)
2017-10-22 09:48:08 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'optiondescription', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def add(self, prop):
2018-10-07 10:55:52 +02:00
"""Add new property for an option"""
if prop in FORBIDDEN_SET_PROPERTIES:
raise ConfigError(_('cannot add this property: "{0}"').format(
' '.join(prop)))
2023-04-27 11:34:35 +02:00
settings = self._config_bag.context.get_settings()
2024-06-20 12:56:27 +02:00
props = settings.get_personalize_properties(self._path,
self._index,
)
2024-04-24 15:39:17 +02:00
settings.setproperties(self._subconfig,
2023-04-27 11:34:35 +02:00
props | {prop},
)
2019-12-24 15:24:20 +01:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'optiondescription', 'with_or_without_index'])
2023-04-27 11:34:35 +02:00
def remove(self,
prop,
):
2018-10-07 10:55:52 +02:00
"""Remove new property for an option"""
2024-02-08 08:32:24 +01:00
settings = self._config_bag.context.get_settings()
2024-06-20 12:56:27 +02:00
props = settings.get_personalize_properties(self._path,
self._index,
)
if prop not in props:
if prop in settings.getproperties(self._subconfig):
msg = f'cannot remove option\'s property "{prop}", use permissive instead'
else:
msg = f'cannot find "{prop}"'
msg += f' in option "{self._path}"'
if self._index is not None:
msg += f' at index "{self._index}"'
raise ConfigError(msg)
2024-04-24 15:39:17 +02:00
settings.setproperties(self._subconfig,
2024-02-08 08:32:24 +01:00
props - {prop},
)
2019-12-24 15:24:20 +01:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'optiondescription', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def reset(self):
2018-10-07 10:55:52 +02:00
"""Reset all personalised properties"""
2024-04-24 15:39:17 +02:00
self._config_bag.context.get_settings().reset(self._subconfig)
2017-10-22 09:48:08 +02:00
class TiramisuOptionPermissive(CommonTiramisuOption):
2018-10-07 10:55:52 +02:00
"""Manage option's permissive"""
2024-06-20 12:56:27 +02:00
_validate_properties = False
2023-06-19 17:19:07 +02:00
@option_type(['option', 'optiondescription', 'symlink', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def get(self):
2018-10-07 10:55:52 +02:00
"""Get permissives value"""
2024-04-24 15:39:17 +02:00
return self._config_bag.context.get_settings().getpermissives(self._subconfig)
2023-05-11 15:44:48 +02:00
@option_type(['option', 'optiondescription', 'with_or_without_index'])
2024-06-20 12:56:27 +02:00
def add(self,
permissive,
2023-04-27 11:34:35 +02:00
):
2018-10-07 10:55:52 +02:00
"""Set permissives value"""
2024-06-20 12:56:27 +02:00
permissives = self._config_bag.context.get_settings().getpermissives(self._subconfig)
self._config_bag.context.get_settings().setpermissives(self._subconfig,
frozenset(permissives | {permissive}),
)
@option_type(['option', 'optiondescription', 'with_or_without_index'])
def remove(self, permissive):
"""Remove a config property"""
permissives = set(self.get())
if permissive not in permissives:
msg = f'cannot find "{permissive}"'
raise ConfigError(msg)
2024-04-24 15:39:17 +02:00
self._config_bag.context.get_settings().setpermissives(self._subconfig,
2024-06-20 12:56:27 +02:00
frozenset(permissives - {permissive}),
2023-04-27 11:34:35 +02:00
)
2024-06-20 12:56:27 +02:00
@option_type(['option', 'optiondescription', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def reset(self):
2018-10-07 10:55:52 +02:00
"""Reset all personalised permissive"""
2024-04-24 15:39:17 +02:00
self._config_bag.context.get_settings().reset_permissives(self._subconfig)
2017-12-13 22:15:34 +01:00
class TiramisuOptionInformation(CommonTiramisuOption):
2018-10-07 10:55:52 +02:00
"""Manage option's informations"""
2024-06-20 12:56:27 +02:00
_validate_properties = False
2017-12-13 22:15:34 +01:00
2023-06-19 17:19:07 +02:00
@option_type(['option', 'optiondescription', 'with_or_without_index', 'symlink'])
2023-04-27 11:34:35 +02:00
def get(self,
2023-05-11 15:44:48 +02:00
name: str,
2023-04-27 11:34:35 +02:00
default=undefined,
) -> Any:
2018-10-07 10:55:52 +02:00
"""Get information"""
2024-06-20 12:56:27 +02:00
return self._config_bag.context.get_values().get_information(self._subconfig,
name,
default,
)
2017-12-13 22:15:34 +01:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'optiondescription'])
2023-04-27 11:34:35 +02:00
def set(self,
key: str,
value: Any) -> None:
2018-10-07 10:55:52 +02:00
"""Set information"""
2024-04-24 15:39:17 +02:00
self._config_bag.context.get_values().set_information(self._subconfig,
2023-04-27 11:34:35 +02:00
key,
value,
)
2018-04-07 20:15:19 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'optiondescription'])
2024-06-20 12:56:27 +02:00
def remove(self,
key: str,
) -> None:
2018-10-07 10:55:52 +02:00
"""Remove information"""
2023-04-27 11:34:35 +02:00
self._config_bag.context.get_values().del_information(key,
2024-04-24 15:39:17 +02:00
path=self._path,
2023-04-27 11:34:35 +02:00
)
2018-04-07 20:15:19 +02:00
2023-06-19 17:19:07 +02:00
@option_type(['option', 'optiondescription', 'with_or_without_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def list(self) -> list:
2018-10-07 10:55:52 +02:00
"""List information's keys"""
2024-06-20 12:56:27 +02:00
lst1 = set(self._subconfig.option._list_information())
2024-04-24 15:39:17 +02:00
lst2 = set(self._config_bag.context.get_values().list_information(self._path))
return lst1 | lst2
2019-12-24 15:24:20 +01:00
2024-04-24 15:39:17 +02:00
class _TiramisuODGet():
def _od_get(self,
root_subconfig: SubConfig,
) -> dict:
"""exports the whole config into a `dict`
:returns: dict of Option's name (or path) and values
"""
def parse_od_get(values):
ret_ = {}
for subconfig, value in values.items():
option = TiramisuOption(subconfig.path,
subconfig.index,
self._config_bag,
subconfig=subconfig,
)
if option.isoptiondescription():
value = parse_od_get(value)
ret_[option] = value
return ret_
return parse_od_get(self._config_bag.context.walk(root_subconfig))
class TiramisuOptionValue(CommonTiramisuOption, _TiramisuODGet):
2019-12-24 15:24:20 +01:00
"""Manage option's value"""
_validate_properties = True
@option_type(['option', 'symlink', 'with_index', 'optiondescription'])
def get(self,
*,
uncalculated: bool=False,
):
2024-04-24 15:39:17 +02:00
"""Get value for an option or option and sub option with values with optiondescription"""
if self._subconfig.option.impl_is_optiondescription():
if uncalculated:
raise ConfigError('uncalculated is not allowed for optiondescription')
2024-04-24 15:39:17 +02:00
return self._od_get(self._subconfig)
if uncalculated:
value = self._subconfig.option.impl_getdefault()
index = self._subconfig.index
if not isinstance(value, list) or index is None:
return value
if index >= len(value):
return self._subconfig.option.impl_getdefault_multi()
return value[index]
return self._get(uncalculated)
2023-04-27 11:34:35 +02:00
def _get(self,
2024-04-24 15:39:17 +02:00
need_help: bool=True,
2023-04-27 11:34:35 +02:00
):
2018-10-07 10:55:52 +02:00
"""Get option's value"""
2024-04-24 15:39:17 +02:00
return self._config_bag.context.get_value(self._subconfig, need_help)
2018-08-02 22:35:40 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'with_index'])
2023-04-27 11:34:35 +02:00
def set(self,
value,
):
2018-10-07 10:55:52 +02:00
"""Change option's value"""
2024-04-24 15:39:17 +02:00
option = self._subconfig.option
if not isinstance(value, Calculation) and option.impl_is_leader() and \
len(value) < self._subconfig.parent.get_length_leadership():
2023-04-27 11:34:35 +02:00
raise LeadershipError(_('cannot reduce length of the leader "{}"'
2024-06-20 12:56:27 +02:00
'').format(option.impl_get_display_name(self._subconfig)))
values = self._config_bag.context.get_values()
return values.set_value(self._subconfig,
value
)
2018-10-07 10:55:52 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['group', 'option', 'with_index'])
def reset(self,
is_group: bool=False,
) -> None:
2018-10-07 10:55:52 +02:00
"""Reset value for an option"""
2019-12-24 15:24:20 +01:00
if is_group:
2024-04-24 15:39:17 +02:00
self._config_bag.context.reset(self._subconfig.path,
2023-04-27 11:34:35 +02:00
self._config_bag,
)
2019-12-24 15:24:20 +01:00
else:
2023-05-11 15:44:48 +02:00
values = self._config_bag.context.get_values()
2024-04-24 15:39:17 +02:00
if self._subconfig.index is not None:
values.reset_follower(self._subconfig)
2023-05-11 15:44:48 +02:00
else:
2024-04-24 15:39:17 +02:00
values.reset(self._subconfig)
2017-10-22 09:48:08 +02:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'with_index', 'symlink'])
2024-04-24 15:39:17 +02:00
def default(self):
2018-11-13 12:39:51 +01:00
"""Get default value (default of option or calculated value)"""
2024-04-24 15:39:17 +02:00
return self._config_bag.context.get_values().get_default_value(self._subconfig)
2018-11-13 12:39:51 +01:00
2023-05-11 15:44:48 +02:00
@option_type(['option', 'with_index'])
2024-04-24 15:39:17 +02:00
def valid(self):
2023-04-27 11:34:35 +02:00
"""The if the option's value is valid"""
try:
2019-11-19 18:39:44 +01:00
with catch_warnings(record=True) as warns:
simplefilter("always", ValueErrorWarning)
2024-04-24 15:39:17 +02:00
self._get(self._subconfig)
for warn in warns:
if isinstance(warn.message, ValueErrorWarning):
return False
except ValueError:
return False
return True
2024-06-20 12:56:27 +02:00
@option_type(['choice', 'with_index'])
def list(self,
*,
uncalculated: bool=False,
):
2019-11-19 18:39:44 +01:00
"""All values available for a ChoiceOption"""
return self._subconfig.option.impl_get_values(self._subconfig,
uncalculated,
)
2019-11-19 18:39:44 +01:00
2019-12-24 15:24:20 +01:00
@option_type('leader')
2023-04-27 11:34:35 +02:00
def pop(self,
2023-05-11 15:44:48 +02:00
index: int,
):
2018-10-07 10:55:52 +02:00
"""Pop a value"""
2024-04-24 15:39:17 +02:00
self._config_bag.context.get_values().reset_leadership(self._subconfig,
2023-05-11 15:44:48 +02:00
index,
)
2023-01-24 22:31:32 +01:00
2023-05-11 15:44:48 +02:00
@option_type(['leader', 'follower', 'with_or_without_index'])
2024-04-24 15:39:17 +02:00
def len(self):
2024-06-20 12:56:27 +02:00
"""Length for a leadership"""
2024-04-24 15:39:17 +02:00
return self._subconfig.parent.get_length_leadership()
2018-10-07 10:55:52 +02:00
def _registers(_registers: Dict[str, type],
prefix: str,
2023-05-11 15:44:48 +02:00
):
2017-12-13 22:15:34 +01:00
for module_name in globals().keys():
if module_name != prefix and module_name.startswith(prefix):
2017-12-13 22:15:34 +01:00
module = globals()[module_name]
func_name = module_name[len(prefix):].lower()
2018-10-07 10:55:52 +02:00
_registers[func_name] = module
2019-12-24 15:24:20 +01:00
#__________________________________________________________________________________________________
#
2017-12-13 22:15:34 +01:00
2017-10-22 09:48:08 +02:00
class TiramisuConfig(TiramisuHelp, _TiramisuOptionWalk):
2019-12-24 15:24:20 +01:00
def __init__(self,
config_bag: ConfigBag,
2024-04-24 15:39:17 +02:00
orig_config_bags: Optional[List["OptionBag"]],
2023-04-27 11:34:35 +02:00
) -> None:
2019-12-24 15:24:20 +01:00
self._config_bag = config_bag
self._orig_config_bags = orig_config_bags
def _return_config(self, config):
2019-12-24 15:24:20 +01:00
if isinstance(config, KernelConfig):
return Config(config)
2019-12-24 15:24:20 +01:00
if isinstance(config, KernelMetaConfig):
return MetaConfig(config)
2019-12-24 15:24:20 +01:00
if isinstance(config, KernelMixConfig):
return MixConfig([], config)
2019-12-24 15:24:20 +01:00
if isinstance(config, KernelGroupConfig):
return GroupConfig(config)
2019-12-24 15:24:20 +01:00
def name(self):
"""get the name"""
return self._config_bag.context.impl_getname()
2019-12-24 15:24:20 +01:00
2024-04-24 15:39:17 +02:00
class TiramisuOption(CommonTiramisu,
_TiramisuOptionOption,
TiramisuConfig,
):
2018-10-07 10:55:52 +02:00
"""Manage selected option"""
_validate_properties = False
2018-10-07 10:55:52 +02:00
_registers = {}
2017-11-12 20:11:56 +01:00
def __init__(self,
2018-04-28 08:39:07 +02:00
path: Optional[str]=None,
index: Optional[int]=None,
2023-04-27 11:34:35 +02:00
config_bag: Optional[ConfigBag]=None,
2024-04-24 15:39:17 +02:00
*,
subconfig: Optional[SubConfig]=None,
2023-04-27 11:34:35 +02:00
) -> None:
self._path = path
self._index = index
self._config_bag = config_bag
if subconfig is None:
self._set_subconfig()
else:
self._subconfig = subconfig
self._tiramisu_dict = None
2018-10-07 10:55:52 +02:00
if not self._registers:
_registers(self._registers, 'TiramisuOption')
2017-10-22 09:48:08 +02:00
2024-04-24 15:39:17 +02:00
def __repr__(self):
2024-06-20 12:56:27 +02:00
msg = f'<TiramisuOption path="{self._path}"'
2024-04-24 15:39:17 +02:00
if self._index is not None:
msg += f', index={self._index}'
msg += '>'
return msg
2018-04-28 08:39:07 +02:00
def __getattr__(self, subfunc: str) -> Any:
2018-10-07 10:55:52 +02:00
if subfunc in self._registers:
2023-04-27 11:34:35 +02:00
return self._registers[subfunc](self._path,
self._index,
self._config_bag,
)
2024-06-20 12:56:27 +02:00
raise ConfigError(_(f'please specify a valid sub function ({self.__class__.__name__}.{subfunc}) for {self._path}'))
2024-04-24 15:39:17 +02:00
#
def __iter__(self):
for sub_subconfig in self._subconfig.get_children(True):
yield TiramisuOption(sub_subconfig.path,
sub_subconfig.index,
self._config_bag,
subconfig=sub_subconfig,
)
2019-08-03 21:03:23 +02:00
2019-12-24 15:24:20 +01:00
@option_type('optiondescription')
2024-04-24 15:39:17 +02:00
def group_type(self):
2018-10-07 10:55:52 +02:00
"""Get type for an optiondescription (only for optiondescription)"""
2024-04-24 15:39:17 +02:00
return self._subconfig.option.impl_get_group_type()
2019-12-24 15:24:20 +01:00
@option_type('optiondescription')
def list(self,
*,
2024-04-24 15:39:17 +02:00
validate_properties: bool=True,
uncalculated: bool=False,
2023-04-27 11:34:35 +02:00
):
2023-06-19 17:19:07 +02:00
"""List options inside an option description (by default list only option)"""
2024-04-24 15:39:17 +02:00
return self._list(self._subconfig,
validate_properties,
uncalculated=uncalculated,
2023-05-11 15:44:48 +02:00
)
def _load_dict(self,
clearable: str="all",
remotable: str="minimum",
):
2023-06-19 17:19:07 +02:00
config = self._config_bag.context
self._tiramisu_dict = TiramisuDict(self._return_config(config),
2023-06-19 17:19:07 +02:00
root=self._path,
2019-08-03 21:03:23 +02:00
clearable=clearable,
remotable=remotable)
2019-12-24 15:24:20 +01:00
@option_type('optiondescription')
def dict(self,
clearable: str="all",
remotable: str="minimum",
form: List=[],
2023-04-27 11:34:35 +02:00
force: bool=False,
) -> Dict:
2022-11-13 15:04:12 +01:00
"""Convert config and option to tiramisu format"""
if force or self._tiramisu_dict is None:
self._load_dict(clearable, remotable)
return self._tiramisu_dict.todict(form)
2019-12-24 15:24:20 +01:00
@option_type('optiondescription')
def updates(self,
2023-04-27 11:34:35 +02:00
body: List,
) -> Dict:
2022-11-13 15:04:12 +01:00
"""Updates value with tiramisu format"""
if self._tiramisu_dict is None: # pragma: no cover
self._load_dict()
return self._tiramisu_dict.set_updates(body)
2020-01-22 20:46:18 +01:00
2019-08-03 21:03:23 +02:00
class TiramisuContextInformation(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Manage config informations"""
def get(self,
name,
default=undefined,
):
2018-10-07 10:55:52 +02:00
"""Get an information"""
2024-06-20 12:56:27 +02:00
context = self._config_bag.context
values = context.get_values()
subconfig = context.get_root(self._config_bag)
return values.get_information(subconfig,
name,
default,
)
2017-12-13 22:15:34 +01:00
def set(self,
name,
value,
):
2018-10-07 10:55:52 +02:00
"""Set an information"""
self._config_bag.context.impl_set_information(self._config_bag,
name,
value,
)
2024-06-20 12:56:27 +02:00
def remove(self,
name,
):
2018-10-07 10:55:52 +02:00
"""Remove an information"""
self._config_bag.context.impl_del_information(name)
2018-10-07 10:55:52 +02:00
def list(self):
2018-10-07 10:55:52 +02:00
"""List information's keys"""
2024-06-20 12:56:27 +02:00
lst1 = set(self._config_bag.context.get_description()._list_information())
lst2 = set(self._config_bag.context.impl_list_information())
return lst1 | lst2
2017-12-13 22:15:34 +01:00
def exportation(self):
2021-05-18 18:53:14 +02:00
"""Export all informations"""
return deepcopy(self._config_bag.context.get_values()._informations)
2021-05-18 18:53:14 +02:00
def importation(self, informations):
2021-05-18 18:53:14 +02:00
"""Import informations"""
self._config_bag.context.get_values()._informations = deepcopy(informations)
2021-05-18 18:53:14 +02:00
2017-12-13 22:15:34 +01:00
2024-04-24 15:39:17 +02:00
class TiramisuContextValue(TiramisuConfig, _TiramisuODGet):
2018-10-07 10:55:52 +02:00
"""Manage config value"""
def mandatory(self):
2018-10-07 10:55:52 +02:00
"""Return path of options with mandatory property without any value"""
2023-04-27 11:34:35 +02:00
config_bag = self._config_bag.copy()
config_bag.properties -= {'mandatory', 'empty', 'warnings'}
config_bag.set_permissive()
2024-04-24 15:39:17 +02:00
root = self._config_bag.context.get_root(config_bag)
2019-12-24 15:24:20 +01:00
options = []
2024-04-24 15:39:17 +02:00
for subconfig in self._config_bag.context.walk(root,
only_mandatory=True,
):
options.append(TiramisuOption(subconfig.path,
subconfig.index,
2023-04-27 11:34:35 +02:00
self._config_bag,
2024-04-24 15:39:17 +02:00
subconfig=subconfig,
2023-04-27 11:34:35 +02:00
))
2019-12-24 15:24:20 +01:00
return options
2017-12-13 22:15:34 +01:00
2019-11-19 18:39:44 +01:00
# FIXME should be only for group/meta
def set(self,
path: str,
value: Any,
only_config=undefined,
force_default=undefined,
force_default_if_same=undefined,
force_dont_change_value=undefined,
):
2018-10-07 10:55:52 +02:00
"""Set a value in config or children for a path"""
2018-01-03 21:07:51 +01:00
kwargs = {}
if only_config is not undefined:
kwargs['only_config'] = only_config
if force_default is not undefined:
kwargs['force_default'] = force_default
if force_default_if_same is not undefined:
kwargs['force_default_if_same'] = force_default_if_same
if force_dont_change_value is not undefined:
kwargs['force_dont_change_value'] = force_dont_change_value
2023-04-27 11:34:35 +02:00
option_bag = OptionBag(None,
None,
self._config_bag,
path=path,
)
return self._config_bag.context.set_value(option_bag,
value,
**kwargs,
)
2018-10-31 16:08:22 +01:00
2019-11-19 18:39:44 +01:00
# FIXME should be only for group/meta
def reset(self,
2018-10-31 16:08:22 +01:00
path: str,
only_children: bool=False):
"""Reset value"""
self._config_bag.context.reset(path,
only_children,
self._config_bag,
)
def get(self):
2024-04-24 15:39:17 +02:00
"""Get option and sub option with values"""
root = self._config_bag.context.get_root(self._config_bag)
return self._od_get(root)
def exportation(self,
with_default_owner: bool=False,
):
2018-10-07 10:55:52 +02:00
"""Export all values"""
exportation = deepcopy(self._config_bag.context.get_values()._values)
if not with_default_owner:
del exportation[None]
return exportation
2018-04-04 16:47:28 +02:00
def importation(self, values):
2018-10-07 10:55:52 +02:00
"""Import values"""
cvalues = self._config_bag.context.get_values()
if None not in values:
current_owner = cvalues.get_context_owner()
cvalues._values = deepcopy(values)
self._config_bag.context.reset_cache(None, None)
if None not in values:
cvalues._values[None] = {None: [None, current_owner]}
2017-12-13 22:15:34 +01:00
2019-08-03 21:03:23 +02:00
class TiramisuContextOwner(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Global owner"""
def get(self):
2018-10-07 10:55:52 +02:00
"""Get owner"""
return self._config_bag.context.get_values().get_context_owner()
def set(self, owner):
2018-10-07 10:55:52 +02:00
"""Set owner"""
2017-12-23 12:29:45 +01:00
try:
obj_owner = getattr(owners, owner)
except AttributeError:
owners.addowner(owner)
obj_owner = getattr(owners, owner)
values = self._config_bag.context.get_values()
values.set_context_owner(obj_owner)
2017-12-23 12:29:45 +01:00
2024-06-20 12:56:27 +02:00
class PropertyPermissive:
def _set_default_permissive(self,
settings,
):
or_properties = settings.rw_append - settings.ro_append - SPECIAL_PROPERTIES
permissives = frozenset(settings.get_context_permissives() | or_properties)
settings.set_context_permissives(permissives)
def _reset_config_properties(self,
settings,
):
properties = settings.get_context_properties()
permissives = settings.get_context_permissives()
self._config_bag.properties = properties
self._config_bag.permissives = permissives
if self._orig_config_bags:
for config_bag in self._orig_config_bags:
config_bag.properties = properties
config_bag.permissives = permissives
class TiramisuContextProperty(TiramisuConfig, PropertyPermissive):
2018-10-07 10:55:52 +02:00
"""Manage config properties"""
def read_only(self):
2018-10-07 10:55:52 +02:00
"""Set config to read only mode"""
2024-06-20 12:56:27 +02:00
if self._config_bag.is_unrestraint:
raise ConfigError('cannot change context property in unrestraint mode')
2019-02-21 19:33:39 +01:00
old_props = self._config_bag.properties
settings = self._config_bag.context.get_settings()
settings.read_only(self._config_bag)
2024-06-20 12:56:27 +02:00
self._set_default_permissive(settings)
self._reset_config_properties(settings)
if 'force_store_value' not in old_props and \
'force_store_value' in self._config_bag.properties:
self._force_store_value()
2017-12-13 22:15:34 +01:00
def read_write(self):
2018-10-07 10:55:52 +02:00
"""Set config to read and write mode"""
2024-06-20 12:56:27 +02:00
if self._config_bag.is_unrestraint:
raise ConfigError('cannot change context property in unrestraint mode')
2019-02-21 19:33:39 +01:00
old_props = self._config_bag.properties
settings = self._config_bag.context.get_settings()
settings.read_write(self._config_bag)
2024-06-20 12:56:27 +02:00
self._set_default_permissive(settings)
self._reset_config_properties(settings)
if 'force_store_value' not in old_props and \
'force_store_value' in self._config_bag.properties:
self._force_store_value()
2017-12-13 22:15:34 +01:00
def add(self, prop):
2018-10-07 10:55:52 +02:00
"""Add a config property"""
props = set(self.get())
2020-01-22 20:46:18 +01:00
if prop not in props:
props.add(prop)
self._set(frozenset(props))
2017-12-13 22:15:34 +01:00
def remove(self, prop):
2018-10-07 10:55:52 +02:00
"""Remove a config property"""
props = set(self.get())
2024-06-20 12:56:27 +02:00
if prop not in props:
msg = f'cannot find "{prop}"'
raise ConfigError(msg)
props.remove(prop)
self._set(frozenset(props))
2017-12-13 22:15:34 +01:00
2024-06-20 12:56:27 +02:00
def get(self,
*,
2024-06-20 12:56:27 +02:00
only_raises: bool=False,
apply_requires: bool=True,
uncalculated: bool=False,
) -> Set:
2018-10-07 10:55:52 +02:00
"""Get all config properties"""
2024-06-20 12:56:27 +02:00
if only_raises:
return set()
return self._config_bag.properties
2017-12-13 22:15:34 +01:00
def _set(self,
props,
):
2018-10-07 10:55:52 +02:00
"""Personalise config properties"""
2024-06-20 12:56:27 +02:00
if self._config_bag.is_unrestraint:
raise ConfigError('cannot change context property in unrestraint mode')
if 'force_store_value' in props:
force_store_value = 'force_store_value' not in self._config_bag.properties
else:
force_store_value = False
settings = self._config_bag.context.get_settings()
settings.set_context_properties(props,
self._config_bag.context,
)
self._reset_config_properties(settings)
if force_store_value:
self._force_store_value()
2017-12-13 22:15:34 +01:00
def reset(self):
2018-10-07 10:55:52 +02:00
"""Remove config properties"""
2024-06-20 12:56:27 +02:00
if self._config_bag.is_unrestraint:
raise ConfigError('cannot change context property in unrestraint mode')
settings = self._config_bag.context.get_settings()
settings.reset(self._config_bag)
self._reset_config_properties(settings)
2017-12-23 20:21:07 +01:00
def exportation(self):
2018-10-07 10:55:52 +02:00
"""Export config properties"""
settings = self._config_bag.context.get_settings()
return {'properties': deepcopy(settings._properties),
'ro_append': settings.ro_append.copy(),
'ro_remove': settings.ro_remove.copy(),
'rw_append': settings.rw_append.copy(),
'rw_remove': settings.rw_remove.copy(),
}
def importation(self, data):
2018-10-07 10:55:52 +02:00
"""Import config properties"""
if self._config_bag.is_unrestraint:
raise ConfigError('cannot change context property in unrestraint mode')
properties = data['properties']
2024-06-20 12:56:27 +02:00
if 'force_store_value' in properties.get(None, {}).get(None, []):
force_store_value = 'force_store_value' not in self._config_bag.properties
else:
force_store_value = False
context = self._config_bag.context
settings = context.get_settings()
settings._properties = deepcopy(properties)
settings.ro_append = data['ro_append'].copy()
settings.ro_remove = data['ro_remove'].copy()
2024-08-07 08:55:43 +02:00
settings.rw_append = data['rw_append'].copy()
settings.rw_remove = data['rw_remove'].copy()
2024-06-20 12:56:27 +02:00
context.reset_cache(None, None)
self._reset_config_properties(settings)
if force_store_value:
self._force_store_value()
def _force_store_value(self):
descr = self._config_bag.context.get_description()
descr.impl_build_force_store_values(self._config_bag)
def setdefault(self,
properties: Set[str],
2024-06-20 12:56:27 +02:00
type: Optional[str],
when: Optional[str]=None) -> None:
if not isinstance(properties, frozenset):
raise TypeError(_('properties must be a frozenset'))
setting = self._config_bag.context.get_settings()
2024-06-20 12:56:27 +02:00
if when not in ['append', 'remove']:
raise ValueError(_('unknown when {} (must be in append or remove)').format(when))
if type == 'read_only':
if when == 'append':
setting.ro_append = properties
else:
2024-06-20 12:56:27 +02:00
setting.ro_remove = properties
elif type == 'read_write':
if when == 'append':
setting.rw_append = properties
else:
setting.rw_remove = properties
else:
raise ValueError(_('unknown type {}').format(type))
2023-06-19 17:19:07 +02:00
def default(self,
type: Optional[str]=None,
when: Optional[str]=None,
) -> Set[str]:
setting = self._config_bag.context.get_settings()
if type is None and when is None:
2024-06-20 12:56:27 +02:00
return DEFAULT_PROPERTIES
2023-06-19 17:19:07 +02:00
if type == 'current':
return setting.get_context_properties(self._config_bag.context.properties_cache)
if when not in ['append', 'remove']:
raise ValueError(_('unknown when {} (must be in append or remove)').format(when))
if type == 'read_only':
if when == 'append':
return setting.ro_append
2023-05-11 15:44:48 +02:00
return setting.ro_remove
if type == 'read_write':
if when == 'append':
return setting.rw_append
2023-05-11 15:44:48 +02:00
return setting.rw_remove
raise ValueError(_('unknown type {}').format(type))
2017-12-13 22:15:34 +01:00
2024-06-20 12:56:27 +02:00
class TiramisuContextPermissive(TiramisuConfig, PropertyPermissive):
2018-10-07 10:55:52 +02:00
"""Manage config permissives"""
def get(self):
2018-10-07 10:55:52 +02:00
"""Get config permissives"""
return self._get()
2020-01-22 20:46:18 +01:00
def _get(self):
return self._config_bag.context.get_settings().get_context_permissives()
2017-12-13 22:15:34 +01:00
def _set(self,
permissives,
):
2018-10-07 10:55:52 +02:00
"""Set config permissives"""
2024-06-20 12:56:27 +02:00
if self._config_bag.is_unrestraint:
raise ConfigError('cannot change context permissive in unrestraint mode')
settings = self._config_bag.context.get_settings()
settings.set_context_permissives(permissives)
self._reset_config_properties(settings)
2017-12-13 22:15:34 +01:00
def exportation(self):
2018-10-07 10:55:52 +02:00
"""Export config permissives"""
return deepcopy(self._config_bag.context.get_settings()._permissives)
def importation(self, permissives):
2018-10-07 10:55:52 +02:00
"""Import config permissives"""
2024-06-20 12:56:27 +02:00
if self._config_bag.is_unrestraint:
raise ConfigError('cannot change context permissive in unrestraint mode')
context = self._config_bag.context
settings = context.get_settings()
settings._permissives = deepcopy(permissives)
context.reset_cache(None,
None,
)
self._reset_config_properties(settings)
def reset(self):
"""Remove config permissives"""
2024-06-20 12:56:27 +02:00
if self._config_bag.is_unrestraint:
raise ConfigError('cannot change context permissive in unrestraint mode')
settings = self._config_bag.context.get_settings()
2023-04-27 11:34:35 +02:00
settings.reset_permissives(self._config_bag)
2024-06-20 12:56:27 +02:00
self._set_default_permissive(settings)
self._reset_config_properties(settings)
2024-06-20 12:56:27 +02:00
def add(self, permissive):
"""Add a config permissive"""
2024-06-20 12:56:27 +02:00
permissives = set(self._get())
permissives.add(permissive)
self._set(frozenset(permissives))
2024-06-20 12:56:27 +02:00
def remove(self, permissive):
"""Remove a config permissive"""
2024-06-20 12:56:27 +02:00
permissives = set(self._get())
if permissive not in permissives:
msg = f'cannot find "{permissive}"'
raise ConfigError(msg)
permissives.remove(permissive)
self._set(frozenset(permissives))
2017-12-13 22:15:34 +01:00
2022-10-01 19:44:48 +02:00
class TiramisuContextOption(TiramisuConfig, _TiramisuOptionWalk):
def __init__(self,
*args,
**kwargs) -> None:
self._tiramisu_dict = None
super().__init__(*args, **kwargs)
2024-06-20 12:56:27 +02:00
def __iter__(self):
root = self._config_bag.context.get_root(self._config_bag)
for sub_subconfig in root.get_children(True):
yield TiramisuOption(sub_subconfig.path,
sub_subconfig.index,
self._config_bag,
subconfig=sub_subconfig,
)
2024-06-20 12:56:27 +02:00
def get(self):
"""Get Tiramisu option"""
return None
def isleadership(self):
"""Test if option is a leader or a follower"""
return False
def description(self,
uncalculated: bool=False,
) -> str:
2024-06-20 12:56:27 +02:00
"""Get option description"""
if not uncalculated:
return self._config_bag.context.get_description().impl_get_display_name(None)
return self._config_bag.context.get_description()._get_information(None,
'doc',
None,
)
2024-06-20 12:56:27 +02:00
def name(self):
"""Get option name"""
return None
def path(self,
):
"""Get option path"""
return None
def has_dependency(self,
self_is_dep=True,
) -> bool:
"""Test if option has dependency"""
return False
def isoptiondescription(self):
"""Test if option is an optiondescription"""
return True
def isdynamic(self):
"""Test if option is a dynamic optiondescription"""
return False
def type(self):
"""Get de option type"""
return 'optiondescription'
2019-12-24 15:24:20 +01:00
def list(self,
2024-04-24 15:39:17 +02:00
validate_properties: bool=True,
):
2018-10-07 10:55:52 +02:00
"""List options (by default list only option)"""
2024-04-24 15:39:17 +02:00
root = self._config_bag.context.get_root(self._config_bag)
return self._list(root,
validate_properties,
2023-05-11 15:44:48 +02:00
)
def _load_dict(self,
clearable="all",
remotable="minimum"):
self._tiramisu_dict = TiramisuDict(self._return_config(self._config_bag.context),
root=None,
clearable=clearable,
remotable=remotable,
)
def dict(self,
2023-04-27 11:34:35 +02:00
clearable="all",
remotable="minimum",
2023-05-11 15:44:48 +02:00
form=None,
2023-04-27 11:34:35 +02:00
force=False,
):
2022-11-13 15:04:12 +01:00
"""Convert config and option to tiramisu format"""
2023-05-11 15:44:48 +02:00
if form is None:
form = []
if force or self._tiramisu_dict is None:
self._load_dict(clearable, remotable)
return self._tiramisu_dict.todict(form)
def updates(self,
body: List) -> Dict:
2022-11-13 15:04:12 +01:00
"""Updates value with tiramisu format"""
if self._tiramisu_dict is None: # pragma: no cover
self._load_dict()
return self._tiramisu_dict.set_updates(body)
2017-12-13 22:15:34 +01:00
2018-10-07 10:55:52 +02:00
class _TiramisuContextConfigReset():
def reset(self):
2018-10-07 10:55:52 +02:00
"""Remove all datas to current config (informations, values, properties, ...)"""
# Option's values
context_owner = self._config_bag.context.get_values().get_context_owner()
self._config_bag.context.get_values()._values = {None: {None: [None, context_owner]}}
2018-10-07 10:55:52 +02:00
# Option's informations
self._config_bag.context.get_values()._informations = {}
2018-10-07 10:55:52 +02:00
# Option's properties
self._config_bag.context.get_settings()._properties = {}
2018-10-07 10:55:52 +02:00
# Option's permissives
self._config_bag.context.get_settings()._permissives = {}
2018-10-07 10:55:52 +02:00
# Remove cache
self._config_bag.context.reset_cache(None, None)
2018-10-07 10:55:52 +02:00
2018-04-07 20:15:19 +02:00
2019-08-03 21:03:23 +02:00
class _TiramisuContextConfig(TiramisuConfig, _TiramisuContextConfigReset):
2018-10-07 10:55:52 +02:00
"""Actions to Config"""
def type(self):
2020-01-22 20:46:18 +01:00
"""Type a Config"""
return 'config'
2018-08-14 22:15:40 +02:00
def copy(self, name=None):
2019-08-05 22:31:56 +02:00
"""Copy current config"""
config = self._config_bag.context.duplicate(name=name)
return self._return_config(config)
def deepcopy(self, metaconfig_prefix=None, name=None):
2019-08-05 22:31:56 +02:00
"""Copy current config with all parents"""
config = self._config_bag.context.duplicate(metaconfig_prefix=metaconfig_prefix,
deep=[],
name=name,
)
return self._return_config(config)
def parents(self):
2019-08-05 22:31:56 +02:00
"""Get all parents of current config"""
2019-12-24 15:24:20 +01:00
ret = []
2019-08-05 22:31:56 +02:00
for parent in self._config_bag.context.get_parents():
ret.append(self._return_config(parent))
2019-12-24 15:24:20 +01:00
return ret
2018-09-05 20:22:16 +02:00
def path(self):
2019-08-05 22:31:56 +02:00
"""Get path from config (all parents name)"""
return self._config_bag.context.get_config_path()
2019-02-06 21:47:11 +01:00
2018-08-14 22:15:40 +02:00
2019-08-03 21:03:23 +02:00
class _TiramisuContextGroupConfig(TiramisuConfig):
2018-10-07 10:55:52 +02:00
"""Actions to GroupConfig"""
def type(self):
2020-01-22 20:46:18 +01:00
"""Type a Config"""
return 'groupconfig'
2018-08-14 22:15:40 +02:00
def list(self):
2018-10-07 10:55:52 +02:00
"""List children's config"""
2024-04-24 15:39:17 +02:00
return [self._return_config(child) for child in self._config_bag.context.get_children()]
#
# def find(self,
# name: str,
# value=undefined,
# ):
# """Find an or a list of config with finding option"""
# return GroupConfig(self._config_bag.context.find_group(byname=name,
# byvalue=value,
# config_bag=self._config_bag,
# ))
2018-10-07 10:55:52 +02:00
def __call__(self,
path: Optional[str]):
2022-11-13 15:04:12 +01:00
"""Select a child Tiramisu config"""
2018-10-07 10:55:52 +02:00
spaths = path.split('.')
config = self._config_bag.context
for spath in spaths:
config = config.getconfig(spath)
2020-02-18 22:10:33 +01:00
if isinstance(config, KernelGroupConfig):
return self._return_config(config)
return self._return_config(config)
def copy(self, name=None):
config = self._config_bag.context.duplicate(name=name)
return self._return_config(config)
def deepcopy(self, name=None, metaconfig_prefix=None):
config = self._config_bag.context.duplicate(metaconfig_prefix=metaconfig_prefix,
deep=[],
name=name,
)
return self._return_config(config)
def path(self):
return self._config_bag.context.get_config_path()
2018-10-07 10:55:52 +02:00
2018-10-31 18:26:20 +01:00
class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextConfigReset):
"""Actions to MixConfig"""
def type(self):
2020-01-22 20:46:18 +01:00
"""Type a Config"""
return 'mixconfig'
def new(self, name=None, type='config'):
2020-03-04 15:39:47 +01:00
"""Create and add a new config"""
config = self._config_bag.context
new_config = config.new_config(type_=type, name=name)
return self._return_config(new_config)
2020-03-04 15:39:47 +01:00
def remove(self, name):
"""Remove config from MetaConfig"""
config = self._config_bag.context.remove_config(name)
return self._return_config(config)
def add(self,
config):
"""Add config from MetaConfig"""
2023-05-11 15:44:48 +02:00
# pylint: disable=protected-access
self._config_bag.context.add_config(config._config_bag.context)
2018-10-31 08:00:19 +01:00
def parents(self):
2019-09-02 14:08:05 +02:00
"""Get all parents of current config"""
2019-12-24 15:24:20 +01:00
ret = []
2019-09-02 14:08:05 +02:00
for parent in self._config_bag.context.get_parents():
ret.append(self._return_config(parent))
2019-12-24 15:24:20 +01:00
return ret
2019-09-02 14:08:05 +02:00
2018-10-31 08:00:19 +01:00
class _TiramisuContextMetaConfig(_TiramisuContextMixConfig):
2018-10-07 10:55:52 +02:00
"""Actions to MetaConfig"""
def type(self):
2020-01-22 20:46:18 +01:00
"""Type a Config"""
return 'metaconfig'
2019-08-03 21:03:23 +02:00
class TiramisuContextCache(TiramisuConfig):
2022-11-13 15:04:12 +01:00
"""Manage config cache"""
def reset(self):
2022-11-13 15:04:12 +01:00
"""Reset cache"""
self._config_bag.context.reset_cache(None, None)
2019-02-10 21:14:32 +01:00
def set_expiration_time(self,
2023-05-11 15:44:48 +02:00
time: int,
) -> None:
2022-11-13 15:04:12 +01:00
"""Change expiration time value"""
2019-02-24 10:36:42 +01:00
self._config_bag.expiration_time = time
def get_expiration_time(self) -> int:
2022-11-13 15:04:12 +01:00
"""Get expiration time value"""
2019-02-25 08:46:58 +01:00
return self._config_bag.expiration_time
2019-02-10 21:14:32 +01:00
2018-08-01 08:37:58 +02:00
class TiramisuAPI(TiramisuHelp):
2023-05-11 15:44:48 +02:00
"""TiramisuAPI common class
"""
2018-10-07 10:55:52 +02:00
_registers = {}
2018-08-01 08:37:58 +02:00
def __init__(self,
2019-12-24 15:24:20 +01:00
config_bag,
orig_config_bags=None) -> None:
self._config_bag = config_bag
self._orig_config_bags = orig_config_bags
2018-10-07 10:55:52 +02:00
if not self._registers:
_registers(self._registers, 'TiramisuContext')
2018-08-01 08:37:58 +02:00
2024-06-20 12:56:27 +02:00
def option(self,
path: str,
index: Optional[int]=None,
) -> TiramisuOption:
"""Select an option by path"""
return TiramisuOption(path,
index,
self._config_bag,
)
2018-08-01 08:37:58 +02:00
def __getattr__(self, subfunc: str) -> Any:
2023-05-11 15:44:48 +02:00
if subfunc in ['forcepermissive', 'unrestraint', 'nowarnings']:
if self._orig_config_bags:
2023-05-11 15:44:48 +02:00
msg = _('do not use unrestraint, nowarnings or forcepermissive together')
raise ConfigError(msg)
config_bag = self._config_bag.copy()
if subfunc == 'unrestraint':
config_bag.unrestraint()
2023-05-11 15:44:48 +02:00
elif subfunc == 'nowarnings':
config_bag.nowarnings()
2019-12-24 15:24:20 +01:00
else:
config_bag.set_permissive()
2024-06-20 12:56:27 +02:00
return ConfigProp(config_bag, [self._config_bag])
2023-05-11 15:44:48 +02:00
if subfunc == 'config':
2018-10-07 10:55:52 +02:00
config_type = self._config_bag.context.impl_type
if config_type == 'group':
config = _TiramisuContextGroupConfig
elif config_type == 'meta':
config = _TiramisuContextMetaConfig
2018-10-31 18:26:20 +01:00
elif config_type == 'mix':
config = _TiramisuContextMixConfig
2018-10-07 10:55:52 +02:00
else:
config = _TiramisuContextConfig
2019-12-24 15:24:20 +01:00
return config(self._config_bag,
self._orig_config_bags)
2023-05-11 15:44:48 +02:00
if subfunc in self._registers:
config_bag = self._config_bag
2019-12-24 15:24:20 +01:00
# del config_bag.permissives
return self._registers[subfunc](config_bag,
self._orig_config_bags)
2023-06-19 17:19:07 +02:00
raise ConfigError(_(f'please specify a valid sub function ({self.__class__.__name__}.{subfunc})'))
2018-08-01 08:37:58 +02:00
2018-10-07 10:55:52 +02:00
def __dir__(self):
2023-05-11 15:44:48 +02:00
return list(self._registers.keys()) + \
['unrestraint', 'forcepermissive', 'nowarnings', 'config']
2018-01-03 21:07:51 +01:00
2024-06-20 12:56:27 +02:00
class ConfigProp(TiramisuAPI, TiramisuContextOption):
def __repr__(self):
return f'<Config path=None>'
2017-10-22 09:48:08 +02:00
2024-06-20 12:56:27 +02:00
class Config(TiramisuAPI, TiramisuContextOption):
2018-10-08 15:56:28 +02:00
"""Root config object that enables us to handle the configuration options"""
def __init__(self,
descr: OptionDescription,
name=None,
display_name=None,
) -> None:
if isinstance(descr, KernelConfig):
config = descr
else:
config = KernelConfig(descr,
name=name,
display_name=display_name,
)
settings = config.get_settings()
2024-06-20 12:56:27 +02:00
properties = settings.get_context_properties()
permissives = settings.get_context_permissives()
2019-12-24 15:24:20 +01:00
config_bag = ConfigBag(config,
properties=properties,
2022-10-01 19:44:48 +02:00
permissives=permissives,
)
2019-12-24 15:24:20 +01:00
super().__init__(config_bag)
2020-01-22 20:46:18 +01:00
def __del__(self):
try:
del self._config_bag.context
del self._config_bag
del self._orig_config_bags
2023-05-11 15:44:48 +02:00
except ConfigError:
2020-01-22 20:46:18 +01:00
pass
2024-06-20 12:56:27 +02:00
def __repr__(self):
return f'<Config path=None>'
2019-12-24 15:24:20 +01:00
2018-08-14 22:15:40 +02:00
class MetaConfig(TiramisuAPI):
2023-05-11 15:44:48 +02:00
"""MetaConfig object that enables us to handle the sub configuration's options
with common root optiondescription
"""
# pylint: disable=too-few-public-methods
def __init__(self,
2023-05-11 15:44:48 +02:00
children: 'Config'=None,
name=None,
optiondescription: Optional[OptionDescription]=None,
display_name=None
) -> None:
2023-05-11 15:44:48 +02:00
if children is None:
children = []
if isinstance(children, KernelMetaConfig):
config = children
else:
_children = []
for child in children:
if isinstance(child, TiramisuAPI):
_children.append(child._config_bag.context)
else:
_children.append(child)
config = KernelMetaConfig(_children,
optiondescription=optiondescription,
name=name,
display_name=display_name,
)
settings = config.get_settings()
2024-06-20 12:56:27 +02:00
properties = settings.get_context_properties()
permissives = settings.get_context_permissives()
2019-12-24 15:24:20 +01:00
config_bag = ConfigBag(config,
properties=properties,
permissives=permissives)
super().__init__(config_bag)
2020-01-22 20:46:18 +01:00
2018-10-31 18:26:20 +01:00
class MixConfig(TiramisuAPI):
2023-05-11 15:44:48 +02:00
"""MixConfig object that enables us to handle the sub configuration's options
with differents root optiondescription
"""
# pylint: disable=too-few-public-methods
def __init__(self,
optiondescription: OptionDescription,
children: List[Config],
name: Callable=None,
display_name=None
) -> None:
if isinstance(children, KernelMixConfig):
config = children
else:
_children = []
for child in children:
if isinstance(child, TiramisuAPI):
_children.append(child._config_bag.context)
else:
_children.append(child)
config = KernelMixConfig(optiondescription,
_children,
name=name,
display_name=display_name,
)
settings = config.get_settings()
properties = settings.get_context_properties(config.properties_cache)
permissives = settings.get_context_permissives()
2019-12-24 15:24:20 +01:00
config_bag = ConfigBag(config,
properties=properties,
2023-06-19 17:19:07 +02:00
permissives=permissives,
)
2019-12-24 15:24:20 +01:00
super().__init__(config_bag)
2020-01-22 20:46:18 +01:00
2018-08-14 22:15:40 +02:00
class GroupConfig(TiramisuAPI):
2023-04-27 11:34:35 +02:00
"""GroupConfig that enables us to access the sub configuration's options"""
2023-05-11 15:44:48 +02:00
# pylint: disable=too-few-public-methods
def __init__(self,
children,
name=None,
) -> None:
2019-06-05 12:33:00 +02:00
if isinstance(children, KernelGroupConfig):
config = children
else:
_children = []
for child in children:
if isinstance(child, TiramisuAPI):
_children.append(child._config_bag.context)
else:
_children.append(child)
2018-08-14 22:15:40 +02:00
config = KernelGroupConfig(_children, name=name)
2019-12-24 15:24:20 +01:00
config_bag = ConfigBag(config,
properties=None,
permissives=None)
super().__init__(config_bag)