add support in calculation when option is in a dynoptiondescription

This commit is contained in:
Emmanuel Garette 2020-04-11 13:13:35 +02:00
parent d71018a88e
commit f7bd6e3a47
10 changed files with 252 additions and 65 deletions

View File

@ -9,7 +9,9 @@ from tiramisu import BoolOption, StrOption, ChoiceOption, IPOption, \
StrOption, PortOption, BroadcastOption, DomainnameOption, \ StrOption, PortOption, BroadcastOption, DomainnameOption, \
EmailOption, URLOption, UsernameOption, FilenameOption, SymLinkOption, \ EmailOption, URLOption, UsernameOption, FilenameOption, SymLinkOption, \
OptionDescription, DynOptionDescription, SynDynOption, submulti, Leadership, \ OptionDescription, DynOptionDescription, SynDynOption, submulti, Leadership, \
Config, Params, ParamOption, ParamValue, ParamSuffix, ParamSelfOption, ParamIndex, Calculation, calc_value, \ Config, \
Params, ParamOption, ParamValue, ParamSuffix, ParamSelfOption, ParamDynOption, ParamIndex, \
Calculation, calc_value, \
delete_session delete_session
from tiramisu.error import PropertiesOptionError, ConfigError, ConflictError from tiramisu.error import PropertiesOptionError, ConfigError, ConflictError
from tiramisu.storage import list_sessions from tiramisu.storage import list_sessions
@ -145,8 +147,8 @@ async def test_getdoc_dyndescription():
assert await cfg.option('od.dodval2.stval2').option.name() == 'stval2' assert await cfg.option('od.dodval2.stval2').option.name() == 'stval2'
assert await cfg.option('od.dodval1').option.name() == 'dodval1' assert await cfg.option('od.dodval1').option.name() == 'dodval1'
assert await cfg.option('od.dodval2').option.name() == 'dodval2' assert await cfg.option('od.dodval2').option.name() == 'dodval2'
assert await cfg.option('od.dodval1.stval1').option.doc() == 'doc1' assert await cfg.option('od.dodval1.stval1').option.doc() == 'doc1val1'
assert await cfg.option('od.dodval2.stval2').option.doc() == 'doc1' assert await cfg.option('od.dodval2.stval2').option.doc() == 'doc1val2'
assert await cfg.option('od.dodval1').option.doc() == 'doc2val1' assert await cfg.option('od.dodval1').option.doc() == 'doc2val1'
assert await cfg.option('od.dodval2').option.doc() == 'doc2val2' assert await cfg.option('od.dodval2').option.doc() == 'doc2val2'
assert not await list_sessions() assert not await list_sessions()
@ -289,6 +291,56 @@ async def test_callback_dyndescription():
assert not await list_sessions() assert not await list_sessions()
@pytest.mark.asyncio
async def test_callback_dyndescription_outside_wrong_param():
lst = StrOption('lst', '', ['val1', 'val2'], multi=True)
st = StrOption('st', '', Calculation(return_dynval))
dod = DynOptionDescription('dod', '', [st], suffixes=Calculation(return_list, Params(ParamOption(lst))))
out = StrOption('out', '', Calculation(return_dynval, Params(ParamOption(st))))
od = OptionDescription('od', '', [dod, out])
od2 = OptionDescription('od', '', [od, lst])
async with await Config(od2) as cfg:
with pytest.raises(ConfigError):
await cfg.value.dict()
assert not await list_sessions()
@pytest.mark.asyncio
async def test_callback_dyndescription_outside1():
lst = StrOption('lst', '', ['val1', 'val2'], multi=True)
st = StrOption('st', '', Calculation(return_dynval))
dod = DynOptionDescription('dod', '', [st], suffixes=Calculation(return_list, Params(ParamOption(lst))))
out = StrOption('out', '', Calculation(return_dynval, Params(ParamDynOption(st, 'val1', dod))))
od = OptionDescription('od', '', [dod, out])
od2 = OptionDescription('od', '', [od, lst])
async with await Config(od2) as cfg:
assert await cfg.value.dict() == {'od.dodval1.stval1': 'val', 'od.dodval2.stval2': 'val', 'od.out': 'val', 'lst': ['val1', 'val2']}
await cfg.option('od.dodval1.stval1').value.set('val1')
await cfg.option('od.dodval2.stval2').value.set('val2')
assert await cfg.value.dict() == {'od.dodval1.stval1': 'val1', 'od.dodval2.stval2': 'val2', 'od.out': 'val1', 'lst': ['val1', 'val2']}
await cfg.option('lst').value.set(['val2'])
with pytest.raises(ConfigError):
await cfg.value.dict()
await cfg.option('lst').value.set(['val1'])
assert await cfg.value.dict() == {'od.dodval1.stval1': 'val1', 'od.out': 'val1', 'lst': ['val1']}
assert not await list_sessions()
@pytest.mark.asyncio
async def test_callback_dyndescription_outside2():
lst = StrOption('lst', '', ['val1', 'val2'], multi=True)
out = StrOption('out', '')
st = StrOption('st', '', Calculation(return_dynval, Params(ParamOption(out))))
dod = DynOptionDescription('dod', '', [st], suffixes=Calculation(return_list, Params(ParamOption(lst))))
od = OptionDescription('od', '', [dod, out])
od2 = OptionDescription('od', '', [od, lst])
async with await Config(od2) as cfg:
assert await cfg.value.dict() == {'od.dodval1.stval1': None, 'od.dodval2.stval2': None, 'od.out': None, 'lst': ['val1', 'val2']}
await cfg.option('od.out').value.set('val1')
assert await cfg.value.dict() == {'od.dodval1.stval1': 'val1', 'od.dodval2.stval2': 'val1', 'od.out': 'val1', 'lst': ['val1', 'val2']}
assert not await list_sessions()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_callback_list_dyndescription(): async def test_callback_list_dyndescription():
st = StrOption('st', '', Calculation(return_list2, Params(ParamSuffix())), multi=True, properties=('notunique',)) st = StrOption('st', '', Calculation(return_list2, Params(ParamSuffix())), multi=True, properties=('notunique',))

