todict works with Metaconfig

This commit is contained in:
Emmanuel Garette 2019-08-03 21:03:23 +02:00
parent 1691e38db5
commit 113b7a3410
3 changed files with 51 additions and 46 deletions

View file

@ -1,6 +1,6 @@
# from json import dumps, loads # from json import dumps, loads
try: try:
from tiramisu_json_api import Config from tiramisu_api import Config
class TestConfig(Config): class TestConfig(Config):
def __init__(self, def __init__(self,
config): config):

View file

@ -8,6 +8,7 @@ from tiramisu import IntOption, StrOption, NetworkOption, NetmaskOption, BoolOpt
Params, ParamOption, ParamValue 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
from .config import config_type, get_config
def teardown_function(function): def teardown_function(function):
@ -76,8 +77,6 @@ def test_path():
assert meta.config('conf2').config.path() == 'meta.conf2' 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(): def test_none():
meta = make_metaconfig() 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 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() assert meta.config(None).config.name() == meta.config.name()
def test_reset(): def test_metaconfig_reset(config_type):
meta = make_metaconfig() meta = make_metaconfig()
assert meta.option('od1.i2').value.get() == 1 meta_api = get_config(meta, config_type)
meta.option('od1.i2').value.set(2) 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) meta.config('conf1').option('od1.i2').value.set(3)
assert meta.option('od1.i2').value.get() == 2 assert meta.option('od1.i2').value.get() == 2
assert meta.config('conf1').option('od1.i2').value.get() == 3 assert meta.config('conf1').option('od1.i2').value.get() == 3

View file

