Merge branch 'develop' into dist/risotto/risotto-2.8.0/develop

This commit is contained in:
Emmanuel Garette 2020-08-12 12:01:20 +02:00
commit 2db305b1f0
19 changed files with 388 additions and 156 deletions

View File

@ -1,3 +1,8 @@
Mon Apr 1 11:47:30 2019 +0200 Emmanuel Garette <egarette@cadoles.com>
* version 3.0 rc16
* tiramisu is now async
* add postgresql storage
Mon Sep 2 14:10:40 2019 +0200 Emmanuel Garette <egarette@cadoles.com> Mon Sep 2 14:10:40 2019 +0200 Emmanuel Garette <egarette@cadoles.com>
* version 3.0 rc15 * version 3.0 rc15
* add parents method to MetaConfig * add parents method to MetaConfig

View File

@ -1,11 +1,17 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from setuptools import setup, find_packages from setuptools import setup
import os import os
from tiramisu import __version__ from tiramisu import __version__
PACKAGE_NAME = os.environ.get('PACKAGE_DST', 'tiramisu') ORI_PACKAGE_NAME = 'tiramisu'
PACKAGE_NAME = os.environ.get('PACKAGE_DST', ORI_PACKAGE_NAME)
if PACKAGE_NAME != ORI_PACKAGE_NAME:
package_dir = {PACKAGE_NAME: ORI_PACKAGE_NAME}
else:
package_dir = None
setup( setup(
version=__version__, version=__version__,
@ -40,5 +46,6 @@ producing flexible and fast options access.
This version requires Python 3.5 or later. This version requires Python 3.5 or later.
""", """,
include_package_data=True, include_package_data=True,
packages=find_packages(include=['tiramisu']) package_dir=package_dir,
packages=[PACKAGE_NAME],
) )

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

@ -187,26 +187,6 @@ async def test_force_store_value():
assert not await list_sessions() assert not await list_sessions()
@pytest.mark.asyncio
async def test_force_store_value_leadership_follower():
b = IntOption('int', 'Test int option', multi=True)
c = StrOption('str', 'Test string option', multi=True, properties=('force_store_value',))
descr = Leadership("int", "", [b, c])
with pytest.raises(ConfigError):
cfg = await Config(descr, session_id='error')
await delete_session('error')
assert not await list_sessions()
#@pytest.mark.asyncio
#async def test_force_store_value_leadership():
# b = IntOption('int', 'Test int option', multi=True, properties=('force_store_value',))
# c = StrOption('str', 'Test string option', multi=True)
# descr = Leadership("int", "", [b, c])
# cfg = await Config(descr)
# assert await cfg.value.get() == {'int': ('forced', ())}
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_force_store_value_leadership_sub(): async def test_force_store_value_leadership_sub():
b = IntOption('int', 'Test int option', multi=True, properties=('force_store_value',)) b = IntOption('int', 'Test int option', multi=True, properties=('force_store_value',))

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

@ -1433,11 +1433,14 @@ async def test_calc_value_remove_duplicate(config_type):
async def test_calc_value_join(config_type): async def test_calc_value_join(config_type):
val1 = StrOption('val1', "", 'val1') val1 = StrOption('val1', "", 'val1')
val2 = StrOption('val2', "", 'val2') val2 = StrOption('val2', "", 'val2')
val3 = StrOption('val3', "", Calculation(calc_value, Params((ParamOption(val1), ParamOption(val2)), join=ParamValue('.')))) val3 = StrOption('val3', "")
od = OptionDescription('root', '', [val1, val2, val3]) val4 = StrOption('val4', "", Calculation(calc_value, Params((ParamOption(val1), ParamOption(val2), ParamOption(val3)), join=ParamValue('.'))))
od = OptionDescription('root', '', [val1, val2, val3, val4])
async with await Config(od) as cfg: async with await Config(od) as cfg:
cfg = await get_config(cfg, config_type) cfg = await get_config(cfg, config_type)
assert await cfg.value.dict() == {'val1': 'val1', 'val2': 'val2', 'val3': 'val1.val2'} assert await cfg.value.dict() == {'val1': 'val1', 'val2': 'val2', 'val3': None, 'val4': None}
await cfg.option('val3').value.set('val3')
assert await cfg.value.dict() == {'val1': 'val1', 'val2': 'val2', 'val3': 'val3', 'val4': 'val1.val2.val3'}
assert not await list_sessions() assert not await list_sessions()

