tiramisu/tiramisu/api.py

1101 lines
43 KiB
Python
Raw Normal View History

2017-10-22 09:48:08 +02:00
# -*- coding: utf-8 -*-
2018-01-26 07:33:47 +01:00
# Copyright (C) 2017-2018 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/>.
# ____________________________________________________________
2018-04-07 20:15:19 +02:00
from inspect import ismethod, getdoc, signature
2017-11-28 22:42:30 +01:00
from time import time
from copy import deepcopy
2018-04-28 08:39:07 +02:00
from typing import List, Any, Optional, Callable, Union, Dict
from .error import APIError, ConfigError, SlaveError, PropertiesOptionError
from .i18n import _
2018-08-01 08:37:58 +02:00
from .setting import ConfigBag, OptionBag, owners, groups, Undefined, undefined, FORBIDDEN_SET_PROPERTIES
2018-08-14 22:15:40 +02:00
from .config import KernelConfig, SubConfig, KernelGroupConfig, KernelMetaConfig
2018-08-02 08:38:42 +02:00
from .option import ChoiceOption, OptionDescription
2017-12-13 22:15:34 +01:00
TIRAMISU_VERSION = 3
2018-04-07 20:15:19 +02:00
EXCLUDE_HELP = ('help', '_get_option', '_test_slave_index')
class TiramisuHelp:
icon = '\u2937'
tmpl_help = '{0}{1} {2}: \n{0} {3}\n'
def help(self,
2018-04-28 08:39:07 +02:00
init: bool=True,
space: str="",
root: str='',
_display: bool=True,
_valid: bool=False) -> List[str]:
2018-04-07 20:15:19 +02:00
options = []
if init and isinstance(self, TiramisuAPI):
options.append(self.tmpl_help.format(space, self.icon, root + 'unrestraint', _('access to option without property restriction')))
options.append(self.tmpl_help.format(space, self.icon, root + 'forcepermissive', _('access to option without verifying permissive property')))
root = '[unrestraint.|forcepermissive.]'
2018-08-01 08:37:58 +02:00
if 'registers' in dir(self):
modules = list(self.registers.keys())
modules.sort()
for module_name in modules:
module = self.registers[module_name]
2018-08-15 08:35:22 +02:00
try:
instance_module = module(None)
except TypeError:
instance_module = module(None, None, None)
2018-08-01 08:37:58 +02:00
if isinstance(instance_module, TiramisuDispatcher):
if _valid and not getdoc(module.__call__): # pragma: no cover
raise Exception('unknown doc for {}'.format('__call__'))
module_doc = _(getdoc(module.__call__))
module_signature = signature(module.__call__)
module_args = [str(module_signature.parameters[key]) for key in list(module_signature.parameters.keys())[1:]]
module_args = '(' + ', '.join(module_args) + ')'
options.append(self.tmpl_help.format(space, self.icon, root + module_name + module_args, module_doc))
if hasattr(module, 'subhelp'):
instance_submodule = module.subhelp(None, None, None, None, None)
options.extend(instance_submodule.help(init=False, space=space + ' ', root=root + module_name + module_args + '.'))
else:
root = root + '[config(path).]'
if isinstance(instance_module, CommonTiramisuOption):
if _valid and not getdoc(module): # pragma: no cover
raise Exception('unknown doc for {}'.format(module.__class__.__name__))
module_doc = _(getdoc(module))
options.append(self.tmpl_help.format(space, self.icon, root + module_name, module_doc))
if isinstance(instance_module, TiramisuContext):
if _valid and not getdoc(module): # pragma: no cover
raise Exception('unknown doc for {}'.format(module.__class__.__name__))
module_doc = _(getdoc(module))
options.append(self.tmpl_help.format(space, self.icon, root + module_name, module_doc))
options.extend(instance_module.help(init=False, space=space + ' ', root=root + '{}.'.format(module_name)))
2018-04-07 20:15:19 +02:00
funcs = dir(self)
funcs.sort()
for func_name in funcs:
if not func_name.startswith('__') and not func_name in EXCLUDE_HELP:
func = getattr(self, func_name)
if ismethod(func):
module_signature = signature(func)
module_args = list(module_signature.parameters.keys())
module_args = [str(module_signature.parameters[key]) for key in module_signature.parameters.keys()]
module_args = '(' + ', '.join(module_args) + ')'
if func_name.startswith('_'):
func_name = func_name[1:]
2018-04-11 16:36:15 +02:00
if _valid and not getdoc(func): # pragma: no cover
2018-04-07 20:15:19 +02:00
raise Exception('unknown doc for {}'.format(func.__name__))
options.append(self.tmpl_help.format(space, self.icon, root + func_name + module_args, _(getdoc(func))))
if init:
2018-04-11 16:36:15 +02:00
if _display: # pragma: no cover
2018-04-07 20:15:19 +02:00
print('\n'.join(options))
else:
return options
class CommonTiramisu(TiramisuHelp):
2018-01-01 21:32:39 +01:00
allow_optiondescription = True
2018-04-07 20:15:19 +02:00
registers = {}
2018-01-01 21:32:39 +01:00
2018-04-28 08:39:07 +02:00
def _get_option(self) -> Any:
2018-08-01 08:37:58 +02:00
option = self.option_bag.option
2018-01-01 21:32:39 +01:00
if option is None:
2018-04-05 21:20:39 +02:00
option = self.subconfig.cfgimpl_get_description().impl_getchild(self._name,
2018-08-02 23:35:07 +02:00
self.option_bag.config_bag,
2018-01-01 21:32:39 +01:00
self.subconfig)
2018-08-01 08:37:58 +02:00
self.option_bag.set_option(option,
2018-08-02 23:35:07 +02:00
self.option_bag.path,
self.option_bag.index,
self.option_bag.config_bag)
2018-08-18 07:51:04 +02:00
self.option_bag.config_bag.context.cfgimpl_get_settings().validate_properties(self.option_bag)
2018-08-03 22:56:04 +02:00
index = self.option_bag.index
if index is not None:
if option.impl_is_optiondescription() or not option.impl_is_master_slaves('slave'):
raise APIError('index must be set only with a slave option')
self._length = self.subconfig.cfgimpl_get_length_slave(self.option_bag)
if index >= self._length:
raise SlaveError(_('index "{}" is higher than the master length "{}" '
'for option "{}"').format(index,
self._length,
option.impl_get_display_name()))
if not self.allow_optiondescription and option.impl_is_optiondescription():
raise APIError(_('option must not be an optiondescription'))
2018-01-01 21:32:39 +01:00
return option
class CommonTiramisuOption(CommonTiramisu):
2017-12-13 22:15:34 +01:00
allow_optiondescription = False
slave_need_index = True
def __init__(self,
2018-08-01 08:37:58 +02:00
name: str,
2018-08-14 22:15:40 +02:00
subconfig: Union[KernelConfig, SubConfig],
2018-08-01 08:37:58 +02:00
option_bag: OptionBag) -> None:
self.option_bag = option_bag
2018-04-05 21:20:39 +02:00
self._name = name
2017-12-27 15:48:49 +01:00
self.subconfig = subconfig
2018-08-15 08:35:22 +02:00
if option_bag is not None:
# for help()
self._get_option()
if option_bag.config_bag is not None and self.slave_need_index:
self._test_slave_index()
2017-10-25 08:46:22 +02:00
2018-04-28 08:39:07 +02:00
def _test_slave_index(self) -> None:
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-03-24 22:37:48 +01:00
if not option.impl_is_optiondescription():
2018-08-02 23:35:07 +02:00
if self.option_bag.index is None and option.impl_is_master_slaves('slave'):
raise APIError(_('index must be set with the slave option "{}"').format(self.option_bag.path))
2018-08-02 23:35:07 +02:00
elif self.option_bag.index is not None and not option.impl_is_master_slaves('slave'):
raise APIError(_('index must be set only with a slave option, not for "{}"').format(self.option_bag.path))
2017-10-22 09:48:08 +02:00
def __getattr__(self, name):
2018-04-07 20:15:19 +02:00
if not hasattr(CommonTiramisuOption, name):
raise APIError(_('unknown method {}').format(name))
2017-10-22 09:48:08 +02:00
else:
2018-04-07 20:15:19 +02:00
super().__getattribute__(name)
2017-10-22 09:48:08 +02:00
class TiramisuOptionOption(CommonTiramisuOption):
2018-04-07 20:15:19 +02:00
"""manage option"""
2017-12-13 22:15:34 +01:00
allow_optiondescription = True
slave_need_index = False
2018-01-05 23:32:00 +01:00
def get(self):
2018-04-07 20:15:19 +02:00
"""get Tiramisu option"""
2018-08-02 23:08:44 +02:00
return self.option_bag.option
2018-01-05 23:32:00 +01:00
def _ismulti(self):
2017-10-22 09:48:08 +02:00
"""test if option could have multi value"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_is_multi()
2018-01-05 23:32:00 +01:00
def _issubmulti(self):
"""test if option could have submulti value"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_is_submulti()
2017-10-22 09:48:08 +02:00
def ismasterslaves(self):
2017-10-22 09:48:08 +02:00
"""test if option is a master or a slave"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_is_master_slaves()
2017-10-22 09:48:08 +02:00
2018-01-05 23:32:00 +01:00
def _ismaster(self):
2017-10-22 09:48:08 +02:00
"""test if option is a master"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_is_master_slaves('master')
2017-10-22 09:48:08 +02:00
2018-01-05 23:32:00 +01:00
def _isslave(self):
2017-10-22 09:48:08 +02:00
"""test if option is a slave"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_is_master_slaves('slave')
2017-10-22 09:48:08 +02:00
2018-01-05 23:32:00 +01:00
def doc(self):
2018-04-07 20:15:19 +02:00
"""get option document"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_get_display_name()
2017-10-25 08:46:22 +02:00
2018-04-05 21:20:39 +02:00
def name(self):
2018-04-07 20:15:19 +02:00
"""get option name"""
2018-04-05 21:20:39 +02:00
return self._name
2018-04-28 08:39:07 +02:00
def path(self) -> str:
"""get option path"""
2018-08-02 23:35:07 +02:00
return self.option_bag.path
2018-01-05 23:32:00 +01:00
def _default(self):
2018-04-07 20:15:19 +02:00
"""get default value for an option (not for optiondescription)"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_getdefault()
2017-10-22 09:48:08 +02:00
2018-01-05 23:32:00 +01:00
def _defaultmulti(self):
2018-04-07 20:15:19 +02:00
"""get default value when added a value for a multi option (not for optiondescription)"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_getdefault_multi()
2017-12-19 23:11:45 +01:00
def has_dependency(self, self_is_dep=True):
2018-04-07 20:15:19 +02:00
"""test if option has dependency"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_has_dependency(self_is_dep)
2017-12-19 23:11:45 +01:00
2018-01-05 23:32:00 +01:00
def _consistencies(self):
2018-04-07 20:15:19 +02:00
"""get consistencies for an option (not for optiondescription)"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-01-05 23:32:00 +01:00
return option.get_consistencies()
def _callbacks(self):
2018-04-07 20:15:19 +02:00
"""get callbacks for an option (not for optiondescription)"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-01-05 23:32:00 +01:00
return option.impl_get_callback()
def requires(self):
2018-04-07 20:15:19 +02:00
"""get requires for an option"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-01-05 23:32:00 +01:00
return option.impl_getrequires()
2018-04-28 08:39:07 +02:00
def __getattr__(self, name: str) -> Callable:
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-08-02 23:35:07 +02:00
if not option.impl_is_optiondescription() and not name.startswith('_'):
2018-08-02 19:22:44 +02:00
return getattr(self, '_' + name)
2018-01-05 23:32:00 +01:00
raise APIError(_('{} is unknown').format(name))
def isoptiondescription(self):
2018-04-07 20:15:19 +02:00
"""test if option is an optiondescription"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
return option.impl_is_optiondescription()
2018-01-05 23:32:00 +01:00
class TiramisuOptionOwner(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""manager option's owner"""
2017-11-12 20:11:56 +01:00
def __init__(self,
2018-08-01 08:37:58 +02:00
name: str,
2018-08-14 22:15:40 +02:00
subconfig: Union[KernelConfig, SubConfig],
2018-08-01 08:37:58 +02:00
option_bag: OptionBag) -> None:
2017-11-20 17:01:36 +01:00
2018-03-31 21:06:19 +02:00
super().__init__(name,
subconfig,
2018-08-01 08:37:58 +02:00
option_bag)
2018-08-15 08:35:22 +02:00
if option_bag is not None:
# for help()
self.values = self.option_bag.config_bag.context.cfgimpl_get_values()
2017-10-22 09:48:08 +02:00
def get(self):
"""get owner for a specified option"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-08-01 08:37:58 +02:00
return self.values.getowner(self.option_bag)
def isdefault(self):
2017-10-22 09:48:08 +02:00
"""is option has defaut value"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-08-01 08:37:58 +02:00
return self.values.is_default_owner(self.option_bag)
2017-10-22 09:48:08 +02:00
def set(self, owner):
2017-10-22 09:48:08 +02:00
"""get owner for a specified option"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.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)
2018-08-01 08:37:58 +02:00
self.values.setowner(obj_owner,
self.option_bag)
2017-10-22 09:48:08 +02:00
class TiramisuOptionProperty(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""manager option's property"""
2017-12-13 22:15:34 +01:00
allow_optiondescription = True
slave_need_index = False
2017-11-12 20:11:56 +01:00
def __init__(self,
2018-08-01 08:37:58 +02:00
name: str,
2018-08-14 22:15:40 +02:00
subconfig: Union[KernelConfig, SubConfig],
2018-08-01 08:37:58 +02:00
option_bag: OptionBag) -> None:
2018-03-31 21:06:19 +02:00
super().__init__(name,
subconfig,
2018-08-01 08:37:58 +02:00
option_bag)
2018-08-15 08:35:22 +02:00
if option_bag and option_bag.config_bag:
2018-08-02 23:35:07 +02:00
self.settings = option_bag.config_bag.context.cfgimpl_get_settings()
2017-10-22 09:48:08 +02:00
2018-01-26 07:33:47 +01:00
def get(self, apply_requires=True):
2018-04-07 20:15:19 +02:00
"""get properties for an option"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-01-26 07:33:47 +01:00
if apply_requires:
self._test_slave_index()
2018-08-01 08:37:58 +02:00
else:
self.option_bag.apply_requires = False
properties = self.option_bag.properties
2017-12-13 22:15:34 +01:00
return set(properties)
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
def add(self, prop):
2018-04-07 20:15:19 +02:00
"""add new property for an option"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
if prop in FORBIDDEN_SET_PROPERTIES:
raise ConfigError(_('cannot add this property: "{0}"').format(
' '.join(prop)))
2018-08-01 08:37:58 +02:00
props = self.settings.getproperties(self.option_bag,
apply_requires=False)
2018-08-02 23:35:07 +02:00
self.settings.setproperties(self.option_bag.path,
props | {prop},
2018-08-01 08:37:58 +02:00
self.option_bag)
2017-10-22 09:48:08 +02:00
2017-12-13 22:15:34 +01:00
def pop(self, prop):
2018-04-07 20:15:19 +02:00
"""remove new property for an option"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-08-01 08:37:58 +02:00
props = self.settings.getproperties(self.option_bag,
apply_requires=False)
2018-08-02 23:35:07 +02:00
self.settings.setproperties(self.option_bag.path,
props - {prop},
2018-08-01 08:37:58 +02:00
self.option_bag)
2017-12-13 22:15:34 +01:00
def reset(self):
2018-04-07 20:15:19 +02:00
"""reset all personalised properties"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-08-01 08:37:58 +02:00
self.settings.reset(self.option_bag)
2017-10-22 09:48:08 +02:00
class TiramisuOptionPermissive(CommonTiramisuOption):
"""manager option's property"""
2017-12-13 22:15:34 +01:00
allow_optiondescription = True
slave_need_index = False
# FIXME should have same api than property
2017-11-12 20:11:56 +01:00
def __init__(self,
2018-08-01 08:37:58 +02:00
name: str,
2018-08-14 22:15:40 +02:00
subconfig: Union[KernelConfig, SubConfig],
2018-08-01 08:37:58 +02:00
option_bag: OptionBag) -> None:
2018-03-31 21:06:19 +02:00
super().__init__(name,
subconfig,
2018-08-01 08:37:58 +02:00
option_bag)
2018-08-15 08:35:22 +02:00
if option_bag and option_bag.config_bag:
2018-08-02 23:35:07 +02:00
self.settings = option_bag.config_bag.context.cfgimpl_get_settings()
def get(self):
2018-04-07 20:15:19 +02:00
"""get permissives value"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-08-02 23:35:07 +02:00
return self.settings.getpermissive(option, self.option_bag.path)
2017-12-28 11:47:29 +01:00
def set(self, permissives):
2018-04-07 20:15:19 +02:00
"""set permissives value"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-08-01 08:37:58 +02:00
self.settings.setpermissive(self.option_bag,
permissives=permissives)
2018-04-11 16:36:15 +02:00
def reset(self):
2018-04-07 20:15:19 +02:00
"""reset all personalised permissive"""
2018-04-11 16:36:15 +02:00
self.set(frozenset())
2017-12-13 22:15:34 +01:00
class TiramisuOptionInformation(CommonTiramisuOption):
2018-04-07 20:15:19 +02:00
"""manage option informations"""
2017-12-13 22:15:34 +01:00
allow_optiondescription = True
2018-01-26 07:33:47 +01:00
slave_need_index = False
2017-12-13 22:15:34 +01:00
def get(self, name, default=undefined):
2018-04-07 20:15:19 +02:00
"""get information for a key name"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2017-12-27 15:48:49 +01:00
return option.impl_get_information(name, default)
2017-12-13 22:15:34 +01:00
2018-04-07 20:15:19 +02:00
def set(self, name, value):
"""set information for a key name"""
2018-08-02 23:08:44 +02:00
#FIXME ?
2018-08-02 22:35:40 +02:00
self.config_bag.context.impl_set_information(name, value)
2018-04-07 20:15:19 +02:00
def reset(self, name):
"""remove information for a key name"""
2018-08-02 23:08:44 +02:00
#FIXME ?
2018-08-02 22:35:40 +02:00
self.config_bag.context.impl_del_information(name)
2018-04-07 20:15:19 +02:00
2017-12-13 22:15:34 +01:00
class TiramisuOptionValue(CommonTiramisuOption):
2017-10-22 09:48:08 +02:00
"""manager option's value"""
slave_need_index = False
2017-10-22 09:48:08 +02:00
def get(self):
2018-04-07 20:15:19 +02:00
"""get option's value"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
self._test_slave_index()
2018-08-02 22:35:40 +02:00
return self.subconfig.getattr(self._name,
self.option_bag)
def set(self, value):
"""set a value for a specified option"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
self._test_slave_index()
2018-08-02 23:35:07 +02:00
values = self.option_bag.config_bag.context.cfgimpl_get_values()
if isinstance(value, list):
while undefined in value:
idx = value.index(undefined)
2018-08-01 08:37:58 +02:00
option_bag = OptionBag()
option_bag.set_option(self.option_bag.option,
self.option_bag.path,
idx,
2018-08-02 23:35:07 +02:00
self.option_bag.config_bag)
2018-08-01 08:37:58 +02:00
value[idx] = values.getdefaultvalue(option_bag)
else:
if value == undefined:
2018-08-01 08:37:58 +02:00
value = values.getdefaultvalue(self.option_bag)
self.subconfig.setattr(value,
self.option_bag)
2017-10-22 09:48:08 +02:00
2018-01-01 21:32:39 +01:00
def _pop(self, index):
2018-04-07 20:15:19 +02:00
"""pop value for a master option (only for master option)"""
2018-08-01 08:37:58 +02:00
if self.option_bag.option.impl_is_symlinkoption():
raise TypeError(_("can't delete a SymLinkOption"))
2018-08-02 23:35:07 +02:00
self.option_bag.config_bag.context.cfgimpl_get_values().reset_master(index,
self.option_bag,
self.subconfig)
2017-11-12 20:11:56 +01:00
def reset(self):
2017-10-22 09:48:08 +02:00
"""reset value for a value"""
self._test_slave_index()
2018-08-01 08:37:58 +02:00
self.subconfig.delattr(self.option_bag)
2017-10-22 09:48:08 +02:00
2018-07-07 17:11:33 +02:00
def _len_master(self):
"""length of master option (only for slave option)"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-07-07 17:11:33 +02:00
# for example if index is None
if '_length' not in vars(self):
self._length = self.subconfig.cfgimpl_get_length()
return self._length
def _len_slave(self):
2018-04-07 20:15:19 +02:00
"""length of slave option (only for slave option)"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
2018-04-19 08:19:03 +02:00
# for example if index is None
if '_length' not in vars(self):
2018-08-01 08:37:58 +02:00
self._length = self.subconfig.cfgimpl_get_length_slave(self.option_bag)
2018-04-19 08:19:03 +02:00
return self._length
2018-04-28 08:39:07 +02:00
def __getattr__(self, name: str) -> Callable:
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
if name == 'list' and isinstance(option, ChoiceOption):
2018-01-01 21:32:39 +01:00
return self._list
2018-08-02 23:08:44 +02:00
elif name == 'pop' and option.impl_is_master_slaves('master'):
2018-01-01 21:32:39 +01:00
return self._pop
2018-07-07 17:11:33 +02:00
elif name == 'len':
2018-08-02 23:08:44 +02:00
if option.impl_is_master_slaves('slave'):
2018-07-07 17:11:33 +02:00
return self._len_slave
2018-08-02 23:08:44 +02:00
if option.impl_is_master_slaves('master'):
2018-07-07 17:11:33 +02:00
return self._len_master
2017-12-19 23:11:45 +01:00
raise APIError(_('{} is unknown').format(name))
2017-12-13 22:15:34 +01:00
def _list(self):
2018-04-07 20:15:19 +02:00
"""all values available for an option (only for choiceoption)"""
2018-08-02 23:08:44 +02:00
option = self.option_bag.option
return option.impl_get_values(self.option_bag)
2017-12-13 22:15:34 +01:00
2018-04-28 08:39:07 +02:00
def registers(registers: Dict[str, type], prefix: str) -> None:
2017-12-13 22:15:34 +01:00
for module_name in globals().keys():
if module_name != prefix and module_name.startswith(prefix):
module = globals()[module_name]
func_name = module_name[len(prefix):].lower()
registers[func_name] = module
2017-10-22 09:48:08 +02:00
2018-01-01 21:32:39 +01:00
class TiramisuOption(CommonTiramisu):
2018-08-01 08:37:58 +02:00
registers = {}
2017-11-12 20:11:56 +01:00
def __init__(self,
2018-04-28 08:39:07 +02:00
name: Optional[str],
path: Optional[str]=None,
index: Optional[int]=None,
2018-08-14 22:15:40 +02:00
subconfig: Union[None, KernelConfig, SubConfig]=None,
2018-08-01 08:37:58 +02:00
config_bag: Optional[ConfigBag]=None,
option_bag: Optional[OptionBag]=None) -> None:
2018-04-05 21:20:39 +02:00
self._name = name
2017-12-27 15:48:49 +01:00
self.subconfig = subconfig
self._path = path
self.index = index
2017-12-19 23:11:45 +01:00
self.config_bag = config_bag
2018-08-01 08:37:58 +02:00
if option_bag:
self.option_bag = option_bag
else:
self.option_bag = OptionBag()
2018-08-02 23:35:07 +02:00
self.option_bag.path = self._path
self.option_bag.index = self.index
self.option_bag.config_bag = self.config_bag
2018-08-01 08:37:58 +02:00
if not self.registers:
registers(self.registers, self.__class__.__name__)
2017-10-22 09:48:08 +02:00
2018-04-28 08:39:07 +02:00
def __getattr__(self, subfunc: str) -> Any:
2017-10-22 09:48:08 +02:00
if subfunc in self.registers:
2018-04-05 21:20:39 +02:00
return self.registers[subfunc](self._name,
2017-12-27 15:48:49 +01:00
self.subconfig,
2018-08-01 08:37:58 +02:00
self.option_bag)
2018-08-03 22:56:04 +02:00
elif self._get_option().impl_is_optiondescription() and not subfunc.startswith('_'):
2018-08-02 19:22:44 +02:00
return getattr(self, '_' + subfunc)
raise APIError(_('please specify a valid sub function ({})').format(subfunc))
2017-12-13 22:15:34 +01:00
2018-01-01 21:32:39 +01:00
def _make_dict(self,
flatten=False,
withvalue=undefined,
withoption=None,
fullpath=False):
2018-04-07 20:15:19 +02:00
"""return dict with path as key and value for an optiondescription (only for optiondescription)"""
2018-08-01 08:37:58 +02:00
self._get_option()
2018-08-02 22:35:40 +02:00
return self.config_bag.context.get_subconfig(self._path,
self.option_bag).make_dict(config_bag=self.config_bag,
flatten=flatten,
fullpath=fullpath,
withoption=withoption,
withvalue=withvalue)
2018-04-10 12:33:51 +02:00
def _find(self,
2018-04-28 08:39:07 +02:00
name: str,
value=undefined,
2018-04-12 23:04:33 +02:00
type=None,
2018-04-28 08:39:07 +02:00
first: bool=False):
2018-04-10 12:33:51 +02:00
"""find an option by name (only for optiondescription)"""
if not first:
ret = []
2018-08-02 22:35:40 +02:00
for path in self.config_bag.context.find(byname=name,
byvalue=value,
bytype=type,
_subpath=self._path,
config_bag=self.config_bag):
subconfig, name = self.config_bag.context.cfgimpl_get_home_by_path(path,
self.config_bag)
2018-04-10 12:33:51 +02:00
t_option = TiramisuOption(name,
path,
None, # index for a slave ?
subconfig,
2018-08-01 08:37:58 +02:00
self.config_bag)
2018-04-10 12:33:51 +02:00
if first:
return t_option
ret.append(t_option)
return ret
2018-08-01 08:37:58 +02:00
def _get(self, name):
self._get_option()
current_option = self.option_bag.option.impl_getchild(name,
self.config_bag,
self.subconfig)
path = self.option_bag.path + '.' + name
option_bag= OptionBag()
option_bag.set_option(current_option,
path,
None,
self.config_bag)
if current_option.impl_is_optiondescription():
subconfig = self.subconfig.getattr(name,
option_bag)
else:
subconfig = self.subconfig
return TiramisuOption(name,
path,
None,
subconfig,
self.config_bag,
option_bag)
2018-04-07 20:15:19 +02:00
def _group_type(self):
"""get type for an optiondescription (only for optiondescription)"""
2018-01-05 23:32:00 +01:00
return self._get_option().impl_get_group_type()
2018-01-01 21:32:39 +01:00
def _list(self,
type='all',
group_type=None):
2018-04-07 20:15:19 +02:00
"""list options in an optiondescription (only for optiondescription)"""
2018-08-01 08:37:58 +02:00
if type not in ('all', 'optiondescription'):
2018-01-01 21:32:39 +01:00
raise APIError(_('unknown list type {}').format(type))
2018-08-01 08:37:58 +02:00
if group_type is not None and not isinstance(group_type,
groups.GroupType):
raise TypeError(_("unknown group_type: {0}").format(group_type))
def _filter(opt):
2018-08-18 07:51:04 +02:00
if self.config_bag.properties:
2018-08-01 08:37:58 +02:00
name = opt.impl_getname()
path = subconfig._get_subpath(name)
option_bag = OptionBag()
option_bag.set_option(opt,
path,
None,
self.config_bag)
if opt.impl_is_optiondescription():
self.subconfig.get_subconfig(name,
option_bag)
else:
self.subconfig.getattr(name,
option_bag)
2018-08-01 08:37:58 +02:00
option = self._get_option()
name = option.impl_getname()
path = self.subconfig._get_subpath(name)
option_bag = OptionBag()
option_bag.set_option(option,
path,
None,
self.config_bag)
subconfig = self.subconfig.get_subconfig(name,
option_bag)
2018-08-01 08:37:58 +02:00
for opt in option.impl_getchildren(self.config_bag):
try:
subsubconfig = _filter(opt)
except PropertiesOptionError:
continue
if opt.impl_is_optiondescription():
if type == 'optiondescription' and \
(group_type and opt.impl_get_group_type() != group_type):
continue
else:
if type == 'optiondescription':
continue
name = opt.impl_getname()
yield TiramisuOption(name,
subconfig._get_subpath(name),
None,
subconfig,
self.config_bag)
2018-01-01 21:32:39 +01:00
2018-04-07 20:15:19 +02:00
class TiramisuContext(TiramisuHelp):
2017-11-23 16:56:14 +01:00
def __init__(self,
2018-04-28 08:39:07 +02:00
config_bag: Optional[ConfigBag]) -> None:
2017-12-19 23:11:45 +01:00
self.config_bag = config_bag
2017-12-13 22:15:34 +01:00
class TiramisuContextInformation(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manage configuration informations"""
2017-12-13 22:15:34 +01:00
def get(self, name, default=undefined):
2018-04-07 20:15:19 +02:00
"""get information for a key name"""
2018-08-02 22:35:40 +02:00
return self.config_bag.context.impl_get_information(name, default)
2017-12-13 22:15:34 +01:00
def set(self, name, value):
2018-04-07 20:15:19 +02:00
"""set information for a key name"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.impl_set_information(name, value)
2017-12-13 22:15:34 +01:00
def reset(self, name):
2018-04-07 20:15:19 +02:00
"""remove information for a key name"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.impl_del_information(name)
2017-12-13 22:15:34 +01:00
class TiramisuContextValue(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manager value"""
2017-12-13 22:15:34 +01:00
def mandatory_warnings(self):
2018-04-07 20:15:19 +02:00
"""return path of options with mandatory property without any value"""
2018-08-02 22:35:40 +02:00
return self.config_bag.context.cfgimpl_get_values().mandatory_warnings(self.config_bag)
2017-12-13 22:15:34 +01:00
2018-01-03 21:07:51 +01:00
def set(self,
2018-04-28 08:39:07 +02:00
path: str,
2018-01-03 21:07:51 +01:00
value,
index=None,
only_config=undefined,
force_default=undefined,
force_default_if_same=undefined,
force_dont_change_value=undefined):
2018-04-07 20:15:19 +02:00
"""set values for a GroupConfig or a MetaConfig"""
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
2018-08-02 22:35:40 +02:00
return self.config_bag.context.set_value(path,
index,
value,
self.config_bag,
**kwargs)
2018-01-03 21:07:51 +01:00
def reset(self,
path):
2018-04-07 20:15:19 +02:00
"""reset value for a GroupConfig or a MetaConfig"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.reset(path,
self.config_bag)
2018-01-03 21:07:51 +01:00
def exportation(self):
2018-04-07 20:15:19 +02:00
"""export all values"""
2018-08-02 22:35:40 +02:00
return self.config_bag.context.cfgimpl_get_values()._p_.exportation()
2018-04-04 16:47:28 +02:00
def importation(self, values):
2018-04-07 20:15:19 +02:00
"""import values"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.cfgimpl_get_values()._p_.importation(values)
self.config_bag.context.cfgimpl_reset_cache(None, None)
2017-12-13 22:15:34 +01:00
class TiramisuContextOwner(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manager value"""
def get(self):
2018-04-07 20:15:19 +02:00
"""get default owner"""
2018-08-02 22:35:40 +02:00
return self.config_bag.context.cfgimpl_get_settings().getowner()
2017-12-23 12:29:45 +01:00
def set(self, owner):
2018-04-07 20:15:19 +02:00
"""set default 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)
2018-08-02 22:35:40 +02:00
self.config_bag.context.cfgimpl_get_settings().setowner(obj_owner)
2017-12-23 12:29:45 +01:00
2017-12-13 22:15:34 +01:00
class TiramisuContextProperty(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manage configuration properties"""
2017-12-13 22:15:34 +01:00
def read_only(self):
2018-04-07 20:15:19 +02:00
"""set configuration to read only mode"""
2018-08-02 22:35:40 +02:00
settings = self.config_bag.context.cfgimpl_get_settings()
2017-12-19 23:11:45 +01:00
settings.read_only()
2018-08-01 08:37:58 +02:00
try:
2018-08-18 07:51:04 +02:00
del self.config_bag.properties
2018-08-01 08:37:58 +02:00
except AttributeError:
pass
2017-12-13 22:15:34 +01:00
def read_write(self):
2018-04-07 20:15:19 +02:00
"""set configuration to read and write mode"""
2018-08-02 22:35:40 +02:00
settings = self.config_bag.context.cfgimpl_get_settings()
2017-12-19 23:11:45 +01:00
settings.read_write()
2017-12-13 22:15:34 +01:00
# #FIXME ?
settings.set_context_permissive(frozenset(['hidden']))
2018-08-01 08:37:58 +02:00
try:
2018-08-18 07:51:04 +02:00
del self.config_bag.properties
2018-08-01 08:37:58 +02:00
except AttributeError:
pass
2017-12-13 22:15:34 +01:00
#/FIXME ?
def add(self, prop):
2018-04-07 20:15:19 +02:00
"""add a configuration property"""
2017-12-13 22:15:34 +01:00
props = self.get()
props.add(prop)
2017-12-28 11:47:29 +01:00
self.set(frozenset(props))
2017-12-13 22:15:34 +01:00
def pop(self, prop):
2018-04-07 20:15:19 +02:00
"""remove a configuration property"""
2017-12-13 22:15:34 +01:00
props = self.get()
2018-04-03 14:27:20 +02:00
if prop in props:
props.remove(prop)
self.set(frozenset(props))
2017-12-13 22:15:34 +01:00
def get(self):
2018-04-07 20:15:19 +02:00
"""get all configuration properties"""
2018-08-18 07:51:04 +02:00
return set(self.config_bag.properties)
2017-12-13 22:15:34 +01:00
def set(self, props):
2018-04-07 20:15:19 +02:00
"""personalise configuration properties"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.cfgimpl_get_settings().set_context_properties(props)
2017-12-13 22:15:34 +01:00
2017-12-23 20:21:07 +01:00
def reset(self):
2018-04-07 20:15:19 +02:00
"""remove configuration properties"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.cfgimpl_get_settings().reset(None)
2017-12-23 20:21:07 +01:00
def exportation(self):
2018-04-07 20:15:19 +02:00
"""export configuration properties"""
2018-08-02 22:35:40 +02:00
return self.config_bag.context.cfgimpl_get_settings()._p_.exportation()
def importation(self, properties):
2018-04-07 20:15:19 +02:00
"""import configuration properties"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.cfgimpl_get_settings()._p_.importation(properties)
self.config_bag.context.cfgimpl_reset_cache(None,
None)
2017-12-13 22:15:34 +01:00
class TiramisuContextPermissive(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manage configuration permissives"""
def get(self):
"""get configuration permissives"""
2018-08-02 22:35:40 +02:00
return self.config_bag.context.cfgimpl_get_settings().get_context_permissive()
2017-12-13 22:15:34 +01:00
def set(self, permissives):
2018-04-07 20:15:19 +02:00
"""set configuration permissives"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.cfgimpl_get_settings().set_context_permissive(permissives)
2017-12-13 22:15:34 +01:00
def exportation(self):
2018-04-07 20:15:19 +02:00
"""export configuration permissives"""
2018-08-02 22:35:40 +02:00
return self.config_bag.context.cfgimpl_get_settings()._pp_.exportation()
def importation(self, permissives):
2018-04-07 20:15:19 +02:00
"""import configuration permissives"""
2018-08-02 22:35:40 +02:00
self.config_bag.context.cfgimpl_get_settings()._pp_.importation(permissives)
self.config_bag.context.cfgimpl_reset_cache(None,
None)
2017-12-13 22:15:34 +01:00
2017-11-23 16:56:14 +01:00
class TiramisuContextOption(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""manage option"""
2017-11-23 16:56:14 +01:00
def find(self,
name,
2018-04-10 12:33:51 +02:00
value=undefined,
2018-04-12 23:04:33 +02:00
type=None,
2018-04-07 20:15:19 +02:00
first=False):
"""find an option by name"""
2018-04-10 12:33:51 +02:00
if not first:
ret = []
2018-08-02 22:35:40 +02:00
for path in self.config_bag.context.find(byname=name,
byvalue=value,
bytype=type,
#_subpath=self._path,
config_bag=self.config_bag):
subconfig, name = self.config_bag.context.cfgimpl_get_home_by_path(path,
self.config_bag)
2018-04-10 12:33:51 +02:00
t_option = TiramisuOption(name,
path,
None, # index for a slave ?
subconfig,
2018-08-01 08:37:58 +02:00
self.config_bag)
2018-04-10 12:33:51 +02:00
if first:
return t_option
ret.append(t_option)
return ret
2017-11-23 16:56:14 +01:00
2018-08-01 08:37:58 +02:00
def get(self, name):
2018-08-02 22:35:40 +02:00
option = self.config_bag.context.cfgimpl_get_description().impl_getchild(name,
self.config_bag,
self.config_bag.context)
2018-08-01 08:37:58 +02:00
return TiramisuOption(name,
name,
None,
2018-08-02 22:35:40 +02:00
self.config_bag.context,
2018-08-01 08:37:58 +02:00
self.config_bag)
2017-11-23 16:56:14 +01:00
2017-12-13 22:15:34 +01:00
def make_dict(self,
flatten=False,
withvalue=undefined,
withoption=None,
fullpath=False):
2018-04-07 20:15:19 +02:00
"""return dict with path as key and value"""
2018-08-18 07:51:04 +02:00
if not self.config_bag.properties:
2018-08-17 22:12:50 +02:00
config_bag = self.config_bag
else:
config_bag = self.config_bag.copy()
2018-08-18 07:51:04 +02:00
config_bag.properties = self.config_bag.properties - {'warnings'}
return config_bag.context.make_dict(config_bag,
flatten=flatten,
fullpath=fullpath,
withoption=withoption,
withvalue=withvalue)
2017-12-13 22:15:34 +01:00
def list(self,
type='all',
group_type=None,
recursive=False):
2018-04-07 20:15:19 +02:00
"""list content of an optiondescription"""
2018-08-02 08:38:42 +02:00
def _filter(opt):
2018-08-18 07:51:04 +02:00
if self.config_bag.properties:
2018-08-02 08:38:42 +02:00
name = opt.impl_getname()
option_bag = OptionBag()
option_bag.set_option(opt,
name,
None,
self.config_bag)
2018-08-02 22:35:40 +02:00
self.config_bag.context.getattr(name,
2018-08-02 08:38:42 +02:00
option_bag)
if type not in ('all', 'optiondescription'):
raise APIError(_('unknown list type {}').format(type))
2018-08-01 08:37:58 +02:00
if group_type is not None and not isinstance(group_type,
groups.GroupType):
raise TypeError(_("unknown group_type: {0}").format(group_type))
2018-08-02 08:38:42 +02:00
if recursive:
2018-04-04 22:00:10 +02:00
if group_type:
2018-08-02 08:38:42 +02:00
raise APIError(_('recursive with group_type is not implemented yet'))
2018-08-18 07:51:04 +02:00
if self.config_bag.properties:
2018-04-04 22:00:10 +02:00
raise APIError(_('not implemented yet'))
2018-08-02 22:35:40 +02:00
for option in self.config_bag.context.cfgimpl_get_description()._cache_paths[1]:
2018-08-02 08:38:42 +02:00
if type == 'optiondescription' and not isinstance(option, OptionDescription):
continue
yield option
2018-01-01 21:32:39 +01:00
else:
2018-08-02 22:35:40 +02:00
option = self.config_bag.context.cfgimpl_get_description()
2018-08-02 08:38:42 +02:00
for opt in option.impl_getchildren(self.config_bag):
try:
subsubconfig = _filter(opt)
except PropertiesOptionError:
continue
if opt.impl_is_optiondescription():
if type == 'optiondescription' and \
(group_type and opt.impl_get_group_type() != group_type):
continue
else:
if type == 'optiondescription':
continue
name = opt.impl_getname()
yield TiramisuOption(name,
2018-08-02 22:35:40 +02:00
self.config_bag.context._get_subpath(name),
2018-08-02 08:38:42 +02:00
None,
2018-08-02 22:35:40 +02:00
self.config_bag.context,
2018-08-02 08:38:42 +02:00
self.config_bag)
2018-01-01 21:32:39 +01:00
2017-12-13 22:15:34 +01:00
2018-01-03 21:07:51 +01:00
class TiramisuContextConfig(TiramisuContext):
2018-04-07 20:15:19 +02:00
"""configuration methods"""
def find(self,
2018-04-28 08:39:07 +02:00
name: str,
2018-04-10 12:33:51 +02:00
value=undefined,
2018-04-28 08:39:07 +02:00
first: bool=False):
2018-04-07 20:15:19 +02:00
"""find a path from option name and optionnaly a value to MetaConfig or GroupConfig"""
if first:
2018-08-17 22:12:50 +02:00
return TiramisuAPI(self.config_bag.context.find_firsts(byname=name,
byvalue=value,
config_bag=self.config_bag))
2018-04-07 20:15:19 +02:00
else:
raise APIError('not implemented yet')
2018-08-14 22:15:40 +02:00
def name(self):
return self.config_bag.context.impl_getname()
def duplicate(self,
session_id=None):
return TiramisuAPI(self.config_bag.context.duplicate(session_id))
def _m_new(self, name):
2018-08-14 22:25:31 +02:00
return TiramisuAPI(self.config_bag.context.new_config(name))
2018-08-14 22:15:40 +02:00
def _m_list(self):
return self._g_list()
def _g_list(self):
2018-08-14 22:25:31 +02:00
for child in self.config_bag.context.cfgimpl_get_children():
yield TiramisuAPI(child)
2018-08-14 22:15:40 +02:00
def __getattr__(self,
name: str) -> Callable:
if not name.startswith('_'):
try:
if isinstance(self.config_bag.context, KernelMetaConfig):
return getattr(self, '_m_' + name)
elif isinstance(self.config_bag.context, KernelGroupConfig):
return getattr(self, '_g_' + name)
except APIError:
raise APIError(_('{} is unknown').format(name))
raise APIError(_('{} is unknown').format(name))
2018-01-03 21:07:51 +01:00
2018-04-07 20:15:19 +02:00
class TiramisuDispatcher:
pass
2018-01-03 21:07:51 +01:00
2018-04-07 20:15:19 +02:00
2018-08-01 08:37:58 +02:00
class TiramisuAPI(TiramisuHelp):
registers = {}
def __init__(self,
2018-08-14 22:15:40 +02:00
config) -> None:
2018-08-01 08:37:58 +02:00
self._config = config
if not self.registers:
registers(self.registers, 'TiramisuContext')
registers(self.registers, 'TiramisuDispatcher')
def __getattr__(self, subfunc: str) -> Any:
if subfunc == 'forcepermissive':
if isinstance(self._config, ConfigBag):
config = self._config.config
2018-08-18 07:51:04 +02:00
force = not self._config.properties
2018-08-01 08:37:58 +02:00
else:
config = self._config
force = None
2018-08-02 22:35:40 +02:00
config_bag = ConfigBag(context=config,
2018-08-01 08:37:58 +02:00
force_permissive=True)
2018-08-17 23:11:25 +02:00
if force is True:
2018-08-18 07:51:04 +02:00
config_bag.properties = frozenset()
2018-08-01 08:37:58 +02:00
return TiramisuAPI(config_bag)
elif subfunc == 'unrestraint':
if isinstance(self._config, ConfigBag):
2018-08-02 22:35:40 +02:00
config = self._config.context
2018-08-01 08:37:58 +02:00
force = self._config.force_permissive
else:
config = self._config
force = None
2018-08-17 23:11:25 +02:00
config_bag = ConfigBag(context=config)
2018-08-18 07:51:04 +02:00
config_bag.properties = frozenset()
2018-08-01 08:37:58 +02:00
if force is not None:
config_bag.force_permissive = force
return TiramisuAPI(config_bag)
elif subfunc in self.registers:
if not isinstance(self._config, ConfigBag):
2018-08-02 22:35:40 +02:00
config_bag = ConfigBag(context=self._config)
2018-08-01 08:37:58 +02:00
else:
config_bag = self._config
return self.registers[subfunc](config_bag)
else:
raise APIError(_('please specify a valid sub function ({})').format(subfunc))
2018-04-07 20:15:19 +02:00
class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig):
2018-08-01 08:37:58 +02:00
def __call__(self,
2018-08-14 22:15:40 +02:00
path: Optional[str]):
2018-04-07 20:15:19 +02:00
"""select a child Tiramisu configuration (only with MetaConfig or GroupConfig)"""
2018-03-19 08:33:53 +01:00
if path is None:
2018-08-01 08:37:58 +02:00
return TiramisuAPI(self.config_bag)
2018-03-19 08:33:53 +01:00
spaths = path.split('.')
2018-08-02 22:35:40 +02:00
config = self.config_bag.context
2018-01-03 21:07:51 +01:00
for spath in spaths:
config = config.getconfig(spath)
2018-08-17 23:11:25 +02:00
config_bag = self.config_bag.copy()
config_bag.context = config
2018-08-01 08:37:58 +02:00
return TiramisuAPI(config_bag)
2018-01-03 21:07:51 +01:00
2018-04-07 20:15:19 +02:00
class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption):
subhelp = TiramisuOption
2018-08-01 08:37:58 +02:00
def __call__(self,
path: str,
index: Optional[int]=None) -> TiramisuOption:
2018-04-07 20:15:19 +02:00
"""select a option (index only for slave option)"""
2018-08-02 22:35:40 +02:00
subconfig, name = self.config_bag.context.cfgimpl_get_home_by_path(path,
self.config_bag)
2017-12-27 15:48:49 +01:00
return TiramisuOption(name,
path,
index,
2017-12-27 15:48:49 +01:00
subconfig,
2018-08-01 08:37:58 +02:00
self.config_bag)
2017-10-22 09:48:08 +02:00
2018-08-14 22:15:40 +02:00
class Config(TiramisuAPI):
def __init__(self,
descr: OptionDescription,
session_id: str=None,
persistent: bool=False,
storage=None) -> None:
if not isinstance(descr, KernelConfig):
config = KernelConfig(descr,
session_id=session_id,
persistent=persistent,
storage=storage)
else:
config = descr
super().__init__(config)
class MetaConfig(TiramisuAPI):
def __init__(self,
children,
session_id: Union[str, None]=None,
persistent: bool=False,
optiondescription: Union[OptionDescription, None]=None) -> None:
_children = []
for child in children:
if isinstance(child, TiramisuAPI):
_children.append(child._config)
else:
_children.append(child)
config = KernelMetaConfig(_children,
session_id=session_id,
persistent=persistent,
optiondescription=optiondescription)
super().__init__(config)
2017-10-22 09:48:08 +02:00
2018-08-14 22:15:40 +02:00
class GroupConfig(TiramisuAPI):
def __init__(self,
children,
session_id: Union[str, None]=None,
persistent: bool=False,
storage=None) -> None:
_children = []
for child in children:
_children.append(child._config)
config = KernelGroupConfig(_children,
session_id=session_id,
persistent=persistent,
storage=storage)
super().__init__(config)
def getapi(config: Config):
"""instanciate Config
:param config: KernelConfig object
:type descr: an instance of ``config.KernelConfig``
2017-10-22 09:48:08 +02:00
"""
2018-08-14 22:15:40 +02:00
return config