diff --git a/tests/test_mixconfig.py b/tests/test_mixconfig.py index 9fe1059..259e973 100644 --- a/tests/test_mixconfig.py +++ b/tests/test_mixconfig.py @@ -6,7 +6,7 @@ from py.test import raises from tiramisu.setting import groups, owners from tiramisu import IntOption, StrOption, NetworkOption, NetmaskOption, \ OptionDescription, Leadership, Config, GroupConfig, MixConfig, \ - Params, ParamOption, ParamValue + MetaConfig, Params, ParamOption, ParamValue from tiramisu.error import ConfigError, ConflictError, PropertiesOptionError, LeadershipError, APIError from tiramisu.storage import list_sessions @@ -1122,3 +1122,10 @@ def test_mix_add_config_readd(): config = Config(od, session_id='new') mix.config.add(config) raises(ConflictError, "mix2.config.add(config)") + + +def test_meta_new_mixconfig(): + od = make_description() + conf = Config(od, session_id='conf1') + meta = MetaConfig([conf]) + assert isinstance(meta.config.new('mixconfig', type="mixconfig"), MixConfig) diff --git a/tiramisu/api.py b/tiramisu/api.py index ce8ffe0..485fde3 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -802,6 +802,18 @@ class TiramisuContext(TiramisuHelp): config_bag: Optional[ConfigBag]) -> None: self._config_bag = config_bag + def _return_config(self, + config): + if isinstance(config, KernelConfig): + return Config(config) + if isinstance(config, KernelMetaConfig): + return MetaConfig(config) + if isinstance(config, KernelMixConfig): + return MixConfig([], config) + if isinstance(config, KernelGroupConfig): + return GroupConfig(config) + raise Exception(_('unknown config type {}').format(type(config))) + class TiramisuContextInformation(TiramisuContext): """Manage config informations""" @@ -1253,23 +1265,23 @@ class _TiramisuContextConfig(TiramisuContext, _TiramisuContextConfigReset): session_id=None, persistent=False, storage=None): - return Config(self._config_bag.context.duplicate(session_id, - persistent=persistent, - storage=storage)) + return self._return_config(self._config_bag.context.duplicate(session_id, + persistent=persistent, + storage=storage)) def deepcopy(self, session_id=None, persistent=False, storage=None, metaconfig_prefix=None): - return Config(self._config_bag.context.duplicate(session_id, - persistent=persistent, - storage=storage, - metaconfig_prefix=metaconfig_prefix, - deep=True)) + return self._return_config(self._config_bag.context.duplicate(session_id, + persistent=persistent, + storage=storage, + metaconfig_prefix=metaconfig_prefix, + deep=True)) def metaconfig(self): - return Config(self._config_bag.context.cfgimpl_get_meta()) + return self._return_config(self._config_bag.context.cfgimpl_get_meta()) def path(self): return self._config_bag.context.cfgimpl_get_config_path() @@ -1284,7 +1296,7 @@ class _TiramisuContextGroupConfig(TiramisuContext): def list(self): """List children's config""" for child in self._config_bag.context.cfgimpl_get_children(): - yield Config(child) + yield self._return_config(child) def find(self, name: str, @@ -1298,31 +1310,31 @@ class _TiramisuContextGroupConfig(TiramisuContext): path: Optional[str]): """select a child Tiramisu config""" if path is None: - return Config(self._config_bag) + return self._return_config(self._config_bag.context) spaths = path.split('.') config = self._config_bag.context for spath in spaths: config = config.getconfig(spath) - return Config(config) + return self._return_config(config) def copy(self, session_id=None, persistent=False, storage=None): - return Config(self._config_bag.context.duplicate(session_id, - persistent=persistent, - storage=storage)) + return self._return_config(self._config_bag.context.duplicate(session_id, + persistent=persistent, + storage=storage)) def deepcopy(self, session_id=None, persistent=False, storage=None, metaconfig_prefix=None): - return Config(self._config_bag.context.duplicate(session_id, - persistent=persistent, - storage=storage, - metaconfig_prefix=metaconfig_prefix, - deep=True)) + return self._return_config(self._config_bag.context.duplicate(session_id, + persistent=persistent, + storage=storage, + metaconfig_prefix=metaconfig_prefix, + deep=True)) def path(self): return self._config_bag.context.cfgimpl_get_config_path() @@ -1333,12 +1345,12 @@ class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextCon def pop(self, session_id): """Remove config from MetaConfig""" - return Config(self._config_bag.context.pop_config(session_id=session_id)) + return self._return_config(self._config_bag.context.pop_config(session_id=session_id)) def add(self, config): """Add config from MetaConfig""" - return Config(self._config_bag.context.add_config(config)) + self._config_bag.context.add_config(config) class _TiramisuContextMetaConfig(_TiramisuContextMixConfig): @@ -1348,9 +1360,10 @@ class _TiramisuContextMetaConfig(_TiramisuContextMixConfig): persistent=False, type='config'): """Create and add a new config""" - return Config(self._config_bag.context.new_config(session_id=session_id, + new_config = self._config_bag.context.new_config(session_id=session_id, persistent=persistent, - type_=type)) + type_=type) + return self._return_config(new_config) class TiramisuContextCache(TiramisuContext): @@ -1438,13 +1451,13 @@ class Config(TiramisuAPI): session_id: str=None, persistent: bool=False, storage=None) -> None: - if isinstance(descr, OptionDescription): + if isinstance(descr, KernelConfig): + config = descr + else: config = KernelConfig(descr, session_id=session_id, persistent=persistent, storage=storage) - else: - config = descr super().__init__(config) @@ -1455,17 +1468,20 @@ class MetaConfig(TiramisuAPI): session_id: Union[str, None]=None, persistent: bool=False, optiondescription: Optional[OptionDescription]=None) -> None: - _children = [] - for child in children: - if isinstance(child, TiramisuAPI): - _children.append(child._config_bag.context) - else: - _children.append(child) + 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, - session_id=session_id, - persistent=persistent, - optiondescription=optiondescription) + config = KernelMetaConfig(_children, + session_id=session_id, + persistent=persistent, + optiondescription=optiondescription) super().__init__(config) @@ -1476,17 +1492,20 @@ class MixConfig(TiramisuAPI): children: List[Config], session_id: Optional[str]=None, persistent: bool=False) -> None: - _children = [] - for child in children: - if isinstance(child, TiramisuAPI): - _children.append(child._config_bag.context) - else: - _children.append(child) + 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, - session_id=session_id, - persistent=persistent) + config = KernelMixConfig(optiondescription, + _children, + session_id=session_id, + persistent=persistent) super().__init__(config) @@ -1495,13 +1514,16 @@ class GroupConfig(TiramisuAPI): def __init__(self, children, session_id: Optional[str]=None) -> None: - _children = [] - for child in children: - if isinstance(child, TiramisuAPI): - _children.append(child._config_bag.context) - else: - _children.append(child) + 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) - config = KernelGroupConfig(_children, - session_id=session_id) + config = KernelGroupConfig(_children, + session_id=session_id) super().__init__(config) diff --git a/tiramisu/config.py b/tiramisu/config.py index 8e1c222..0776183 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -1246,7 +1246,7 @@ class KernelMetaConfig(KernelMixConfig): if session_id in [child.impl_getname() for child in self._impl_children]: raise ConflictError(_('config name must be uniq in ' 'groupconfig for {0}').format(session_id)) - assert type_ in ('config', 'metaconfig'), _('unknown type {}').format(type_) + assert type_ in ('config', 'metaconfig', 'mixconfig'), _('unknown type {}').format(type_) new = not persistent or session_id not in list_sessions() if type_ == 'config': config = KernelConfig(self._impl_descr, @@ -1257,6 +1257,11 @@ class KernelMetaConfig(KernelMixConfig): optiondescription=self._impl_descr, session_id=session_id, persistent=persistent) + elif type_ == 'mixconfig': + config = KernelMixConfig(children=[], + optiondescription=self._impl_descr, + session_id=session_id, + persistent=persistent) # Copy context properties/permissives if new: config.cfgimpl_get_settings().set_context_properties(self.cfgimpl_get_settings().get_context_properties(), config)