From 8658fdd6ca6d5c2f4f2449b93870a0d0e0b0ca25 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 9 Jan 2022 19:10:09 +0100 Subject: [PATCH] option's informations avalaible in config --- tests/test_config.py | 12 ++++---- tiramisu/api.py | 43 ++++++++++++++++++---------- tiramisu/error.py | 1 - tiramisu/option/baseoption.py | 15 ++++++++-- tiramisu/storage/cacheobj.py | 4 +-- tiramisu/storage/dictionary/value.py | 3 +- 6 files changed, 49 insertions(+), 29 deletions(-) diff --git a/tests/test_config.py b/tests/test_config.py index 7d50837..ba8938a 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -154,10 +154,10 @@ async def test_information_config(): async with await Config(descr) as cfg: string = 'some informations' # - assert list(await cfg.information.list()) == [] + assert list(await cfg.information.list()) == ['doc'] await cfg.information.set('info', string) assert await cfg.information.get('info') == string - assert list(await cfg.information.list()) == ['info'] + assert set(await cfg.information.list()) == {'doc', 'info'} # with pytest.raises(ValueError): await cfg.information.get('noinfo') @@ -167,7 +167,7 @@ async def test_information_config(): await cfg.information.get('info') with pytest.raises(ValueError): await cfg.information.reset('noinfo') - assert list(await cfg.information.list()) == [] + assert list(await cfg.information.list()) == ['doc'] assert not await list_sessions() @@ -177,10 +177,10 @@ async def test_information_option(): async with await Config(descr) as cfg: string = 'some informations' # - list(await cfg.option('gc.name').information.list()) == [] + assert list(await cfg.option('gc.name').information.list()) == ['doc'] await cfg.option('gc.name').information.set('info', string) assert await cfg.option('gc.name').information.get('info') == string - list(await cfg.option('gc.name').information.list()) == ['info'] + assert set(await cfg.option('gc.name').information.list()) == {'doc', 'info'} # with pytest.raises(ValueError): await cfg.option('gc.name').information.get('noinfo') @@ -190,7 +190,7 @@ async def test_information_option(): await cfg.option('gc.name').information.get('info') with pytest.raises(ValueError): await cfg.option('gc.name').information.reset('noinfo') - list(await cfg.option('gc.name').information.list()) == [] + assert list(await cfg.option('gc.name').information.list()) == ['doc'] # assert await cfg.option('wantref').information.get('info') == 'default value' await cfg.option('wantref').information.set('info', 'default value') diff --git a/tiramisu/api.py b/tiramisu/api.py index 1448baa..8fecd15 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -92,7 +92,8 @@ class CommonTiramisu(TiramisuHelp): try: subconfig, name = await config_bag.context.cfgimpl_get_home_by_path(self._option_bag.path, config_bag, - validate_properties=self._validate_properties) + validate_properties=self._validate_properties, + ) except AssertionError as err: raise APIError(str(err)) except Exception as err: @@ -465,11 +466,14 @@ class TiramisuOptionInformation(CommonTiramisuOption): async def get(self, key, default=undefined): """Get information""" values = self._option_bag.config_bag.context.cfgimpl_get_values() - return await values.get_information(self._option_bag.config_bag, - self._option_bag, - key, - default, - ) + try: + return await values.get_information(self._option_bag.config_bag, + self._option_bag, + key, + undefined, + ) + except ValueError: + return self._option_bag.option.impl_get_information(key, default) @option_and_connection async def set(self, key, value): @@ -498,8 +502,11 @@ class TiramisuOptionInformation(CommonTiramisuOption): """List information's keys""" path = self._option_bag.path values = self._option_bag.config_bag.context.cfgimpl_get_values() - return await values.list_information(self._option_bag.config_bag.connection, - path) + lst1 = set(self._option_bag.option.impl_list_information()) + lst2 = set(await values.list_information(self._option_bag.config_bag.connection, + path, + )) + return lst1 | lst2 # # async def len(self): # """Length of leadership""" @@ -588,7 +595,8 @@ class TiramisuOptionValue(CommonTiramisuOption): if self._option_bag.option.impl_is_follower() and self._option_bag.index is None: raise APIError('index must be set with a follower option') return await self._subconfig.getattr(self._name, - self._option_bag) + self._option_bag, + ) @option_type('option') async def set(self, value): @@ -891,11 +899,14 @@ class TiramisuContextInformation(TiramisuConfig): ): """Get an information""" values = self._config_bag.context.cfgimpl_get_values() - return await values.get_information(self._config_bag, - None, - name, - default, - ) + try: + return await values.get_information(self._config_bag, + None, + name, + undefined, + ) + except ValueError: + return self._config_bag.context.cfgimpl_get_description().impl_get_information(name, default) @connection async def set(self, @@ -920,7 +931,9 @@ class TiramisuContextInformation(TiramisuConfig): @connection async def list(self): """List information's keys""" - return await self._config_bag.context.impl_list_information(self._config_bag.connection) + lst1 = set(self._config_bag.context.cfgimpl_get_description().impl_list_information()) + lst2 = set(await self._config_bag.context.impl_list_information(self._config_bag.connection)) + return lst1 | lst2 @connection async def exportation(self): diff --git a/tiramisu/error.py b/tiramisu/error.py index c79a26a..106aeea 100644 --- a/tiramisu/error.py +++ b/tiramisu/error.py @@ -193,7 +193,6 @@ class ValueWarning(_CommonError, UserWarning): def __init__(self, *args, **kwargs): if len(args) == 1 and not kwargs: self.msg = args[0] - pass else: super().__init__(*args, **kwargs) self.msg = None diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index c682634..496ec3c 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -203,7 +203,8 @@ class Base: # information def impl_get_information(self, key: str, - default: Any=undefined) -> Any: + default: Any=undefined, + ) -> Any: """retrieves one information's item :param key: the item string (ex: "help") @@ -225,7 +226,8 @@ class Base: def impl_set_information(self, key: str, - value: Any) -> None: + value: Any, + ) -> None: """updates the information's attribute (which is a dictionary) @@ -239,6 +241,15 @@ class Base: key)) self._informations[key] = value + def impl_list_information(self) -> Any: + dico = self._informations + if isinstance(dico, tuple): + return list(dico[0]) + elif isinstance(dico, str): + return ['doc'] + # it's a dict + return list(dico.keys()) + class BaseOption(Base): """This abstract base class stands for attribute access diff --git a/tiramisu/storage/cacheobj.py b/tiramisu/storage/cacheobj.py index 516b675..20d8c28 100644 --- a/tiramisu/storage/cacheobj.py +++ b/tiramisu/storage/cacheobj.py @@ -57,7 +57,7 @@ class Cache(DictCache): # so value is self_props self_props = value # recheck "cache" value - if 'cache' in props or 'cache' in props: + if 'cache' in props: if expiration_time and timestamp and \ ('expire' in props or \ 'expire' in self_props): @@ -69,8 +69,6 @@ class Cache(DictCache): # else: # log.debug('getcache expired value for path %s < %s', # timestamp + expiration_time, ntime) - # if expired, remove from cache - # self.delcache(path) else: # log.debug('getcache in cache (2) %s %s %s %s %s', path, value, _display_classname(self), # id(self), index) diff --git a/tiramisu/storage/dictionary/value.py b/tiramisu/storage/dictionary/value.py index b3a7f80..bdb61e1 100644 --- a/tiramisu/storage/dictionary/value.py +++ b/tiramisu/storage/dictionary/value.py @@ -77,7 +77,6 @@ class Values: a specified value must be associated to an owner """ log.debug('setvalue %s %s %s %s %s', path, value, owner, index, id(self)) - #if isinstance(value, list): # value = value values = self._storage.get_values() @@ -281,7 +280,7 @@ class Values: path): informations = self._storage.get_informations() if path in informations: - return informations[path].keys() + return list(informations[path].keys()) else: return []