option's informations avalaible in config

This commit is contained in:
Emmanuel Garette 2022-01-09 19:10:09 +01:00
parent 49ba56b6ec
commit 8658fdd6ca
6 changed files with 49 additions and 29 deletions

View file

@ -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')

View file

@ -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):

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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 []