@ -712,9 +712,29 @@ class _TiramisuOption(CommonTiramisu):
self._option_bag, self._option_bag,
config) config)
raise APIError(_('please specify a valid sub function ({})').format(subfunc)) # pragma: no cover 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, def find(self,
name: str, name: str,
value=undefined, value=undefined,
@ -811,10 +831,10 @@ class _TiramisuOptionDescription(_TiramisuOption):
clearable: str="all", clearable: str="all",
remotable: str="minimum"): remotable: str="minimum"):
root = self._get_option().impl_getpath() root = self._get_option().impl_getpath()
self._tiramisu_dict = TiramisuDict(Config(self._config_bag.context), self._tiramisu_dict = TiramisuDict(self._return_config(self._config_bag.context),
root=root, root=root,
clearable=clearable, clearable=clearable,
remotable=remotable) remotable=remotable)
def dict(self, def dict(self,
clearable: str="all", clearable: str="all",
@ -863,29 +883,9 @@ class TiramisuOption(CommonTiramisuOption):
config_bag=config_bag) config_bag=config_bag)
#__________________________________________________________________________________________________
#
class TiramisuContext(TiramisuHelp): class TiramisuContextInformation(TiramisuConfig):
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):
"""Manage config informations""" """Manage config informations"""
def get(self, name, default=undefined): def get(self, name, default=undefined):
"""Get an information""" """Get an information"""
@ -904,7 +904,7 @@ class TiramisuContextInformation(TiramisuContext):
return self._config_bag.context.impl_list_information() return self._config_bag.context.impl_list_information()
class TiramisuContextValue(TiramisuContext): class TiramisuContextValue(TiramisuConfig):
"""Manage config value""" """Manage config value"""
def mandatory(self): def mandatory(self):
"""Return path of options with mandatory property without any value""" """Return path of options with mandatory property without any value"""
@ -988,7 +988,7 @@ class TiramisuContextValue(TiramisuContext):
True) True)
class TiramisuContextOwner(TiramisuContext): class TiramisuContextOwner(TiramisuConfig):
"""Global owner""" """Global owner"""
def get(self): def get(self):
@ -1005,7 +1005,7 @@ class TiramisuContextOwner(TiramisuContext):
self._config_bag.context.cfgimpl_get_values().set_context_owner(obj_owner) self._config_bag.context.cfgimpl_get_values().set_context_owner(obj_owner)
class TiramisuContextProperty(TiramisuContext): class TiramisuContextProperty(TiramisuConfig):
"""Manage config properties""" """Manage config properties"""
def read_only(self): def read_only(self):
@ -1136,7 +1136,7 @@ class TiramisuContextProperty(TiramisuContext):
raise ValueError(_('unknown type {}').format(type)) raise ValueError(_('unknown type {}').format(type))
class TiramisuContextPermissive(TiramisuContext): class TiramisuContextPermissive(TiramisuConfig):
"""Manage config permissives""" """Manage config permissives"""
def get(self): def get(self):
@ -1180,7 +1180,7 @@ class TiramisuContextPermissive(TiramisuContext):
self.set(frozenset(props)) self.set(frozenset(props))
class TiramisuContextOption(TiramisuContext): class TiramisuContextOption(TiramisuConfig):
def __init__(self, def __init__(self,
*args, *args,
**kwargs) -> None: **kwargs) -> None:
@ -1286,7 +1286,7 @@ class TiramisuContextOption(TiramisuContext):
def _load_dict(self, def _load_dict(self,
clearable="all", clearable="all",
remotable="minimum"): remotable="minimum"):
self._tiramisu_dict = TiramisuDict(Config(self._config_bag.context), self._tiramisu_dict = TiramisuDict(self._return_config(self._config_bag.context),
root=None, root=None,
clearable=clearable, clearable=clearable,
remotable=remotable) remotable=remotable)
@ -1330,7 +1330,7 @@ class _TiramisuContextConfigReset():
self._config_bag.context.cfgimpl_reset_cache(None, None) self._config_bag.context.cfgimpl_reset_cache(None, None)
class _TiramisuContextConfig(TiramisuContext, _TiramisuContextConfigReset): class _TiramisuContextConfig(TiramisuConfig, _TiramisuContextConfigReset):
"""Actions to Config""" """Actions to Config"""
def name(self): def name(self):
return self._config_bag.context.impl_getname() return self._config_bag.context.impl_getname()
@ -1361,7 +1361,7 @@ class _TiramisuContextConfig(TiramisuContext, _TiramisuContextConfigReset):
return self._config_bag.context.cfgimpl_get_config_path() return self._config_bag.context.cfgimpl_get_config_path()
class _TiramisuContextGroupConfig(TiramisuContext): class _TiramisuContextGroupConfig(TiramisuConfig):
"""Actions to GroupConfig""" """Actions to GroupConfig"""
def name(self): def name(self):
"""Get config name""" """Get config name"""
@ -1421,9 +1421,12 @@ class _TiramisuContextGroupConfig(TiramisuContext):
class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextConfigReset): class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextConfigReset):
"""Actions to MixConfig""" """Actions to MixConfig"""
def pop(self, def pop(self,
session_id): session_id=None,
config=None):
"""Remove config from MetaConfig""" """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, def add(self,
config): config):
@ -1439,12 +1442,13 @@ class _TiramisuContextMetaConfig(_TiramisuContextMixConfig):
type='config'): type='config'):
"""Create and add a new config""" """Create and add a new config"""
new_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) return self._return_config(new_config)
class TiramisuContextCache(TiramisuContext):
class TiramisuContextCache(TiramisuConfig):
def reset(self): def reset(self):
self._config_bag.context.cfgimpl_reset_cache(None, None) self._config_bag.context.cfgimpl_reset_cache(None, None)
@ -1521,7 +1525,6 @@ class TiramisuDispatcherOption(TiramisuDispatcher, TiramisuContextOption):
self._config_bag) self._config_bag)
#__________________________________________________________________________________________________
class Config(TiramisuAPI): class Config(TiramisuAPI):
"""Root config object that enables us to handle the configuration options""" """Root config object that enables us to handle the configuration options"""
def __init__(self, def __init__(self,