can add new config with mixconfig type

This commit is contained in:
Emmanuel Garette 2019-06-05 12:33:00 +02:00
parent e7cbd7849a
commit 07e6ea5eb2
3 changed files with 91 additions and 57 deletions

View file

@ -6,7 +6,7 @@ from py.test import raises
from tiramisu.setting import groups, owners from tiramisu.setting import groups, owners
from tiramisu import IntOption, StrOption, NetworkOption, NetmaskOption, \ from tiramisu import IntOption, StrOption, NetworkOption, NetmaskOption, \
OptionDescription, Leadership, Config, GroupConfig, MixConfig, \ OptionDescription, Leadership, Config, GroupConfig, MixConfig, \
Params, ParamOption, ParamValue MetaConfig, Params, ParamOption, ParamValue
from tiramisu.error import ConfigError, ConflictError, PropertiesOptionError, LeadershipError, APIError from tiramisu.error import ConfigError, ConflictError, PropertiesOptionError, LeadershipError, APIError
from tiramisu.storage import list_sessions from tiramisu.storage import list_sessions
@ -1122,3 +1122,10 @@ def test_mix_add_config_readd():
config = Config(od, session_id='new') config = Config(od, session_id='new')
mix.config.add(config) mix.config.add(config)
raises(ConflictError, "mix2.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)

View file

@ -802,6 +802,18 @@ class TiramisuContext(TiramisuHelp):
config_bag: Optional[ConfigBag]) -> None: config_bag: Optional[ConfigBag]) -> None:
self._config_bag = config_bag 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): class TiramisuContextInformation(TiramisuContext):
"""Manage config informations""" """Manage config informations"""
@ -1253,7 +1265,7 @@ class _TiramisuContextConfig(TiramisuContext, _TiramisuContextConfigReset):
session_id=None, session_id=None,
persistent=False, persistent=False,
storage=None): storage=None):
return Config(self._config_bag.context.duplicate(session_id, return self._return_config(self._config_bag.context.duplicate(session_id,
persistent=persistent, persistent=persistent,
storage=storage)) storage=storage))
@ -1262,14 +1274,14 @@ class _TiramisuContextConfig(TiramisuContext, _TiramisuContextConfigReset):
persistent=False, persistent=False,
storage=None, storage=None,
metaconfig_prefix=None): metaconfig_prefix=None):
return Config(self._config_bag.context.duplicate(session_id, return self._return_config(self._config_bag.context.duplicate(session_id,
persistent=persistent, persistent=persistent,
storage=storage, storage=storage,
metaconfig_prefix=metaconfig_prefix, metaconfig_prefix=metaconfig_prefix,
deep=True)) deep=True))
def metaconfig(self): 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): def path(self):
return self._config_bag.context.cfgimpl_get_config_path() return self._config_bag.context.cfgimpl_get_config_path()
@ -1284,7 +1296,7 @@ class _TiramisuContextGroupConfig(TiramisuContext):
def list(self): def list(self):
"""List children's config""" """List children's config"""
for child in self._config_bag.context.cfgimpl_get_children(): for child in self._config_bag.context.cfgimpl_get_children():
yield Config(child) yield self._return_config(child)
def find(self, def find(self,
name: str, name: str,
@ -1298,18 +1310,18 @@ class _TiramisuContextGroupConfig(TiramisuContext):
path: Optional[str]): path: Optional[str]):
"""select a child Tiramisu config""" """select a child Tiramisu config"""
if path is None: if path is None:
return Config(self._config_bag) return self._return_config(self._config_bag.context)
spaths = path.split('.') spaths = path.split('.')
config = self._config_bag.context config = self._config_bag.context
for spath in spaths: for spath in spaths:
config = config.getconfig(spath) config = config.getconfig(spath)
return Config(config) return self._return_config(config)
def copy(self, def copy(self,
session_id=None, session_id=None,
persistent=False, persistent=False,
storage=None): storage=None):
return Config(self._config_bag.context.duplicate(session_id, return self._return_config(self._config_bag.context.duplicate(session_id,
persistent=persistent, persistent=persistent,
storage=storage)) storage=storage))
@ -1318,7 +1330,7 @@ class _TiramisuContextGroupConfig(TiramisuContext):
persistent=False, persistent=False,
storage=None, storage=None,
metaconfig_prefix=None): metaconfig_prefix=None):
return Config(self._config_bag.context.duplicate(session_id, return self._return_config(self._config_bag.context.duplicate(session_id,
persistent=persistent, persistent=persistent,
storage=storage, storage=storage,
metaconfig_prefix=metaconfig_prefix, metaconfig_prefix=metaconfig_prefix,
@ -1333,12 +1345,12 @@ class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextCon
def pop(self, def pop(self,
session_id): session_id):
"""Remove config from MetaConfig""" """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, def add(self,
config): config):
"""Add config from MetaConfig""" """Add config from MetaConfig"""
return Config(self._config_bag.context.add_config(config)) self._config_bag.context.add_config(config)
class _TiramisuContextMetaConfig(_TiramisuContextMixConfig): class _TiramisuContextMetaConfig(_TiramisuContextMixConfig):
@ -1348,9 +1360,10 @@ class _TiramisuContextMetaConfig(_TiramisuContextMixConfig):
persistent=False, persistent=False,
type='config'): type='config'):
"""Create and add a new 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, persistent=persistent,
type_=type)) type_=type)
return self._return_config(new_config)
class TiramisuContextCache(TiramisuContext): class TiramisuContextCache(TiramisuContext):
@ -1438,13 +1451,13 @@ class Config(TiramisuAPI):
session_id: str=None, session_id: str=None,
persistent: bool=False, persistent: bool=False,
storage=None) -> None: storage=None) -> None:
if isinstance(descr, OptionDescription): if isinstance(descr, KernelConfig):
config = descr
else:
config = KernelConfig(descr, config = KernelConfig(descr,
session_id=session_id, session_id=session_id,
persistent=persistent, persistent=persistent,
storage=storage) storage=storage)
else:
config = descr
super().__init__(config) super().__init__(config)
@ -1455,6 +1468,9 @@ class MetaConfig(TiramisuAPI):
session_id: Union[str, None]=None, session_id: Union[str, None]=None,
persistent: bool=False, persistent: bool=False,
optiondescription: Optional[OptionDescription]=None) -> None: optiondescription: Optional[OptionDescription]=None) -> None:
if isinstance(children, KernelMetaConfig):
config = children
else:
_children = [] _children = []
for child in children: for child in children:
if isinstance(child, TiramisuAPI): if isinstance(child, TiramisuAPI):
@ -1476,6 +1492,9 @@ class MixConfig(TiramisuAPI):
children: List[Config], children: List[Config],
session_id: Optional[str]=None, session_id: Optional[str]=None,
persistent: bool=False) -> None: persistent: bool=False) -> None:
if isinstance(children, KernelMixConfig):
config = children
else:
_children = [] _children = []
for child in children: for child in children:
if isinstance(child, TiramisuAPI): if isinstance(child, TiramisuAPI):
@ -1495,6 +1514,9 @@ class GroupConfig(TiramisuAPI):
def __init__(self, def __init__(self,
children, children,
session_id: Optional[str]=None) -> None: session_id: Optional[str]=None) -> None:
if isinstance(children, KernelGroupConfig):
config = children
else:
_children = [] _children = []
for child in children: for child in children:
if isinstance(child, TiramisuAPI): if isinstance(child, TiramisuAPI):

View file

@ -1246,7 +1246,7 @@ class KernelMetaConfig(KernelMixConfig):
if session_id in [child.impl_getname() for child in self._impl_children]: if session_id in [child.impl_getname() for child in self._impl_children]:
raise ConflictError(_('config name must be uniq in ' raise ConflictError(_('config name must be uniq in '
'groupconfig for {0}').format(session_id)) '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() new = not persistent or session_id not in list_sessions()
if type_ == 'config': if type_ == 'config':
config = KernelConfig(self._impl_descr, config = KernelConfig(self._impl_descr,
@ -1257,6 +1257,11 @@ class KernelMetaConfig(KernelMixConfig):
optiondescription=self._impl_descr, optiondescription=self._impl_descr,
session_id=session_id, session_id=session_id,
persistent=persistent) persistent=persistent)
elif type_ == 'mixconfig':
config = KernelMixConfig(children=[],
optiondescription=self._impl_descr,
session_id=session_id,
persistent=persistent)
# Copy context properties/permissives # Copy context properties/permissives
if new: if new:
config.cfgimpl_get_settings().set_context_properties(self.cfgimpl_get_settings().get_context_properties(), config) config.cfgimpl_get_settings().set_context_properties(self.cfgimpl_get_settings().get_context_properties(), config)