From 113b7a3410ffa7cda8e05abd87852a3def4efdca Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 3 Aug 2019 21:03:23 +0200 Subject: [PATCH] todict works with Metaconfig --- tests/config.py | 2 +- tests/test_metaconfig.py | 12 +++--- tiramisu/api.py | 83 +++++++++++++++++++++------------------- 3 files changed, 51 insertions(+), 46 deletions(-) diff --git a/tests/config.py b/tests/config.py index b800a8a..6b90d94 100644 --- a/tests/config.py +++ b/tests/config.py @@ -1,6 +1,6 @@ # from json import dumps, loads try: - from tiramisu_json_api import Config + from tiramisu_api import Config class TestConfig(Config): def __init__(self, config): diff --git a/tests/test_metaconfig.py b/tests/test_metaconfig.py index 02f049e..4a3bbb6 100644 --- a/tests/test_metaconfig.py +++ b/tests/test_metaconfig.py @@ -8,6 +8,7 @@ from tiramisu import IntOption, StrOption, NetworkOption, NetmaskOption, BoolOpt Params, ParamOption, ParamValue from tiramisu.error import ConfigError, ConflictError, PropertiesOptionError, LeadershipError, APIError from tiramisu.storage import list_sessions +from .config import config_type, get_config def teardown_function(function): @@ -76,8 +77,6 @@ def test_path(): assert meta.config('conf2').config.path() == 'meta.conf2' -#FIXME ne pas mettre 2 meta dans une config -#FIXME ne pas mettre 2 OD differents dans un meta def test_none(): meta = make_metaconfig() assert meta.option('od1.i3').value.get() is meta.config('conf1').option('od1.i3').value.get() is meta.config('conf2').option('od1.i3').value.get() is None @@ -112,10 +111,13 @@ def test_none(): assert meta.config(None).config.name() == meta.config.name() -def test_reset(): +def test_metaconfig_reset(config_type): meta = make_metaconfig() - assert meta.option('od1.i2').value.get() == 1 - meta.option('od1.i2').value.set(2) + meta_api = get_config(meta, config_type) + assert meta_api.option('od1.i2').value.get() == 1 + meta_api.option('od1.i2').value.set(2) + if config_type == 'tiramisu-api': + meta_api.send() meta.config('conf1').option('od1.i2').value.set(3) assert meta.option('od1.i2').value.get() == 2 assert meta.config('conf1').option('od1.i2').value.get() == 3 diff --git a/tiramisu/api.py b/tiramisu/api.py index 4591cec..6617fe6 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -712,9 +712,29 @@ class _TiramisuOption(CommonTiramisu): self._option_bag, config) raise APIError(_('please specify a valid sub function ({})').format(subfunc)) # pragma: no cover +#__________________________________________________________________________________________________ +# -class _TiramisuOptionDescription(_TiramisuOption): +class TiramisuConfig(TiramisuHelp): + def __init__(self, + 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 _TiramisuOptionDescription(_TiramisuOption, TiramisuConfig): def find(self, name: str, value=undefined, @@ -811,10 +831,10 @@ class _TiramisuOptionDescription(_TiramisuOption): clearable: str="all", remotable: str="minimum"): root = self._get_option().impl_getpath() - self._tiramisu_dict = TiramisuDict(Config(self._config_bag.context), - root=root, - clearable=clearable, - remotable=remotable) + self._tiramisu_dict = TiramisuDict(self._return_config(self._config_bag.context), + root=root, + clearable=clearable, + remotable=remotable) def dict(self, clearable: str="all", @@ -863,29 +883,9 @@ class TiramisuOption(CommonTiramisuOption): config_bag=config_bag) -#__________________________________________________________________________________________________ -# -class TiramisuContext(TiramisuHelp): - def __init__(self, - 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): +class TiramisuContextInformation(TiramisuConfig): """Manage config informations""" def get(self, name, default=undefined): """Get an information""" @@ -904,7 +904,7 @@ class TiramisuContextInformation(TiramisuContext): return self._config_bag.context.impl_list_information() -class TiramisuContextValue(TiramisuContext): +class TiramisuContextValue(TiramisuConfig): """Manage config value""" def mandatory(self): """Return path of options with mandatory property without any value""" @@ -988,7 +988,7 @@ class TiramisuContextValue(TiramisuContext): True) -class TiramisuContextOwner(TiramisuContext): +class TiramisuContextOwner(TiramisuConfig): """Global owner""" def get(self): @@ -1005,7 +1005,7 @@ class TiramisuContextOwner(TiramisuContext): self._config_bag.context.cfgimpl_get_values().set_context_owner(obj_owner) -class TiramisuContextProperty(TiramisuContext): +class TiramisuContextProperty(TiramisuConfig): """Manage config properties""" def read_only(self): @@ -1136,7 +1136,7 @@ class TiramisuContextProperty(TiramisuContext): raise ValueError(_('unknown type {}').format(type)) -class TiramisuContextPermissive(TiramisuContext): +class TiramisuContextPermissive(TiramisuConfig): """Manage config permissives""" def get(self): @@ -1180,7 +1180,7 @@ class TiramisuContextPermissive(TiramisuContext): self.set(frozenset(props)) -class TiramisuContextOption(TiramisuContext): +class TiramisuContextOption(TiramisuConfig): def __init__(self, *args, **kwargs) -> None: @@ -1286,7 +1286,7 @@ class TiramisuContextOption(TiramisuContext): def _load_dict(self, clearable="all", remotable="minimum"): - self._tiramisu_dict = TiramisuDict(Config(self._config_bag.context), + self._tiramisu_dict = TiramisuDict(self._return_config(self._config_bag.context), root=None, clearable=clearable, remotable=remotable) @@ -1330,7 +1330,7 @@ class _TiramisuContextConfigReset(): self._config_bag.context.cfgimpl_reset_cache(None, None) -class _TiramisuContextConfig(TiramisuContext, _TiramisuContextConfigReset): +class _TiramisuContextConfig(TiramisuConfig, _TiramisuContextConfigReset): """Actions to Config""" def name(self): return self._config_bag.context.impl_getname() @@ -1361,7 +1361,7 @@ class _TiramisuContextConfig(TiramisuContext, _TiramisuContextConfigReset): return self._config_bag.context.cfgimpl_get_config_path() -class _TiramisuContextGroupConfig(TiramisuContext): +class _TiramisuContextGroupConfig(TiramisuConfig): """Actions to GroupConfig""" def name(self): """Get config name""" @@ -1421,9 +1421,12 @@ class _TiramisuContextGroupConfig(TiramisuContext): class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextConfigReset): """Actions to MixConfig""" def pop(self, - session_id): + session_id=None, + config=None): """Remove config from MetaConfig""" - return self._return_config(self._config_bag.context.pop_config(session_id=session_id)) + if __debug__ and None not in [session_id, config]: + raise APIError(_('cannot set session_id and config together')) + return self._return_config(self._config_bag.context.pop_config(session_id=session_id, config=config)) def add(self, config): @@ -1439,12 +1442,13 @@ class _TiramisuContextMetaConfig(_TiramisuContextMixConfig): type='config'): """Create and add a new config""" new_config = self._config_bag.context.new_config(session_id=session_id, - persistent=persistent, - type_=type) + persistent=persistent, + type_=type) return self._return_config(new_config) -class TiramisuContextCache(TiramisuContext): + +class TiramisuContextCache(TiramisuConfig): def reset(self): self._config_bag.context.cfgimpl_reset_cache(None, None) @@ -1521,7 +1525,6 @@ class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption): self._config_bag) -#__________________________________________________________________________________________________ class Config(TiramisuAPI): """Root config object that enables us to handle the configuration options""" def __init__(self,