View File

@ -358,6 +358,27 @@ async def test_meta_new_config_wrong_name():
await delete_sessions(meta) await delete_sessions(meta)
@pytest.mark.asyncio
async def test_meta_load_config():
od = make_description()
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
assert len(list(await meta.config.list())) == 2
await meta.config.load('name1')
assert len(list(await meta.config.list())) == 3
await delete_sessions(meta)
@pytest.mark.asyncio
async def test_meta_load_config_wrong_name():
od = make_description()
meta = await MetaConfig(['name1', 'name2'], optiondescription=od)
assert len(list(await meta.config.list())) == 2
with pytest.raises(ConfigError):
await meta.config.load('name3')
assert len(list(await meta.config.list())) == 2
await delete_sessions(meta)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_meta_meta_set(): async def test_meta_meta_set():
meta = await make_metaconfig(double=True) meta = await make_metaconfig(double=True)

View File

@ -17,8 +17,8 @@
from .function import calc_value, calc_value_property_help, valid_ip_netmask, \ from .function import calc_value, calc_value_property_help, valid_ip_netmask, \
valid_network_netmask, valid_in_network, valid_broadcast, \ valid_network_netmask, valid_in_network, valid_broadcast, \
valid_not_equal valid_not_equal
from .autolib import Calculation, Params, ParamOption, ParamSelfOption, ParamValue, \ from .autolib import Calculation, Params, ParamOption, ParamDynOption, ParamSelfOption, \
ParamIndex, ParamSuffix ParamValue, ParamIndex, ParamSuffix
from .option import * from .option import *
from .error import APIError from .error import APIError
from .api import Config, MetaConfig, GroupConfig, MixConfig from .api import Config, MetaConfig, GroupConfig, MixConfig
@ -31,6 +31,7 @@ from .storage import default_storage, Storage, list_sessions, \
allfuncs = ['Calculation', allfuncs = ['Calculation',
'Params', 'Params',
'ParamOption', 'ParamOption',
'ParamDynOption',
'ParamSelfOption', 'ParamSelfOption',
'ParamValue', 'ParamValue',
'ParamIndex', 'ParamIndex',

View File

@ -1533,8 +1533,7 @@ class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextCon
async def new(self, async def new(self,
session_id, session_id,
storage=None, storage=None,
type='config', type='config'):
new=None):
"""Create and add a new config""" """Create and add a new config"""
config = self._config_bag.context config = self._config_bag.context
if storage is None: if storage is None:
@ -1545,7 +1544,26 @@ class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextCon
session_id=session_id, session_id=session_id,
storage=storage, storage=storage,
type_=type, type_=type,
new=new) )
return await self._return_config(new_config,
storage)
async def load(self,
session_id,
storage=None,
type='config',
):
"""Create and add a new config"""
config = self._config_bag.context
if storage is None:
storage = config._storage
storage_obj = await storage.get()
async with storage_obj.Connection() as connection:
new_config = await config.load_config(connection,
session_id=session_id,
storage=storage,
type_=type,
)
return await self._return_config(new_config, return await self._return_config(new_config,
storage) storage)