View File

@ -5,7 +5,6 @@ import pytest
import warnings import warnings
from tiramisu.api import TIRAMISU_VERSION
from tiramisu.setting import groups, owners from tiramisu.setting import groups, owners
from tiramisu import StrOption, IntOption, OptionDescription, submulti, Leadership, Config, \ from tiramisu import StrOption, IntOption, OptionDescription, submulti, Leadership, Config, \
MetaConfig, undefined, Params, ParamOption, Calculation MetaConfig, undefined, Params, ParamOption, Calculation
@ -38,11 +37,7 @@ async def test_unknown_multi():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_submulti(): async def test_submulti():
multi = StrOption('multi', '', multi=submulti) multi = StrOption('multi', '', multi=submulti)
if TIRAMISU_VERSION == 2: multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
default_multi = 'yes'
else:
default_multi = ['yes']
multi2 = StrOption('multi2', '', default_multi=default_multi, multi=submulti)
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti) multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
od = OptionDescription('od', '', [multi, multi2, multi3]) od = OptionDescription('od', '', [multi, multi2, multi3])
async with await Config(od) as cfg: async with await Config(od) as cfg:
@ -66,11 +61,7 @@ async def test_submulti_default_multi_not_list():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_append_submulti(): async def test_append_submulti():
multi = StrOption('multi', '', multi=submulti) multi = StrOption('multi', '', multi=submulti)
if TIRAMISU_VERSION == 2: multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
default_multi = 'yes'
else:
default_multi = ['yes']
multi2 = StrOption('multi2', '', default_multi=default_multi, multi=submulti)
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti) multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
od = OptionDescription('od', '', [multi, multi2, multi3]) od = OptionDescription('od', '', [multi, multi2, multi3])
async with await Config(od) as cfg: async with await Config(od) as cfg:
@ -104,11 +95,7 @@ async def test_append_submulti():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_append_unvalide_submulti(): async def test_append_unvalide_submulti():
multi = StrOption('multi', '', multi=submulti) multi = StrOption('multi', '', multi=submulti)
if TIRAMISU_VERSION == 2: multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
default_multi = 'yes'
else:
default_multi = ['yes']
multi2 = StrOption('multi2', '', default_multi=default_multi, multi=submulti)
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti) multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
od = OptionDescription('od', '', [multi, multi2, multi3]) od = OptionDescription('od', '', [multi, multi2, multi3])
async with await Config(od) as cfg: async with await Config(od) as cfg:
@ -137,11 +124,7 @@ async def test_append_unvalide_submulti():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_pop_submulti(): async def test_pop_submulti():
multi = StrOption('multi', '', multi=submulti) multi = StrOption('multi', '', multi=submulti)
if TIRAMISU_VERSION == 2: multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
default_multi = 'yes'
else:
default_multi = ['yes']
multi2 = StrOption('multi2', '', default_multi=default_multi, multi=submulti)
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti) multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
od = OptionDescription('od', '', [multi, multi2, multi3]) od = OptionDescription('od', '', [multi, multi2, multi3])
async with await Config(od) as cfg: async with await Config(od) as cfg:
@ -267,6 +250,27 @@ async def test_values_with_leader_and_followers_submulti():
assert not await list_sessions() assert not await list_sessions()
@pytest.mark.asyncio
async def test_values_with_leader_and_followers_submulti_default_multi():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "masque du sous-réseau", multi=submulti, default_multi=['255.255.0.0', '0.0.0.0'])
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
maconfig = OptionDescription('toto', '', [interface1])
async with await Config(maconfig) as cfg:
await cfg.property.read_write()
owner = await cfg.owner.get()
assert interface1.impl_get_group_type() == groups.leadership
assert await cfg.option('ip_admin_eth0.ip_admin_eth0').owner.get() == owners.default
await cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(["192.168.230.145"])
assert await cfg.option('ip_admin_eth0.ip_admin_eth0').value.get() == ["192.168.230.145"]
assert await cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == ['255.255.0.0', '0.0.0.0']
await cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(["192.168.230.145", "192.168.230.147"])
await cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0'])
assert await cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == ['255.255.255.0']
assert await cfg.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == ['255.255.0.0', '0.0.0.0']
assert not await list_sessions()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_reset_values_with_leader_and_followers_submulti(): async def test_reset_values_with_leader_and_followers_submulti():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True) ip_admin_eth0 = StrOption('ip_admin_eth0', "ip réseau autorisé", multi=True)
@ -429,6 +433,20 @@ async def test_callback_submulti():
assert not await list_sessions() assert not await list_sessions()
@pytest.mark.asyncio
async def test_callback_submulti_follower():
multi = StrOption('multi', '', multi=True)
multi2 = StrOption('multi2', '', Calculation(return_list), multi=submulti)
od = Leadership('multi', '', [multi, multi2])
od = OptionDescription('multi', '', [od])
async with await Config(od) as cfg:
await cfg.property.read_write()
assert await cfg.option('multi.multi').value.get() == []
await cfg.option('multi.multi').value.set(['val'])
assert await cfg.option('multi.multi2', 0).value.get() == ['val', 'val']
assert not await list_sessions()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_submulti_unique(): async def test_submulti_unique():
i = IntOption('int', '', multi=submulti, properties=('unique',)) i = IntOption('int', '', multi=submulti, properties=('unique',))

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

