config => KernelConfig + Config in api
This commit is contained in:
@ -12,11 +12,10 @@
|
||||
#
|
||||
# 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/>.
|
||||
from .config import Config, MetaConfig, GroupConfig
|
||||
from .function import Params, ParamOption, ParamValue, ParamContext
|
||||
from .option import *
|
||||
from .error import APIError
|
||||
from .api import getapi
|
||||
from .api import getapi, Config, MetaConfig, GroupConfig
|
||||
from .option import __all__ as all_options
|
||||
from .setting import owners, undefined
|
||||
|
||||
|
109
tiramisu/api.py
109
tiramisu/api.py
@ -23,7 +23,7 @@ from typing import List, Any, Optional, Callable, Union, Dict
|
||||
from .error import APIError, ConfigError, SlaveError, PropertiesOptionError
|
||||
from .i18n import _
|
||||
from .setting import ConfigBag, OptionBag, owners, groups, Undefined, undefined, FORBIDDEN_SET_PROPERTIES
|
||||
from .config import Config, SubConfig, GroupConfig, MetaConfig
|
||||
from .config import KernelConfig, SubConfig, KernelGroupConfig, KernelMetaConfig
|
||||
from .option import ChoiceOption, OptionDescription
|
||||
|
||||
|
||||
@ -138,7 +138,7 @@ class CommonTiramisuOption(CommonTiramisu):
|
||||
|
||||
def __init__(self,
|
||||
name: str,
|
||||
subconfig: Union[Config, SubConfig],
|
||||
subconfig: Union[KernelConfig, SubConfig],
|
||||
option_bag: OptionBag) -> None:
|
||||
self.option_bag = option_bag
|
||||
self._name = name
|
||||
@ -256,7 +256,7 @@ class TiramisuOptionOwner(CommonTiramisuOption):
|
||||
|
||||
def __init__(self,
|
||||
name: str,
|
||||
subconfig: Union[Config, SubConfig],
|
||||
subconfig: Union[KernelConfig, SubConfig],
|
||||
option_bag: OptionBag) -> None:
|
||||
|
||||
super().__init__(name,
|
||||
@ -293,7 +293,7 @@ class TiramisuOptionProperty(CommonTiramisuOption):
|
||||
|
||||
def __init__(self,
|
||||
name: str,
|
||||
subconfig: Union[Config, SubConfig],
|
||||
subconfig: Union[KernelConfig, SubConfig],
|
||||
option_bag: OptionBag) -> None:
|
||||
super().__init__(name,
|
||||
subconfig,
|
||||
@ -345,7 +345,7 @@ class TiramisuOptionPermissive(CommonTiramisuOption):
|
||||
|
||||
def __init__(self,
|
||||
name: str,
|
||||
subconfig: Union[Config, SubConfig],
|
||||
subconfig: Union[KernelConfig, SubConfig],
|
||||
option_bag: OptionBag) -> None:
|
||||
super().__init__(name,
|
||||
subconfig,
|
||||
@ -483,7 +483,7 @@ class TiramisuOption(CommonTiramisu):
|
||||
name: Optional[str],
|
||||
path: Optional[str]=None,
|
||||
index: Optional[int]=None,
|
||||
subconfig: Union[None, Config, SubConfig]=None,
|
||||
subconfig: Union[None, KernelConfig, SubConfig]=None,
|
||||
config_bag: Optional[ConfigBag]=None,
|
||||
option_bag: Optional[OptionBag]=None) -> None:
|
||||
self._name = name
|
||||
@ -915,6 +915,34 @@ class TiramisuContextConfig(TiramisuContext):
|
||||
else:
|
||||
raise APIError('not implemented yet')
|
||||
|
||||
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):
|
||||
self.config_bag.context.new_config(name)
|
||||
|
||||
def _m_list(self):
|
||||
return self._g_list()
|
||||
|
||||
def _g_list(self):
|
||||
return self.config_bag.context.cfgimpl_get_children()
|
||||
|
||||
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))
|
||||
|
||||
|
||||
class TiramisuDispatcher:
|
||||
pass
|
||||
@ -924,7 +952,7 @@ class TiramisuAPI(TiramisuHelp):
|
||||
registers = {}
|
||||
|
||||
def __init__(self,
|
||||
config: Union[Config, GroupConfig, MetaConfig, ConfigBag]) -> None:
|
||||
config) -> None:
|
||||
self._config = config
|
||||
if not self.registers:
|
||||
registers(self.registers, 'TiramisuContext')
|
||||
@ -967,7 +995,7 @@ class TiramisuAPI(TiramisuHelp):
|
||||
|
||||
class TiramisuDispatcherConfig(TiramisuDispatcher, TiramisuContextConfig):
|
||||
def __call__(self,
|
||||
path: Optional[str]) -> TiramisuAPI:
|
||||
path: Optional[str]):
|
||||
"""select a child Tiramisu configuration (only with MetaConfig or GroupConfig)"""
|
||||
if path is None:
|
||||
return TiramisuAPI(self.config_bag)
|
||||
@ -996,10 +1024,65 @@ class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption):
|
||||
self.config_bag)
|
||||
|
||||
|
||||
def getapi(config: Union[Config, GroupConfig, MetaConfig]):
|
||||
"""instanciate TiramisuAPI
|
||||
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)
|
||||
|
||||
:param config: Config object
|
||||
:type descr: an instance of ``config.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)
|
||||
|
||||
|
||||
|
||||
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``
|
||||
"""
|
||||
return TiramisuAPI(config)
|
||||
return config
|
||||
|
@ -93,7 +93,7 @@ def manager_callback(callbk: Union[ParamOption, ParamValue],
|
||||
return value[index]
|
||||
return value
|
||||
except PropertiesOptionError as err:
|
||||
if callbk.notraiseproperty:
|
||||
if callbk.notraisepropertyerror:
|
||||
raise err
|
||||
raise ConfigError(_('unable to carry out a calculation for "{}"'
|
||||
', {}').format(option.impl_get_display_name(), err))
|
||||
|
@ -602,7 +602,7 @@ class SubConfig(object):
|
||||
|
||||
|
||||
class _CommonConfig(SubConfig):
|
||||
"abstract base class for the Config, GroupConfig and the MetaConfig"
|
||||
"abstract base class for the Config, KernelGroupConfig and the KernelMetaConfig"
|
||||
__slots__ = ('_impl_values',
|
||||
'_impl_settings',
|
||||
'_impl_meta')
|
||||
@ -644,10 +644,10 @@ class _CommonConfig(SubConfig):
|
||||
raise NotImplementedError()
|
||||
|
||||
def _gen_fake_values(self):
|
||||
fake_config = Config(self._impl_descr,
|
||||
persistent=False,
|
||||
force_values=get_default_values_storages(),
|
||||
force_settings=self.cfgimpl_get_settings())
|
||||
fake_config = KernelConfig(self._impl_descr,
|
||||
persistent=False,
|
||||
force_values=get_default_values_storages(),
|
||||
force_settings=self.cfgimpl_get_settings())
|
||||
fake_config.cfgimpl_get_values()._p_.importation(self.cfgimpl_get_values()._p_.exportation())
|
||||
return fake_config
|
||||
|
||||
@ -656,12 +656,12 @@ class _CommonConfig(SubConfig):
|
||||
force_values=None,
|
||||
force_settings=None,
|
||||
storage=None):
|
||||
config = Config(self._impl_descr,
|
||||
_duplicate=True,
|
||||
session_id=session_id,
|
||||
force_values=force_values,
|
||||
force_settings=force_settings,
|
||||
storage=storage)
|
||||
config = KernelConfig(self._impl_descr,
|
||||
_duplicate=True,
|
||||
session_id=session_id,
|
||||
force_values=force_values,
|
||||
force_settings=force_settings,
|
||||
storage=storage)
|
||||
config.cfgimpl_get_values()._p_.importation(self.cfgimpl_get_values()._p_.exportation())
|
||||
config.cfgimpl_get_settings()._p_.importation(self.cfgimpl_get_settings(
|
||||
)._p_.exportation())
|
||||
@ -671,7 +671,7 @@ class _CommonConfig(SubConfig):
|
||||
|
||||
|
||||
# ____________________________________________________________
|
||||
class Config(_CommonConfig):
|
||||
class KernelConfig(_CommonConfig):
|
||||
"main configuration management entry"
|
||||
__slots__ = ('__weakref__', '_impl_name')
|
||||
|
||||
@ -721,10 +721,10 @@ class Config(_CommonConfig):
|
||||
permissives)
|
||||
self._impl_values = Values(self,
|
||||
values)
|
||||
super(Config, self).__init__(descr,
|
||||
weakref.ref(self),
|
||||
ConfigBag(self),
|
||||
None)
|
||||
super(KernelConfig, self).__init__(descr,
|
||||
weakref.ref(self),
|
||||
ConfigBag(self),
|
||||
None)
|
||||
if _duplicate is False and (force_settings is None or force_values is None):
|
||||
self._impl_build_all_caches()
|
||||
self._impl_name = session_id
|
||||
@ -736,7 +736,7 @@ class Config(_CommonConfig):
|
||||
return self._impl_values._p_._storage.session_id
|
||||
|
||||
|
||||
class GroupConfig(_CommonConfig):
|
||||
class KernelGroupConfig(_CommonConfig):
|
||||
__slots__ = ('__weakref__',
|
||||
'_impl_children',
|
||||
'_impl_name')
|
||||
@ -753,7 +753,10 @@ class GroupConfig(_CommonConfig):
|
||||
for child in children:
|
||||
if not isinstance(child,
|
||||
_CommonConfig):
|
||||
raise ValueError(_("groupconfig's children must be Config, MetaConfig or GroupConfig"))
|
||||
try:
|
||||
child = child._config
|
||||
except:
|
||||
raise ValueError(_("groupconfig's children must be Config, MetaConfig or GroupConfig"))
|
||||
name_ = child._impl_name
|
||||
names.append(name_)
|
||||
if len(names) != len(set(names)):
|
||||
@ -772,7 +775,7 @@ class GroupConfig(_CommonConfig):
|
||||
permissives)
|
||||
self._impl_values = Values(self, values)
|
||||
self._impl_meta = None
|
||||
super(GroupConfig, self).__init__(_descr,
|
||||
super(KernelGroupConfig, self).__init__(_descr,
|
||||
weakref.ref(self),
|
||||
ConfigBag(self),
|
||||
None)
|
||||
@ -787,8 +790,8 @@ class GroupConfig(_CommonConfig):
|
||||
resetted_opts=None):
|
||||
if resetted_opts is None:
|
||||
resetted_opts = []
|
||||
if isinstance(self, MetaConfig):
|
||||
super(GroupConfig, self).cfgimpl_reset_cache(option_bag,
|
||||
if isinstance(self, KernelMetaConfig):
|
||||
super(KernelGroupConfig, self).cfgimpl_reset_cache(option_bag,
|
||||
resetted_opts=copy(resetted_opts))
|
||||
for child in self._impl_children:
|
||||
child.cfgimpl_reset_cache(option_bag,
|
||||
@ -801,14 +804,14 @@ class GroupConfig(_CommonConfig):
|
||||
config_bag,
|
||||
only_config=False,
|
||||
_commit=True):
|
||||
"""Setattr not in current GroupConfig, but in each children
|
||||
"""Setattr not in current KernelGroupConfig, but in each children
|
||||
"""
|
||||
ret = []
|
||||
for child in self._impl_children:
|
||||
cconfig_bag = config_bag.copy()
|
||||
cconfig_bag.context = child
|
||||
try:
|
||||
if isinstance(child, GroupConfig):
|
||||
if isinstance(child, KernelGroupConfig):
|
||||
ret.extend(child.set_value(path,
|
||||
index,
|
||||
value,
|
||||
@ -852,13 +855,13 @@ class GroupConfig(_CommonConfig):
|
||||
byvalue=undefined,
|
||||
raise_if_not_found=True,
|
||||
_sub=False):
|
||||
"""Find first not in current GroupConfig, but in each children
|
||||
"""Find first not in current KernelGroupConfig, but in each children
|
||||
"""
|
||||
#if MetaConfig, all children have same OptionDescription in context
|
||||
#if KernelMetaConfig, all children have same OptionDescription in context
|
||||
#so search only one time the option for all children
|
||||
if bypath is undefined and byname is not None and \
|
||||
isinstance(self,
|
||||
MetaConfig):
|
||||
KernelMetaConfig):
|
||||
bypath = next(self.find(bytype=None,
|
||||
byvalue=undefined,
|
||||
byname=byname,
|
||||
@ -869,7 +872,7 @@ class GroupConfig(_CommonConfig):
|
||||
|
||||
ret = []
|
||||
for child in self._impl_children:
|
||||
if isinstance(child, GroupConfig):
|
||||
if isinstance(child, KernelGroupConfig):
|
||||
ret.extend(child.find_firsts(byname=byname,
|
||||
bypath=bypath,
|
||||
byoption=byoption,
|
||||
@ -894,7 +897,7 @@ class GroupConfig(_CommonConfig):
|
||||
else:
|
||||
self._find_return_results(ret != [],
|
||||
raise_if_not_found)
|
||||
return GroupConfig(ret)
|
||||
return KernelGroupConfig(ret)
|
||||
|
||||
def impl_getname(self):
|
||||
return self._impl_name
|
||||
@ -907,7 +910,7 @@ class GroupConfig(_CommonConfig):
|
||||
raise ConfigError(_('unknown config "{}"').format(name))
|
||||
|
||||
|
||||
class MetaConfig(GroupConfig):
|
||||
class KernelMetaConfig(KernelGroupConfig):
|
||||
__slots__ = tuple()
|
||||
|
||||
def __init__(self,
|
||||
@ -919,15 +922,18 @@ class MetaConfig(GroupConfig):
|
||||
if optiondescription is not None:
|
||||
new_children = []
|
||||
for child_session_id in children:
|
||||
new_children.append(Config(optiondescription,
|
||||
persistent=persistent,
|
||||
session_id=child_session_id))
|
||||
new_children.append(KernelConfig(optiondescription,
|
||||
persistent=persistent,
|
||||
session_id=child_session_id))
|
||||
children = new_children
|
||||
for child in children:
|
||||
if not isinstance(child, _CommonConfig):
|
||||
raise TypeError(_("metaconfig's children "
|
||||
"should be config, not {0}"
|
||||
).format(type(child)))
|
||||
try:
|
||||
child = child._config
|
||||
except:
|
||||
raise TypeError(_("metaconfig's children "
|
||||
"should be config, not {0}"
|
||||
).format(type(child)))
|
||||
if child.cfgimpl_get_meta() is not None:
|
||||
raise ValueError(_("child has already a metaconfig's"))
|
||||
if descr is None:
|
||||
@ -937,10 +943,10 @@ class MetaConfig(GroupConfig):
|
||||
'have the same optiondescription'))
|
||||
child._impl_meta = weakref.ref(self)
|
||||
|
||||
super(MetaConfig, self).__init__(children,
|
||||
session_id,
|
||||
persistent,
|
||||
descr)
|
||||
super(KernelMetaConfig, self).__init__(children,
|
||||
session_id,
|
||||
persistent,
|
||||
descr)
|
||||
|
||||
def set_value(self,
|
||||
path,
|
||||
@ -953,19 +959,19 @@ class MetaConfig(GroupConfig):
|
||||
only_config=False,
|
||||
_commit=True):
|
||||
"""only_config: could be set if you want modify value in all Config included in
|
||||
this MetaConfig
|
||||
this KernelMetaConfig
|
||||
"""
|
||||
if only_config:
|
||||
if force_default or force_default_if_same or force_dont_change_value:
|
||||
raise ValueError(_('force_default, force_default_if_same or '
|
||||
'force_dont_change_value cannot be set with'
|
||||
' only_config'))
|
||||
return super(MetaConfig, self).set_value(path,
|
||||
index,
|
||||
value,
|
||||
config_bag,
|
||||
only_config=only_config,
|
||||
_commit=_commit)
|
||||
return super(KernelMetaConfig, self).set_value(path,
|
||||
index,
|
||||
value,
|
||||
config_bag,
|
||||
only_config=only_config,
|
||||
_commit=_commit)
|
||||
ret = []
|
||||
if force_default or force_default_if_same or force_dont_change_value:
|
||||
if force_default and force_dont_change_value:
|
||||
@ -1047,9 +1053,9 @@ class MetaConfig(GroupConfig):
|
||||
def new_config(self,
|
||||
session_id,
|
||||
persistent=False):
|
||||
config = Config(self._impl_descr,
|
||||
session_id=session_id,
|
||||
persistent=persistent)
|
||||
config = KernelConfig(self._impl_descr,
|
||||
session_id=session_id,
|
||||
persistent=persistent)
|
||||
|
||||
if config._impl_name in [child._impl_name for child in self._impl_children]: # pragma: no cover
|
||||
raise ConflictError(_('config name must be uniq in '
|
||||
|
@ -162,7 +162,7 @@ class ValueWarning(UserWarning): # pragma: optional cover
|
||||
>>> import warnings
|
||||
>>> from tiramisu.error import ValueWarning
|
||||
>>> from tiramisu.option import StrOption, OptionDescription
|
||||
>>> from tiramisu.config import Config
|
||||
>>> from tiramisu import Config
|
||||
>>> warnings.simplefilter("always", ValueWarning)
|
||||
>>> def a(val):
|
||||
... raise ValueError('pouet')
|
||||
|
@ -58,7 +58,7 @@ class ParamOption(Param):
|
||||
).format(type(notraisepropertyerror)))
|
||||
|
||||
self.option = cur_opt
|
||||
self.notraiseproperty = notraisepropertyerror
|
||||
self.notraisepropertyerror = notraisepropertyerror
|
||||
|
||||
|
||||
class ParamValue(Param):
|
||||
|
@ -4,7 +4,7 @@ from .syndynoptiondescription import SynDynOptionDescription
|
||||
from .masterslave import MasterSlaves
|
||||
from .baseoption import submulti
|
||||
from .symlinkoption import SymLinkOption, DynSymLinkOption
|
||||
from .option import Option
|
||||
from .option import Option, RegexpOption
|
||||
from .choiceoption import ChoiceOption
|
||||
from .booloption import BoolOption
|
||||
from .intoption import IntOption
|
||||
@ -30,4 +30,4 @@ __all__ = ('MasterSlaves', 'OptionDescription', 'DynOptionDescription',
|
||||
'IntOption', 'FloatOption', 'StrOption', 'UnicodeOption',
|
||||
'IPOption', 'PortOption', 'NetworkOption', 'NetmaskOption',
|
||||
'BroadcastOption', 'DomainnameOption', 'EmailOption', 'URLOption',
|
||||
'UsernameOption', 'FilenameOption', 'PasswordOption', 'submulti')
|
||||
'UsernameOption', 'FilenameOption', 'PasswordOption', 'submulti', 'RegexpOption')
|
||||
|
Reference in New Issue
Block a user