View File

@ -81,6 +81,26 @@ class ParamOption(Param):
self.raisepropertyerror = raisepropertyerror self.raisepropertyerror = raisepropertyerror
class ParamDynOption(ParamOption):
__slots__ = ('suffix',
)
def __init__(self,
option: 'Option',
suffix: str,
dynoptiondescription: 'DynOptionDescription',
notraisepropertyerror: bool=False,
raisepropertyerror: bool=False,
todict: bool=False,
) -> None:
super().__init__(option,
notraisepropertyerror,
raisepropertyerror,
todict,
)
self.suffix = suffix
self.dynoptiondescription = dynoptiondescription
class ParamSelfOption(Param): class ParamSelfOption(Param):
__slots__ = ('todict', 'whole') __slots__ = ('todict', 'whole')
def __init__(self, def __init__(self,
@ -214,7 +234,10 @@ async def manager_callback(callbk: Union[ParamOption, ParamValue],
value = value[apply_index] value = value[apply_index]
return value return value
async def get_value(callbk, option_bag, path): async def get_value(callbk,
option_bag,
path,
):
try: try:
# get value # get value
value = await config_bag.context.getattr(path, value = await config_bag.context.getattr(path,
@ -227,6 +250,10 @@ async def manager_callback(callbk: Union[ParamOption, ParamValue],
', {}').format(option.impl_get_display_name(), err), err) ', {}').format(option.impl_get_display_name(), err), err)
except ValueError as err: except ValueError as err:
raise ValueError(_('the option "{0}" is used in a calculation but is invalid ({1})').format(option_bag.option.impl_get_display_name(), err)) raise ValueError(_('the option "{0}" is used in a calculation but is invalid ({1})').format(option_bag.option.impl_get_display_name(), err))
except AttributeError as err:
raise ConfigError(_('impossible to calculate "{0}", {1}').format(option_bag.option.impl_get_display_name(),
err,
))
return value return value
async def get_option_bag(config_bag, async def get_option_bag(config_bag,
@ -272,41 +299,59 @@ async def manager_callback(callbk: Union[ParamOption, ParamValue],
return {'name': option.impl_get_display_name(), return {'name': option.impl_get_display_name(),
'value': value} 'value': value}
# it's ParamOption if isinstance(callbk, ParamOption):
callbk_option = callbk.option callbk_option = callbk.option
if callbk_option.issubdyn(): if callbk_option.issubdyn():
callbk_option = callbk_option.to_dynoption(option.rootpath, if isinstance(callbk, ParamDynOption):
option.impl_getsuffix(), subdyn = callbk.dynoptiondescription
callbk_option.getsubdyn()) rootpath = subdyn.impl_getpath() + callbk.suffix
if leadership_must_have_index and callbk_option.impl_get_leadership() and index is None: suffix = callbk.suffix
raise Break() else:
if config_bag is undefined: if not option.impl_is_dynsymlinkoption():
return undefined msg = 'option "{}" is not dynamic in callback of the option "{}"'
if index is not None and callbk_option.impl_get_leadership() and \ raise ConfigError(_(msg).format(callbk_option.impl_get_display_name(),
callbk_option.impl_get_leadership().in_same_group(option): option.impl_get_display_name(),
if not callbk_option.impl_is_follower(): ))
# leader rootpath = option.rootpath
index_ = None suffix = option.impl_getsuffix()
with_index = True subdyn = callbk_option.getsubdyn()
callbk_option = callbk_option.to_dynoption(rootpath,
suffix,
subdyn)
if leadership_must_have_index and callbk_option.impl_get_leadership() and index is None:
raise Break()
if config_bag is undefined:
return undefined
if index is not None and callbk_option.impl_get_leadership() and \
callbk_option.impl_get_leadership().in_same_group(option):
if not callbk_option.impl_is_follower():
# leader
index_ = None
with_index = True
else:
# follower
index_ = index
with_index = False
else: else:
# follower index_ = None
index_ = index
with_index = False with_index = False
else: path = callbk_option.impl_getpath()
index_ = None option_bag = await get_option_bag(config_bag,
with_index = False callbk_option,
path = callbk_option.impl_getpath() index_,
option_bag = await get_option_bag(config_bag, False)
callbk_option, value = await get_value(callbk,
index_, option_bag,
False) path,
value = await get_value(callbk, option_bag, path) )
if with_index: if with_index:
value = value[index] value = value[index]
if not callbk.todict: if not callbk.todict:
return value return value
return {'name': callbk_option.impl_get_display_name(), return {'name': callbk_option.impl_get_display_name(),
'value': value} 'value': value}
raise ConfigError(_('unknown callback type {} in option {}').format(callbk,
option.impl_get_display_name()))
async def carry_out_calculation(option, async def carry_out_calculation(option,

View File

@ -124,6 +124,18 @@ class SubConfig:
await self.reset_one_option_cache(desc, await self.reset_one_option_cache(desc,
resetted_opts, resetted_opts,
doption_bag) doption_bag)
async for coption in self.cfgimpl_get_description().get_children_recursively(None,
None,
option_bag.config_bag):
coption_bag = OptionBag()
coption_bag.set_option(coption,
option_bag.index,
option_bag.config_bag)
coption_bag.properties = await self.cfgimpl_get_settings().getproperties(coption_bag)
await self.reset_one_option_cache(option,
resetted_opts,
coption_bag,
)
elif option.issubdyn(): elif option.issubdyn():
# it's an option in dynoptiondescription, remove cache for all generated option # it's an option in dynoptiondescription, remove cache for all generated option
dynopt = option.getsubdyn() dynopt = option.getsubdyn()
@ -1176,13 +1188,28 @@ class KernelMixConfig(KernelGroupConfig):
session_id, session_id,
type_='config', type_='config',
storage=None, storage=None,
new=None,
): ):
if new is None: if session_id in [child.impl_getname() for child in self._impl_children]:
new = session_id not in await list_sessions()
if new and session_id in [child.impl_getname() for child in self._impl_children]:
raise ConflictError(_('config name must be uniq in ' raise ConflictError(_('config name must be uniq in '
'groupconfig for {0}').format(session_id)) 'groupconfig for {0}').format(session_id))
return await self.load_config(connection,
session_id,
type_,
storage,
new=True,
)
async def load_config(self,
connection,
session_id,
type_='config',
storage=None,
new=False,
):
if not new:
if session_id not in [child.impl_getname() for child in self._impl_children]:
raise ConfigError(_('cannot find existing config with session_id to "{}"').format(session_id))
assert type_ in ('config', 'metaconfig', 'mixconfig'), _('unknown type {}').format(type_) assert type_ in ('config', 'metaconfig', 'mixconfig'), _('unknown type {}').format(type_)
if type_ == 'config': if type_ == 'config':
config = await KernelConfig(self._impl_descr, config = await KernelConfig(self._impl_descr,

View File

@ -270,20 +270,36 @@ class BaseOption(Base):
return self.impl_get_callback()[0] is not None return self.impl_get_callback()[0] is not None
def _impl_get_display_name(self, def _impl_get_display_name(self,
dyn_name: Base=None) -> str: dyn_name: Base=None,
suffix: str=None,
) -> str:
name = self.impl_get_information('doc', None) name = self.impl_get_information('doc', None)
if name is None or name == '': if name is None or name == '':
if dyn_name is not None: if dyn_name is not None:
name = dyn_name name = dyn_name
else: else:
name = self.impl_getname() name = self.impl_getname()
elif suffix:
name += suffix
return name return name
def impl_get_display_name(self, def _get_display_name(self,
dyn_name: Base=None) -> str: dyn_name,
suffix,
):
if hasattr(self, '_display_name_function'): if hasattr(self, '_display_name_function'):
return self._display_name_function(self, dyn_name) return self._display_name_function(self,
return self._impl_get_display_name(dyn_name) dyn_name,
suffix,
)
return self._impl_get_display_name(dyn_name,
suffix,
)
def impl_get_display_name(self) -> str:
return self._get_display_name(None,
None,
)
def reset_cache(self, def reset_cache(self,
path: str, path: str,

View File

@ -20,6 +20,8 @@
# ____________________________________________________________ # ____________________________________________________________
import re import re
from typing import List, Callable from typing import List, Callable
from itertools import chain
from ..autolib import ParamOption
from ..i18n import _ from ..i18n import _
@ -60,8 +62,12 @@ class DynOptionDescription(OptionDescription):
'dynoptiondescription')) 'dynoptiondescription'))
child._setsubdyn(self) child._setsubdyn(self)
# add suffixes # add suffixes
if __debug__ and isinstance(suffixes, Calculation): if __debug__ and not isinstance(suffixes, Calculation):
self._suffixes = suffixes raise ConfigError(_('suffixes in dynoptiondescription has to be a calculation'))
for param in chain(suffixes.params.args, suffixes.params.kwargs.values()):
if isinstance(param, ParamOption):
param.option._add_dependency(self)
self._suffixes = suffixes
def convert_suffix_to_path(self, def convert_suffix_to_path(self,
suffix): suffix):

View File

@ -93,17 +93,16 @@ class Option(BaseOption):
doc, doc,
properties=properties, properties=properties,
is_multi=is_multi) is_multi=is_multi)
if __debug__: if validators is not None:
if validators is not None: if __debug__ and not isinstance(validators, list):
if not isinstance(validators, list): raise ValueError(_('validators must be a list of Calculation for "{}"').format(name))
raise ValueError(_('validators must be a list of Calculation for "{}"').format(name)) for validator in validators:
for validator in validators: if __debug__ and not isinstance(validator, Calculation):
if not isinstance(validator, Calculation): raise ValueError(_('validators must be a Calculation for "{}"').format(name))
raise ValueError(_('validators must be a Calculation for "{}"').format(name)) for param in chain(validator.params.args, validator.params.kwargs.values()):
for param in chain(validator.params.args, validator.params.kwargs.values()): if isinstance(param, ParamOption):
if isinstance(param, ParamOption): param.option._add_dependency(self)
param.option._add_dependency(self) self._has_dependency = True
self._has_dependency = True
self._validators = tuple(validators) self._validators = tuple(validators)
if extra is not None and extra != {}: if extra is not None and extra != {}:

View File

@ -59,7 +59,9 @@ class SynDynOption:
return self.opt.impl_getname() + self.suffix return self.opt.impl_getname() + self.suffix
def impl_get_display_name(self) -> str: def impl_get_display_name(self) -> str:
return self.opt.impl_get_display_name(dyn_name=self.impl_getname()) + self.suffix return self.opt._get_display_name(dyn_name=self.impl_getname(),
suffix=self.suffix,
)
def impl_getsuffix(self) -> str: def impl_getsuffix(self) -> str:
return self.suffix return self.suffix