@ -52,7 +52,7 @@ class TiramisuHelp:
if module_name in ['forcepermissive', 'unrestraint']: if module_name in ['forcepermissive', 'unrestraint']:
force = True force = True
max_len = max(max_len, len('forcepermissive')) max_len = max(max_len, len('forcepermissive'))
elif module_name is not 'help' and not module_name.startswith('_'): elif module_name != 'help' and not module_name.startswith('_'):
modules.append(module_name) modules.append(module_name)
max_len = max(max_len, len(module_name)) max_len = max(max_len, len(module_name))
modules.sort() modules.sort()
@ -212,6 +212,17 @@ class _TiramisuOptionOptionDescription(CommonTiramisuOption):
"""Test if option has dependency""" """Test if option has dependency"""
return self._option_bag.option.impl_has_dependency(self_is_dep) return self._option_bag.option.impl_has_dependency(self_is_dep)
@option_and_connection
async def dependencies(self):
"""Get dependencies from this option"""
options = []
for option in self._option_bag.option._get_dependencies(self._option_bag.config_bag.context):
options.append(TiramisuOption(option().impl_getpath(),
None,
self._option_bag.config_bag,
))
return options
@option_and_connection @option_and_connection
async def isoptiondescription(self): async def isoptiondescription(self):
"""Test if option is an optiondescription""" """Test if option is an optiondescription"""
@ -1533,8 +1544,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 +1555,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,
@ -136,7 +156,9 @@ class Calculation:
option_bag: OptionBag, option_bag: OptionBag,
leadership_must_have_index: bool=False, leadership_must_have_index: bool=False,
orig_value: Any=undefined, orig_value: Any=undefined,
allow_raises=False) -> Any: allow_value_error=False,
force_value_warning=False,
) -> Any:
return await carry_out_calculation(option_bag.option, return await carry_out_calculation(option_bag.option,
callback=self.function, callback=self.function,
callback_params=self.params, callback_params=self.params,
@ -144,7 +166,9 @@ class Calculation:
config_bag=option_bag.config_bag, config_bag=option_bag.config_bag,
leadership_must_have_index=leadership_must_have_index, leadership_must_have_index=leadership_must_have_index,
orig_value=orig_value, orig_value=orig_value,
allow_raises=allow_raises) allow_value_error=allow_value_error,
force_value_warning=force_value_warning,
)
async def help(self, async def help(self,
option_bag: OptionBag, option_bag: OptionBag,
@ -214,7 +238,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 +254,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,12 +303,25 @@ 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
suffix = callbk.suffix
else:
if not option.impl_is_dynsymlinkoption():
msg = 'option "{}" is not dynamic in callback of the option "{}"'
raise ConfigError(_(msg).format(callbk_option.impl_get_display_name(),
option.impl_get_display_name(),
))
rootpath = option.rootpath
suffix = option.impl_getsuffix()
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: if leadership_must_have_index and callbk_option.impl_get_leadership() and index is None:
raise Break() raise Break()
if config_bag is undefined: if config_bag is undefined:
@ -300,13 +344,18 @@ async def manager_callback(callbk: Union[ParamOption, ParamValue],
callbk_option, callbk_option,
index_, index_,
False) False)
value = await get_value(callbk, option_bag, 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,
@ -316,18 +365,18 @@ async def carry_out_calculation(option,
config_bag: Optional[ConfigBag], config_bag: Optional[ConfigBag],
orig_value=undefined, orig_value=undefined,
leadership_must_have_index: bool=False, leadership_must_have_index: bool=False,
allow_raises: int=False): allow_value_error: bool=False,
force_value_warning: bool=False,
):
"""a function that carries out a calculation for an option's value """a function that carries out a calculation for an option's value
:param option: the option :param option: the option
:param callback: the name of the callback function :param callback: the name of the callback function
:type callback: str
:param callback_params: the callback's parameters :param callback_params: the callback's parameters
(only keyword parameters are allowed) (only keyword parameters are allowed)
:type callback_params: dict
:param index: if an option is multi, only calculates the nth value :param index: if an option is multi, only calculates the nth value
:type index: int :param allow_value_error: to know if carry_out_calculation can return ValueError or ValueWarning (for example if it's a validation)
:param allow_raises: to know if carry_out_calculation is used to validate a value :param force_value_warning: transform valueError to ValueWarning object
The callback_params is a dict. Key is used to build args (if key is '') The callback_params is a dict. Key is used to build args (if key is '')
and kwargs (otherwise). Values are tuple of: and kwargs (otherwise). Values are tuple of:
@ -366,11 +415,12 @@ async def carry_out_calculation(option,
continue continue
ret = calculate(option, ret = calculate(option,
callback, callback,
allow_raises, allow_value_error,
force_value_warning,
args, args,
kwargs) kwargs)
if isinstance(ret, list) and not option.impl_is_dynoptiondescription() and \ if isinstance(ret, list) and not option.impl_is_dynoptiondescription() and \
option.impl_is_follower(): option.impl_is_follower() and not option.impl_is_submulti():
if args or kwargs: if args or kwargs:
raise LeadershipError(_('the "{}" function with positional arguments "{}" ' raise LeadershipError(_('the "{}" function with positional arguments "{}" '
'and keyword arguments "{}" must not return ' 'and keyword arguments "{}" must not return '
@ -391,9 +441,11 @@ async def carry_out_calculation(option,
def calculate(option, def calculate(option,
callback: Callable, callback: Callable,
allow_raises: bool, allow_value_error: bool,
force_value_warning: bool,
args, args,
kwargs): kwargs,
):
"""wrapper that launches the 'callback' """wrapper that launches the 'callback'
:param callback: callback function :param callback: callback function
@ -403,12 +455,12 @@ def calculate(option,
""" """
try: try:
return callback(*args, **kwargs) return callback(*args, **kwargs)
except ValueError as err: except (ValueError, ValueWarning) as err:
if allow_raises: if allow_value_error:
if force_value_warning:
raise ValueWarning(str(err))
raise err raise err
error = err error = err
except ValueWarning as err:
raise err
except Exception as err: except Exception as err:
# import traceback # import traceback
# traceback.print_exc() # traceback.print_exc()

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,30 @@ 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 storage is None:
storage = self._storage
if not new:
if session_id not in await list_sessions(storage=storage):
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

@ -311,7 +311,10 @@ class CalcValue:
min_args_len) min_args_len)
if not multi: if not multi:
if join is not None: if join is not None:
if None not in value:
value = join.join(value) value = join.join(value)
else:
value = None
elif value and operator: elif value and operator:
new_value = value[0] new_value = value[0]
op = {'mul': mul, op = {'mul': mul,

View File

@ -55,7 +55,8 @@ def get_translation() -> str:
trans = translation(domain=app_name, trans = translation(domain=app_name,
localedir=translations_path, localedir=translations_path,
languages=[user_locale], languages=[user_locale],
codeset='UTF-8') )
# codeset='UTF-8')
except FileNotFoundError: except FileNotFoundError:
log.debug('cannot found translation file for langage {} in localedir {}'.format(user_locale, log.debug('cannot found translation file for langage {} in localedir {}'.format(user_locale,
translations_path)) translations_path))

View File

@ -106,7 +106,7 @@ class Base:
context_od) -> Set[str]: context_od) -> Set[str]:
ret = set(getattr(self, '_dependencies', STATIC_TUPLE)) ret = set(getattr(self, '_dependencies', STATIC_TUPLE))
if context_od and hasattr(context_od, '_dependencies'): if context_od and hasattr(context_od, '_dependencies'):
# if context is set in options, add those options # add options that have context is set in calculation
return set(context_od._dependencies) | ret return set(context_od._dependencies) | ret
return ret return ret
@ -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,7 +62,11 @@ 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):
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 self._suffixes = suffixes
def convert_suffix_to_path(self, def convert_suffix_to_path(self,

View File

@ -62,7 +62,9 @@ class IPOption(StrOption):
def _validate_ip(self, value): def _validate_ip(self, value):
try: try:
ip_address(value) new_value = str(ip_address(value))
if value != new_value:
raise ValueError(f'should be {new_value}')
except ValueError: except ValueError:
raise ValueError() raise ValueError()

View File

@ -84,7 +84,9 @@ class Option(BaseOption):
is_multi = True is_multi = True
_multi = submulti _multi = submulti
else: else:
raise ValueError(_('invalid multi type "{}"').format(multi)) raise ValueError(_('invalid multi type "{}" for "{}"').format(multi,
name,
))
if _multi != 1: if _multi != 1:
_setattr(self, '_multi', _multi) _setattr(self, '_multi', _multi)
if multi is not False and default is None: if multi is not False and default is None:
@ -93,12 +95,11 @@ 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 not isinstance(validators, list): if __debug__ and 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 not isinstance(validator, Calculation): if __debug__ and 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):
@ -336,7 +337,9 @@ class Option(BaseOption):
if ((check_error and not calc_is_warnings_only) or if ((check_error and not calc_is_warnings_only) or
(not check_error and calc_is_warnings_only)): (not check_error and calc_is_warnings_only)):
try: try:
kwargs = {'allow_raises': True} kwargs = {'allow_value_error': True,
'force_value_warning': calc_is_warnings_only,
}
if _index is not None and option_bag.index == _index: if _index is not None and option_bag.index == _index:
soption_bag = option_bag soption_bag = option_bag
else: else:
@ -347,17 +350,6 @@ class Option(BaseOption):
await validator.execute(soption_bag, await validator.execute(soption_bag,
leadership_must_have_index=True, leadership_must_have_index=True,
**kwargs) **kwargs)
except ValueError as err:
if calc_is_warnings_only:
warnings.warn_explicit(ValueWarning(val,
self._display_name,
self,
'{0}'.format(err),
_index),
ValueWarning,
self.__class__.__name__, 306)
else:
raise err
except ValueWarning as warn: except ValueWarning as warn:
warnings.warn_explicit(ValueWarning(val, warnings.warn_explicit(ValueWarning(val,
self._display_name, self._display_name,
@ -365,7 +357,7 @@ class Option(BaseOption):
'{0}'.format(warn), '{0}'.format(warn),
_index), _index),
ValueWarning, ValueWarning,
self.__class__.__name__, 316) self.__class__.__name__, 356)
async def do_validation(_value, async def do_validation(_value,
_index): _index):

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

View File

@ -198,7 +198,19 @@ class Values:
# now try to get default value: # now try to get default value:
value = await self.calc_value(option_bag, value = await self.calc_value(option_bag,
option_bag.option.impl_getdefault()) option_bag.option.impl_getdefault())
if option_bag.option.impl_is_multi() and option_bag.index is not None and isinstance(value, (list, tuple)): if option_bag.index is not None and isinstance(value, (list, tuple)):
if value and option_bag.option.impl_is_submulti():
# first index is a list, assume other data are list too
if isinstance(value[0], list):
# if index, must return good value for this index
if len(value) > option_bag.index:
value = value[option_bag.index]
else:
# no value for this index, retrieve default multi value
# default_multi is already a list for submulti
value = await self.calc_value(option_bag,
option_bag.option.impl_getdefault_multi())
elif option_bag.option.impl_is_multi():
# if index, must return good value for this index # if index, must return good value for this index
if len(value) > option_bag.index: if len(value) > option_bag.index:
value = value[option_bag.index] value = value[option_bag.index]
@ -229,7 +241,8 @@ class Values:
await option_bag.config_bag.context.cfgimpl_reset_cache(option_bag) await option_bag.config_bag.context.cfgimpl_reset_cache(option_bag)
async def calculate_value(self, async def calculate_value(self,
option_bag: OptionBag) -> Any: option_bag: OptionBag,
) -> Any:
# if value has callback, calculate value # if value has callback, calculate value
callback, callback_params = option_bag.option.impl_get_callback() callback, callback_params = option_bag.option.impl_get_callback()