api: unify walk option

This commit is contained in:
Emmanuel Garette 2022-10-01 19:44:48 +02:00
parent 9731769694
commit 026e665ab0

View file

@ -145,6 +145,83 @@ class CommonTiramisuOption(CommonTiramisu):
raise APIError(_('unknown method "{}" in "{}"').format(name, self.__class__.__name__))
class _TiramisuOptionWalk:
async def _filter(self,
opt,
subconfig,
config_bag,
):
option_bag = OptionBag()
option_bag.set_option(opt,
None,
config_bag)
settings = config_bag.context.cfgimpl_get_settings()
option_bag.properties = await settings.getproperties(option_bag)
if opt.impl_is_optiondescription():
await settings.validate_properties(option_bag)
return await subconfig.get_subconfig(option_bag)
await subconfig.getattr(opt.impl_getname(),
option_bag)
async def _walk(self,
option,
recursive,
type_,
group_type,
config_bag,
subconfig,
):
options = []
for opt in await option.get_children(config_bag):
try:
subsubconfig = await self._filter(opt,
subconfig,
config_bag)
except PropertiesOptionError:
continue
if opt.impl_is_optiondescription():
if recursive:
options.extend(await self._walk(opt,
recursive,
type_,
group_type,
config_bag,
subsubconfig))
if type_ == 'option' or (type_ == 'optiondescription' and \
group_type and opt.impl_get_group_type() != group_type):
continue
elif type_ == 'optiondescription':
continue
options.append(TiramisuOption(opt.impl_getpath(),
None,
config_bag,
))
return options
async def _list(self,
type,
group_type,
recursive,
root_option,
config_bag,
):
assert type in ('all', 'option', 'optiondescription'), _('unknown list type {}').format(type)
assert group_type is None or isinstance(group_type, groups.GroupType), \
_("unknown group_type: {0}").format(group_type)
if config_bag.properties and 'warnings' in config_bag.properties:
config_bag = config_bag.copy()
config_bag.remove_warnings()
options = []
for opt in await self._walk(root_option,
recursive,
type,
group_type,
config_bag,
config_bag.context):
options.append(opt)
return options
def option_and_connection(func):
async def wrapped(self, *args, **kwargs):
config_bag = self._option_bag.config_bag
@ -157,7 +234,7 @@ def option_and_connection(func):
return wrapped
class _TiramisuOptionOptionDescription(CommonTiramisuOption):
class _TiramisuOptionOptionDescription(CommonTiramisuOption, _TiramisuOptionWalk):
"""Manage option"""
_allow_optiondescription = True
_follower_need_index = False
@ -341,6 +418,19 @@ class TiramisuOptionOption(_TiramisuOptionOptionDescription):
#FIXME only from 0.0.0.0 to 255.255.255.255
return r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
@option_and_connection
async def list(self,
type='option',
group_type=None,
recursive=False,
):
return await self._list(type,
group_type,
recursive,
self._option_bag.option,
self._option_bag.config_bag,
)
class TiramisuOptionOwner(CommonTiramisuOption):
#FIXME optiondescription must not have Owner!
@ -808,7 +898,8 @@ class TiramisuOption(CommonTiramisu, TiramisuConfig):
async def _filter(self,
opt,
subconfig,
config_bag):
config_bag,
):
settings = config_bag.context.cfgimpl_get_settings()
option_bag = OptionBag()
option_bag.set_option(opt,
@ -844,7 +935,8 @@ class TiramisuOption(CommonTiramisu, TiramisuConfig):
try:
await self._filter(opt,
subconfig,
config_bag)
config_bag,
)
except PropertiesOptionError:
continue
if opt.impl_is_optiondescription():
@ -1299,7 +1391,7 @@ class TiramisuContextPermissive(TiramisuConfig):
await self._set(frozenset(props))
class TiramisuContextOption(TiramisuConfig):
class TiramisuContextOption(TiramisuConfig, _TiramisuOptionWalk):
def __init__(self,
*args,
**kwargs) -> None:
@ -1327,78 +1419,19 @@ class TiramisuContextOption(TiramisuConfig):
options.append(option)
return options
async def _filter(self,
opt,
subconfig,
config_bag):
option_bag = OptionBag()
option_bag.set_option(opt,
None,
config_bag)
settings = config_bag.context.cfgimpl_get_settings()
option_bag.properties = await settings.getproperties(option_bag)
if opt.impl_is_optiondescription():
await settings.validate_properties(option_bag)
return await subconfig.get_subconfig(option_bag)
await subconfig.getattr(opt.impl_getname(),
option_bag)
async def _walk(self,
option,
recursive,
type_,
group_type,
config_bag,
subconfig):
options = []
for opt in await option.get_children(config_bag):
try:
subsubconfig = await self._filter(opt,
subconfig,
config_bag)
except PropertiesOptionError:
continue
if opt.impl_is_optiondescription():
if recursive:
options.extend(await self._walk(opt,
recursive,
type_,
group_type,
config_bag,
subsubconfig))
if type_ == 'option' or (type_ == 'optiondescription' and \
group_type and opt.impl_get_group_type() != group_type):
continue
elif type_ == 'optiondescription':
continue
options.append(TiramisuOption(opt.impl_getpath(),
None,
self._config_bag))
return options
@connection
async def list(self,
type='option',
group_type=None,
recursive=False):
recursive=False,
):
"""List options (by default list only option)"""
assert type in ('all', 'option', 'optiondescription'), _('unknown list type {}').format(type)
assert group_type is None or isinstance(group_type, groups.GroupType), \
_("unknown group_type: {0}").format(group_type)
config_bag = self._config_bag
if config_bag.properties and 'warnings' in config_bag.properties:
config_bag = config_bag.copy()
config_bag.remove_warnings()
option = config_bag.context.cfgimpl_get_description()
options = []
for opt in await self._walk(option,
recursive,
type,
group_type,
config_bag,
config_bag.context):
options.append(opt)
return options
return await self._list(type,
group_type,
recursive,
self._config_bag.context.cfgimpl_get_description(),
self._config_bag,
)
async def _load_dict(self,
clearable="all",
@ -1750,7 +1783,8 @@ class Config(TiramisuAPI):
session_id: str=None,
delete_old_session: bool=False,
storage=None,
display_name=None) -> None:
display_name=None,
) -> None:
if storage is None:
storage = default_storage
storage_obj = await storage.get()
@ -1763,14 +1797,17 @@ class Config(TiramisuAPI):
session_id=session_id,
delete_old_session=delete_old_session,
storage=storage,
display_name=display_name)
display_name=display_name,
)
settings = config.cfgimpl_get_settings()
properties = await settings.get_context_properties(connection,
config._impl_properties_cache)
config._impl_properties_cache,
)
permissives = await settings.get_context_permissives(connection)
config_bag = ConfigBag(config,
properties=properties,
permissives=permissives)
permissives=permissives,
)
super().__init__(config_bag)
async def __aenter__(self):