Compare commits
23 Commits
pkg/dev/ri
...
29ca5d18fa
Author | SHA1 | Date | |
---|---|---|---|
29ca5d18fa | |||
3e0e7244ef | |||
e1fef11465 | |||
a269cbcc93 | |||
cd50225bad | |||
6910371779 | |||
afeedc1db6 | |||
2db305b1f0 | |||
ba23a144ce | |||
baa56616bd | |||
50d42624cc | |||
b82480f5d6 | |||
659243ba8f | |||
dc8010f0af | |||
96c76286db | |||
5c1d4afd56 | |||
c4572ec409 | |||
2f125cfc8c | |||
2d7729612b | |||
86a48ce9f1 | |||
f437bb78f3 | |||
f7bd6e3a47 | |||
d71018a88e |
@ -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>
|
||||
* version 3.0 rc15
|
||||
* add parents method to MetaConfig
|
||||
|
6
debian/control
vendored
6
debian/control
vendored
@ -1,12 +1,12 @@
|
||||
Source: tiramisu
|
||||
Source: python3-tiramisu3
|
||||
Section: admin
|
||||
Priority: extra
|
||||
Maintainer: Cadoles <contact@cadoles.com>
|
||||
Build-depends: debhelper (>=11), python3-all, python3-setuptools
|
||||
Build-depends: debhelper (>=11), python3-all, python3-setuptools, dh-python
|
||||
Standards-Version: 3.9.4
|
||||
Homepage: https://framagit.org/tiramisu/tiramisu.git
|
||||
|
||||
Package: tiramisu
|
||||
Package: python3-tiramisu3
|
||||
Architecture: any
|
||||
Pre-Depends: dpkg, python3, ${misc:Pre-Depends}
|
||||
Depends: ${python:Depends}, ${misc:Depends}
|
||||
|
5
debian/rules
vendored
5
debian/rules
vendored
@ -3,8 +3,9 @@
|
||||
# output every command that modifies files on the build system.
|
||||
#DH_VERBOSE = 1
|
||||
|
||||
export PYBUILD_NAME = tiramisu
|
||||
export PYBUILD_DISABLE_python3 = test
|
||||
export PACKAGE_DST=tiramisu3
|
||||
export PYBUILD_NAME=python3-tiramisu3
|
||||
export PYBUILD_DISABLE_python3=test
|
||||
|
||||
%:
|
||||
dh $@ --with python3 --buildsystem=pybuild
|
||||
|
13
setup.py
13
setup.py
@ -1,11 +1,17 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from setuptools import setup, find_packages
|
||||
from setuptools import setup
|
||||
import os
|
||||
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(
|
||||
version=__version__,
|
||||
@ -40,5 +46,6 @@ producing flexible and fast options access.
|
||||
This version requires Python 3.5 or later.
|
||||
""",
|
||||
include_package_data=True,
|
||||
packages=find_packages(include=['tiramisu'])
|
||||
package_dir=package_dir,
|
||||
packages=[PACKAGE_NAME],
|
||||
)
|
||||
|
@ -59,6 +59,16 @@ async def test_copy():
|
||||
assert not await list_sessions()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_copy_information():
|
||||
od = make_description()
|
||||
async with await Config(od) as cfg:
|
||||
await cfg.information.set('key', 'value')
|
||||
async with await cfg.config.copy() as ncfg:
|
||||
assert await ncfg.information.get('key') == 'value'
|
||||
assert not await list_sessions()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_copy_force_store_value():
|
||||
od = make_description()
|
||||
|
@ -9,7 +9,9 @@ from tiramisu import BoolOption, StrOption, ChoiceOption, IPOption, \
|
||||
StrOption, PortOption, BroadcastOption, DomainnameOption, \
|
||||
EmailOption, URLOption, UsernameOption, FilenameOption, SymLinkOption, \
|
||||
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
|
||||
from tiramisu.error import PropertiesOptionError, ConfigError, ConflictError
|
||||
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.dodval1').option.name() == 'dodval1'
|
||||
assert await cfg.option('od.dodval2').option.name() == 'dodval2'
|
||||
assert await cfg.option('od.dodval1.stval1').option.doc() == 'doc1'
|
||||
assert await cfg.option('od.dodval2.stval2').option.doc() == 'doc1'
|
||||
assert await cfg.option('od.dodval1.stval1').option.doc() == 'doc1val1'
|
||||
assert await cfg.option('od.dodval2.stval2').option.doc() == 'doc1val2'
|
||||
assert await cfg.option('od.dodval1').option.doc() == 'doc2val1'
|
||||
assert await cfg.option('od.dodval2').option.doc() == 'doc2val2'
|
||||
assert not await list_sessions()
|
||||
@ -289,6 +291,56 @@ async def test_callback_dyndescription():
|
||||
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
|
||||
async def test_callback_list_dyndescription():
|
||||
st = StrOption('st', '', Calculation(return_list2, Params(ParamSuffix())), multi=True, properties=('notunique',))
|
||||
|
@ -187,26 +187,6 @@ async def test_force_store_value():
|
||||
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
|
||||
async def test_force_store_value_leadership_sub():
|
||||
b = IntOption('int', 'Test int option', multi=True, properties=('force_store_value',))
|
||||
|
@ -358,6 +358,27 @@ async def test_meta_new_config_wrong_name():
|
||||
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
|
||||
async def test_meta_meta_set():
|
||||
meta = await make_metaconfig(double=True)
|
||||
|
@ -10,7 +10,7 @@ from tiramisu.setting import groups, owners
|
||||
from tiramisu import ChoiceOption, BoolOption, IntOption, FloatOption, \
|
||||
StrOption, OptionDescription, SymLinkOption, IPOption, NetmaskOption, Leadership, \
|
||||
undefined, Calculation, Params, ParamOption, ParamValue, ParamIndex, calc_value, \
|
||||
valid_ip_netmask, ParamSelfOption
|
||||
valid_ip_netmask, ParamSelfOption, ParamInformation
|
||||
from tiramisu.error import PropertiesOptionError, ConflictError, LeadershipError, ConfigError
|
||||
from tiramisu.i18n import _
|
||||
from tiramisu.storage import list_sessions
|
||||
@ -43,6 +43,10 @@ def return_value(value=None):
|
||||
return value
|
||||
|
||||
|
||||
async def return_async_value(value=None):
|
||||
return value
|
||||
|
||||
|
||||
def return_value2(*args, **kwargs):
|
||||
value = list(args)
|
||||
value.extend(kwargs.values())
|
||||
@ -333,6 +337,50 @@ async def test_callback_value(config_type):
|
||||
assert not await list_sessions()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_callback_async_value(config_type):
|
||||
val1 = StrOption('val1', "", 'val')
|
||||
val2 = StrOption('val2', "", Calculation(return_async_value, Params(ParamOption(val1))))
|
||||
val3 = StrOption('val3', "", Calculation(return_async_value, Params(ParamValue('yes'))))
|
||||
val4 = StrOption('val4', "", Calculation(return_async_value, Params(kwargs={'value': ParamOption(val1)})))
|
||||
val5 = StrOption('val5', "", Calculation(return_async_value, Params(ParamValue('yes'))))
|
||||
maconfig = OptionDescription('rootconfig', '', [val1, val2, val3, val4, val5])
|
||||
async with await Config(maconfig) as cfg:
|
||||
await cfg.property.read_write()
|
||||
cfg = await get_config(cfg, config_type)
|
||||
assert await cfg.option('val1').value.get() == 'val'
|
||||
assert await cfg.option('val2').value.get() == 'val'
|
||||
assert await cfg.option('val4').value.get() == 'val'
|
||||
await cfg.option('val1').value.set('new-val')
|
||||
assert await cfg.option('val1').value.get() == 'new-val'
|
||||
assert await cfg.option('val2').value.get() == 'new-val'
|
||||
assert await cfg.option('val4').value.get() == 'new-val'
|
||||
await cfg.option('val1').value.reset()
|
||||
assert await cfg.option('val1').value.get() == 'val'
|
||||
assert await cfg.option('val2').value.get() == 'val'
|
||||
assert await cfg.option('val3').value.get() == 'yes'
|
||||
assert await cfg.option('val4').value.get() == 'val'
|
||||
assert await cfg.option('val5').value.get() == 'yes'
|
||||
assert not await list_sessions()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_callback_information(config_type):
|
||||
val1 = StrOption('val1', "", Calculation(return_value, Params(ParamInformation('information', 'no_value'))))
|
||||
val2 = StrOption('val2', "", Calculation(return_value, Params(ParamInformation('information'))))
|
||||
maconfig = OptionDescription('rootconfig', '', [val1, val2])
|
||||
async with await Config(maconfig) as cfg:
|
||||
await cfg.property.read_write()
|
||||
cfg = await get_config(cfg, config_type)
|
||||
assert await cfg.option('val1').value.get() == 'no_value'
|
||||
with pytest.raises(ConfigError):
|
||||
await cfg.option('val2').value.get()
|
||||
await cfg.information.set('information', 'new_value')
|
||||
assert await cfg.option('val1').value.get() == 'new_value'
|
||||
assert await cfg.option('val2').value.get() == 'new_value'
|
||||
assert not await list_sessions()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_callback_value_tuple(config_type):
|
||||
val1 = StrOption('val1', "", 'val1')
|
||||
@ -1433,11 +1481,14 @@ async def test_calc_value_remove_duplicate(config_type):
|
||||
async def test_calc_value_join(config_type):
|
||||
val1 = StrOption('val1', "", 'val1')
|
||||
val2 = StrOption('val2', "", 'val2')
|
||||
val3 = StrOption('val3', "", Calculation(calc_value, Params((ParamOption(val1), ParamOption(val2)), join=ParamValue('.'))))
|
||||
od = OptionDescription('root', '', [val1, val2, val3])
|
||||
val3 = StrOption('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:
|
||||
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()
|
||||
|
||||
|
||||
|
@ -5,7 +5,6 @@ import pytest
|
||||
import warnings
|
||||
|
||||
|
||||
from tiramisu.api import TIRAMISU_VERSION
|
||||
from tiramisu.setting import groups, owners
|
||||
from tiramisu import StrOption, IntOption, OptionDescription, submulti, Leadership, Config, \
|
||||
MetaConfig, undefined, Params, ParamOption, Calculation
|
||||
@ -38,11 +37,7 @@ async def test_unknown_multi():
|
||||
@pytest.mark.asyncio
|
||||
async def test_submulti():
|
||||
multi = StrOption('multi', '', multi=submulti)
|
||||
if TIRAMISU_VERSION == 2:
|
||||
default_multi = 'yes'
|
||||
else:
|
||||
default_multi = ['yes']
|
||||
multi2 = StrOption('multi2', '', default_multi=default_multi, multi=submulti)
|
||||
multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
|
||||
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
|
||||
od = OptionDescription('od', '', [multi, multi2, multi3])
|
||||
async with await Config(od) as cfg:
|
||||
@ -66,11 +61,7 @@ async def test_submulti_default_multi_not_list():
|
||||
@pytest.mark.asyncio
|
||||
async def test_append_submulti():
|
||||
multi = StrOption('multi', '', multi=submulti)
|
||||
if TIRAMISU_VERSION == 2:
|
||||
default_multi = 'yes'
|
||||
else:
|
||||
default_multi = ['yes']
|
||||
multi2 = StrOption('multi2', '', default_multi=default_multi, multi=submulti)
|
||||
multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
|
||||
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
|
||||
od = OptionDescription('od', '', [multi, multi2, multi3])
|
||||
async with await Config(od) as cfg:
|
||||
@ -104,11 +95,7 @@ async def test_append_submulti():
|
||||
@pytest.mark.asyncio
|
||||
async def test_append_unvalide_submulti():
|
||||
multi = StrOption('multi', '', multi=submulti)
|
||||
if TIRAMISU_VERSION == 2:
|
||||
default_multi = 'yes'
|
||||
else:
|
||||
default_multi = ['yes']
|
||||
multi2 = StrOption('multi2', '', default_multi=default_multi, multi=submulti)
|
||||
multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
|
||||
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
|
||||
od = OptionDescription('od', '', [multi, multi2, multi3])
|
||||
async with await Config(od) as cfg:
|
||||
@ -137,11 +124,7 @@ async def test_append_unvalide_submulti():
|
||||
@pytest.mark.asyncio
|
||||
async def test_pop_submulti():
|
||||
multi = StrOption('multi', '', multi=submulti)
|
||||
if TIRAMISU_VERSION == 2:
|
||||
default_multi = 'yes'
|
||||
else:
|
||||
default_multi = ['yes']
|
||||
multi2 = StrOption('multi2', '', default_multi=default_multi, multi=submulti)
|
||||
multi2 = StrOption('multi2', '', default_multi=['yes'], multi=submulti)
|
||||
multi3 = StrOption('multi3', '', default=[['yes']], multi=submulti)
|
||||
od = OptionDescription('od', '', [multi, multi2, multi3])
|
||||
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()
|
||||
|
||||
|
||||
@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
|
||||
async def test_reset_values_with_leader_and_followers_submulti():
|
||||
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()
|
||||
|
||||
|
||||
@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
|
||||
async def test_submulti_unique():
|
||||
i = IntOption('int', '', multi=submulti, properties=('unique',))
|
||||
|
@ -17,8 +17,8 @@
|
||||
from .function import calc_value, calc_value_property_help, valid_ip_netmask, \
|
||||
valid_network_netmask, valid_in_network, valid_broadcast, \
|
||||
valid_not_equal
|
||||
from .autolib import Calculation, Params, ParamOption, ParamSelfOption, ParamValue, \
|
||||
ParamIndex, ParamSuffix
|
||||
from .autolib import Calculation, Params, ParamOption, ParamDynOption, ParamSelfOption, \
|
||||
ParamValue, ParamIndex, ParamSuffix, ParamInformation
|
||||
from .option import *
|
||||
from .error import APIError
|
||||
from .api import Config, MetaConfig, GroupConfig, MixConfig
|
||||
@ -31,10 +31,12 @@ from .storage import default_storage, Storage, list_sessions, \
|
||||
allfuncs = ['Calculation',
|
||||
'Params',
|
||||
'ParamOption',
|
||||
'ParamDynOption',
|
||||
'ParamSelfOption',
|
||||
'ParamValue',
|
||||
'ParamIndex',
|
||||
'ParamSuffix',
|
||||
'ParamInformation',
|
||||
'MetaConfig',
|
||||
'MixConfig',
|
||||
'GroupConfig',
|
||||
|
@ -52,7 +52,7 @@ class TiramisuHelp:
|
||||
if module_name in ['forcepermissive', 'unrestraint']:
|
||||
force = True
|
||||
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)
|
||||
max_len = max(max_len, len(module_name))
|
||||
modules.sort()
|
||||
@ -87,8 +87,8 @@ class CommonTiramisu(TiramisuHelp):
|
||||
|
||||
async def _get_option(self,
|
||||
connection) -> Any:
|
||||
config_bag = self._option_bag.config_bag
|
||||
if not self._subconfig:
|
||||
config_bag = self._option_bag.config_bag
|
||||
try:
|
||||
subconfig, name = await config_bag.context.cfgimpl_get_home_by_path(self._option_bag.path,
|
||||
config_bag,
|
||||
@ -101,7 +101,7 @@ class CommonTiramisu(TiramisuHelp):
|
||||
self._name = name
|
||||
option = self._option_bag.option
|
||||
if option is None:
|
||||
option = await self._subconfig.cfgimpl_get_description().get_child(name,
|
||||
option = await self._subconfig.cfgimpl_get_description().get_child(self._name,
|
||||
config_bag,
|
||||
self._subconfig.cfgimpl_get_path())
|
||||
self._option_bag.option = option
|
||||
@ -212,6 +212,17 @@ class _TiramisuOptionOptionDescription(CommonTiramisuOption):
|
||||
"""Test if option has dependency"""
|
||||
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
|
||||
async def isoptiondescription(self):
|
||||
"""Test if option is an optiondescription"""
|
||||
@ -874,24 +885,35 @@ def connection(func):
|
||||
class TiramisuContextInformation(TiramisuConfig):
|
||||
"""Manage config informations"""
|
||||
@connection
|
||||
async def get(self, name, default=undefined):
|
||||
async def get(self,
|
||||
name,
|
||||
default=undefined,
|
||||
):
|
||||
"""Get an information"""
|
||||
return await self._config_bag.context.impl_get_information(self._config_bag.connection,
|
||||
name,
|
||||
default)
|
||||
default,
|
||||
)
|
||||
|
||||
@connection
|
||||
async def set(self, name, value):
|
||||
async def set(self,
|
||||
name,
|
||||
value,
|
||||
):
|
||||
"""Set an information"""
|
||||
await self._config_bag.context.impl_set_information(self._config_bag.connection,
|
||||
await self._config_bag.context.impl_set_information(self._config_bag,
|
||||
name,
|
||||
value)
|
||||
value,
|
||||
)
|
||||
|
||||
@connection
|
||||
async def reset(self, name):
|
||||
async def reset(self,
|
||||
name,
|
||||
):
|
||||
"""Remove an information"""
|
||||
await self._config_bag.context.impl_del_information(self._config_bag.connection,
|
||||
name)
|
||||
name,
|
||||
)
|
||||
|
||||
@connection
|
||||
async def list(self):
|
||||
@ -1533,8 +1555,7 @@ class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextCon
|
||||
async def new(self,
|
||||
session_id,
|
||||
storage=None,
|
||||
type='config',
|
||||
new=None):
|
||||
type='config'):
|
||||
"""Create and add a new config"""
|
||||
config = self._config_bag.context
|
||||
if storage is None:
|
||||
@ -1545,7 +1566,26 @@ class _TiramisuContextMixConfig(_TiramisuContextGroupConfig, _TiramisuContextCon
|
||||
session_id=session_id,
|
||||
storage=storage,
|
||||
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,
|
||||
storage)
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
# ____________________________________________________________
|
||||
"enables us to carry out a calculation and return an option's value"
|
||||
from typing import Any, Optional, Union, Callable, Dict, List
|
||||
from types import CoroutineType
|
||||
from itertools import chain
|
||||
|
||||
from .error import PropertiesOptionError, ConfigError, LeadershipError, ValueWarning
|
||||
@ -81,6 +82,25 @@ class ParamOption(Param):
|
||||
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):
|
||||
__slots__ = ('todict', 'whole')
|
||||
def __init__(self,
|
||||
@ -98,6 +118,16 @@ class ParamValue(Param):
|
||||
self.value = value
|
||||
|
||||
|
||||
class ParamInformation(Param):
|
||||
__slots__ = ('information_name',)
|
||||
def __init__(self,
|
||||
information_name: str,
|
||||
default_value: Any=undefined,
|
||||
) -> None:
|
||||
self.information_name = information_name
|
||||
self.default_value = default_value
|
||||
|
||||
|
||||
class ParamIndex(Param):
|
||||
__slots__ = tuple()
|
||||
|
||||
@ -136,7 +166,9 @@ class Calculation:
|
||||
option_bag: OptionBag,
|
||||
leadership_must_have_index: bool=False,
|
||||
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,
|
||||
callback=self.function,
|
||||
callback_params=self.params,
|
||||
@ -144,7 +176,9 @@ class Calculation:
|
||||
config_bag=option_bag.config_bag,
|
||||
leadership_must_have_index=leadership_must_have_index,
|
||||
orig_value=orig_value,
|
||||
allow_raises=allow_raises)
|
||||
allow_value_error=allow_value_error,
|
||||
force_value_warning=force_value_warning,
|
||||
)
|
||||
|
||||
async def help(self,
|
||||
option_bag: OptionBag,
|
||||
@ -175,7 +209,7 @@ class Break(Exception):
|
||||
pass
|
||||
|
||||
|
||||
async def manager_callback(callbk: Union[ParamOption, ParamValue],
|
||||
async def manager_callback(callbk: Param,
|
||||
option,
|
||||
index: Optional[int],
|
||||
orig_value,
|
||||
@ -214,7 +248,10 @@ async def manager_callback(callbk: Union[ParamOption, ParamValue],
|
||||
value = value[apply_index]
|
||||
return value
|
||||
|
||||
async def get_value(callbk, option_bag, path):
|
||||
async def get_value(callbk,
|
||||
option_bag,
|
||||
path,
|
||||
):
|
||||
try:
|
||||
# get value
|
||||
value = await config_bag.context.getattr(path,
|
||||
@ -227,6 +264,10 @@ async def manager_callback(callbk: Union[ParamOption, ParamValue],
|
||||
', {}').format(option.impl_get_display_name(), err), 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))
|
||||
except AttributeError as err:
|
||||
raise ConfigError(_('impossible to calculate "{0}", {1}').format(option_bag.option.impl_get_display_name(),
|
||||
err,
|
||||
))
|
||||
return value
|
||||
|
||||
async def get_option_bag(config_bag,
|
||||
@ -255,12 +296,23 @@ async def manager_callback(callbk: Union[ParamOption, ParamValue],
|
||||
if isinstance(callbk, ParamValue):
|
||||
return callbk.value
|
||||
|
||||
if isinstance(callbk, ParamInformation):
|
||||
try:
|
||||
return await config_bag.context.impl_get_information(config_bag.connection,
|
||||
callbk.information_name,
|
||||
callbk.default_value,
|
||||
)
|
||||
except ValueError as err:
|
||||
raise ConfigError(_('option "{}" cannot be calculated: {}').format(option.impl_get_display_name(),
|
||||
str(err),
|
||||
))
|
||||
|
||||
if isinstance(callbk, ParamIndex):
|
||||
return index
|
||||
|
||||
if isinstance(callbk, ParamSuffix):
|
||||
if not option.issubdyn():
|
||||
raise ConfigError('option "{}" is not in a dynoptiondescription'.format(option.impl_get_display_name()))
|
||||
raise ConfigError(_('option "{}" is not in a dynoptiondescription').format(option.impl_get_display_name()))
|
||||
return option.impl_getsuffix()
|
||||
|
||||
if isinstance(callbk, ParamSelfOption):
|
||||
@ -272,41 +324,59 @@ async def manager_callback(callbk: Union[ParamOption, ParamValue],
|
||||
return {'name': option.impl_get_display_name(),
|
||||
'value': value}
|
||||
|
||||
# it's ParamOption
|
||||
callbk_option = callbk.option
|
||||
if callbk_option.issubdyn():
|
||||
callbk_option = callbk_option.to_dynoption(option.rootpath,
|
||||
option.impl_getsuffix(),
|
||||
callbk_option.getsubdyn())
|
||||
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
|
||||
if isinstance(callbk, ParamOption):
|
||||
callbk_option = callbk.option
|
||||
if callbk_option.issubdyn():
|
||||
if isinstance(callbk, ParamDynOption):
|
||||
subdyn = callbk.dynoptiondescription
|
||||
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:
|
||||
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:
|
||||
# follower
|
||||
index_ = index
|
||||
index_ = None
|
||||
with_index = False
|
||||
else:
|
||||
index_ = None
|
||||
with_index = False
|
||||
path = callbk_option.impl_getpath()
|
||||
option_bag = await get_option_bag(config_bag,
|
||||
callbk_option,
|
||||
index_,
|
||||
False)
|
||||
value = await get_value(callbk, option_bag, path)
|
||||
if with_index:
|
||||
value = value[index]
|
||||
if not callbk.todict:
|
||||
return value
|
||||
return {'name': callbk_option.impl_get_display_name(),
|
||||
'value': value}
|
||||
path = callbk_option.impl_getpath()
|
||||
option_bag = await get_option_bag(config_bag,
|
||||
callbk_option,
|
||||
index_,
|
||||
False)
|
||||
value = await get_value(callbk,
|
||||
option_bag,
|
||||
path,
|
||||
)
|
||||
if with_index:
|
||||
value = value[index]
|
||||
if not callbk.todict:
|
||||
return value
|
||||
return {'name': callbk_option.impl_get_display_name(),
|
||||
'value': value}
|
||||
raise ConfigError(_('unknown callback type {} in option {}').format(callbk,
|
||||
option.impl_get_display_name()))
|
||||
|
||||
|
||||
async def carry_out_calculation(option,
|
||||
@ -316,18 +386,18 @@ async def carry_out_calculation(option,
|
||||
config_bag: Optional[ConfigBag],
|
||||
orig_value=undefined,
|
||||
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
|
||||
|
||||
:param option: the option
|
||||
:param callback: the name of the callback function
|
||||
:type callback: str
|
||||
:param callback_params: the callback's parameters
|
||||
(only keyword parameters are allowed)
|
||||
:type callback_params: dict
|
||||
:param index: if an option is multi, only calculates the nth value
|
||||
:type index: int
|
||||
:param allow_raises: to know if carry_out_calculation is used to validate a value
|
||||
:param allow_value_error: to know if carry_out_calculation can return ValueError or ValueWarning (for example if it's a validation)
|
||||
:param force_value_warning: transform valueError to ValueWarning object
|
||||
|
||||
The callback_params is a dict. Key is used to build args (if key is '')
|
||||
and kwargs (otherwise). Values are tuple of:
|
||||
@ -364,13 +434,14 @@ async def carry_out_calculation(option,
|
||||
kwargs[key] = {'propertyerror': str(err)}
|
||||
except Break:
|
||||
continue
|
||||
ret = calculate(option,
|
||||
callback,
|
||||
allow_raises,
|
||||
args,
|
||||
kwargs)
|
||||
ret = await calculate(option,
|
||||
callback,
|
||||
allow_value_error,
|
||||
force_value_warning,
|
||||
args,
|
||||
kwargs)
|
||||
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:
|
||||
raise LeadershipError(_('the "{}" function with positional arguments "{}" '
|
||||
'and keyword arguments "{}" must not return '
|
||||
@ -389,11 +460,13 @@ async def carry_out_calculation(option,
|
||||
return ret
|
||||
|
||||
|
||||
def calculate(option,
|
||||
callback: Callable,
|
||||
allow_raises: bool,
|
||||
args,
|
||||
kwargs):
|
||||
async def calculate(option,
|
||||
callback: Callable,
|
||||
allow_value_error: bool,
|
||||
force_value_warning: bool,
|
||||
args,
|
||||
kwargs,
|
||||
):
|
||||
"""wrapper that launches the 'callback'
|
||||
|
||||
:param callback: callback function
|
||||
@ -402,13 +475,16 @@ def calculate(option,
|
||||
|
||||
"""
|
||||
try:
|
||||
return callback(*args, **kwargs)
|
||||
except ValueError as err:
|
||||
if allow_raises:
|
||||
ret = callback(*args, **kwargs)
|
||||
if isinstance(ret, CoroutineType):
|
||||
ret = await ret
|
||||
return ret
|
||||
except (ValueError, ValueWarning) as err:
|
||||
if allow_value_error:
|
||||
if force_value_warning:
|
||||
raise ValueWarning(str(err))
|
||||
raise err
|
||||
error = err
|
||||
except ValueWarning as err:
|
||||
raise err
|
||||
except Exception as err:
|
||||
# import traceback
|
||||
# traceback.print_exc()
|
||||
|
@ -64,11 +64,11 @@ class SubConfig:
|
||||
(not isinstance(descr, (BaseOption, SynDynOptionDescription)) or
|
||||
not descr.impl_is_optiondescription()):
|
||||
try:
|
||||
msg = descr.impl_get_displayname()
|
||||
msg = descr.impl_get_display_name()
|
||||
except AttributeError:
|
||||
msg = descr
|
||||
raise TypeError(_('"{0}" must be an optiondescription, not an {1}'
|
||||
).format(msg, type(descr)))
|
||||
raise TypeError(_('cannot create a sub config for "{0}" this is a "{1}", not an "OptionDescription"'
|
||||
).format(msg, descr.__class__.__name__))
|
||||
self._impl_descr = descr
|
||||
self._impl_context = context
|
||||
self._impl_path = subpath
|
||||
@ -124,6 +124,18 @@ class SubConfig:
|
||||
await self.reset_one_option_cache(desc,
|
||||
resetted_opts,
|
||||
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():
|
||||
# it's an option in dynoptiondescription, remove cache for all generated option
|
||||
dynopt = option.getsubdyn()
|
||||
@ -550,17 +562,24 @@ class _CommonConfig(SubConfig):
|
||||
|
||||
# information
|
||||
async def impl_set_information(self,
|
||||
connection,
|
||||
config_bag,
|
||||
key,
|
||||
value):
|
||||
value,
|
||||
):
|
||||
"""updates the information's attribute
|
||||
|
||||
:param key: information's key (ex: "help", "doc"
|
||||
:param value: information's value (ex: "the help string")
|
||||
"""
|
||||
await self._impl_values.set_information(connection,
|
||||
await self._impl_values.set_information(config_bag.connection,
|
||||
key,
|
||||
value)
|
||||
for option in config_bag.context.cfgimpl_get_description()._cache_dependencies_information.get(key, []):
|
||||
option_bag = OptionBag()
|
||||
option_bag.set_option(option,
|
||||
None,
|
||||
config_bag)
|
||||
await config_bag.context.cfgimpl_reset_cache(option_bag)
|
||||
|
||||
async def impl_get_information(self,
|
||||
connection,
|
||||
@ -635,6 +654,9 @@ class _CommonConfig(SubConfig):
|
||||
duplicated_settings = duplicated_config.cfgimpl_get_settings()
|
||||
await duplicated_values._p_.importation(connection,
|
||||
await self.cfgimpl_get_values()._p_.exportation(connection))
|
||||
await duplicated_values._p_.importation_informations(connection,
|
||||
await self.cfgimpl_get_values()._p_.exportation_informations(connection),
|
||||
)
|
||||
properties = await self.cfgimpl_get_settings()._p_.exportation(connection)
|
||||
await duplicated_settings._p_.importation(connection,
|
||||
properties)
|
||||
@ -1176,13 +1198,30 @@ class KernelMixConfig(KernelGroupConfig):
|
||||
session_id,
|
||||
type_='config',
|
||||
storage=None,
|
||||
new=None,
|
||||
):
|
||||
if new is None:
|
||||
new = session_id not in await list_sessions()
|
||||
if new and session_id in [child.impl_getname() for child in self._impl_children]:
|
||||
if session_id in [child.impl_getname() for child in self._impl_children]:
|
||||
raise ConflictError(_('config name must be uniq in '
|
||||
'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_)
|
||||
if type_ == 'config':
|
||||
config = await KernelConfig(self._impl_descr,
|
||||
|
@ -311,7 +311,10 @@ class CalcValue:
|
||||
min_args_len)
|
||||
if not multi:
|
||||
if join is not None:
|
||||
value = join.join(value)
|
||||
if None not in value:
|
||||
value = join.join(value)
|
||||
else:
|
||||
value = None
|
||||
elif value and operator:
|
||||
new_value = value[0]
|
||||
op = {'mul': mul,
|
||||
|
@ -55,7 +55,8 @@ def get_translation() -> str:
|
||||
trans = translation(domain=app_name,
|
||||
localedir=translations_path,
|
||||
languages=[user_locale],
|
||||
codeset='UTF-8')
|
||||
)
|
||||
# codeset='UTF-8')
|
||||
except FileNotFoundError:
|
||||
log.debug('cannot found translation file for langage {} in localedir {}'.format(user_locale,
|
||||
translations_path))
|
||||
|
@ -106,7 +106,7 @@ class Base:
|
||||
context_od) -> Set[str]:
|
||||
ret = set(getattr(self, '_dependencies', STATIC_TUPLE))
|
||||
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 ret
|
||||
|
||||
@ -270,20 +270,36 @@ class BaseOption(Base):
|
||||
return self.impl_get_callback()[0] is not None
|
||||
|
||||
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)
|
||||
if name is None or name == '':
|
||||
if dyn_name is not None:
|
||||
name = dyn_name
|
||||
else:
|
||||
name = self.impl_getname()
|
||||
elif suffix:
|
||||
name += suffix
|
||||
return name
|
||||
|
||||
def impl_get_display_name(self,
|
||||
dyn_name: Base=None) -> str:
|
||||
def _get_display_name(self,
|
||||
dyn_name,
|
||||
suffix,
|
||||
):
|
||||
if hasattr(self, '_display_name_function'):
|
||||
return self._display_name_function(self, dyn_name)
|
||||
return self._impl_get_display_name(dyn_name)
|
||||
return self._display_name_function(self,
|
||||
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,
|
||||
path: str,
|
||||
|
@ -76,17 +76,18 @@ class ChoiceOption(Option):
|
||||
if isinstance(self._choice_values, Calculation):
|
||||
return
|
||||
values = self._choice_values
|
||||
if values is not undefined and value not in values:
|
||||
if len(values) == 1:
|
||||
raise ValueError(_('only "{0}" is allowed'
|
||||
'').format(values[0]))
|
||||
raise ValueError(_('only {0} are allowed'
|
||||
'').format(display_list(values, add_quote=True)))
|
||||
self.validate_values(value, values)
|
||||
|
||||
async def validate_with_option(self,
|
||||
value: Any,
|
||||
option_bag: OptionBag) -> None:
|
||||
values = await self.impl_get_values(option_bag)
|
||||
self.validate_values(value, values)
|
||||
|
||||
def validate_values(self,
|
||||
value,
|
||||
values,
|
||||
) -> None:
|
||||
if values is not undefined and value not in values:
|
||||
if len(values) == 1:
|
||||
raise ValueError(_('only "{0}" is allowed'
|
||||
|
@ -20,6 +20,8 @@
|
||||
# ____________________________________________________________
|
||||
import re
|
||||
from typing import List, Callable
|
||||
from itertools import chain
|
||||
from ..autolib import ParamOption
|
||||
|
||||
|
||||
from ..i18n import _
|
||||
@ -60,8 +62,12 @@ class DynOptionDescription(OptionDescription):
|
||||
'dynoptiondescription'))
|
||||
child._setsubdyn(self)
|
||||
# add suffixes
|
||||
if __debug__ and isinstance(suffixes, Calculation):
|
||||
self._suffixes = suffixes
|
||||
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
|
||||
|
||||
def convert_suffix_to_path(self,
|
||||
suffix):
|
||||
|
@ -62,7 +62,9 @@ class IPOption(StrOption):
|
||||
|
||||
def _validate_ip(self, value):
|
||||
try:
|
||||
ip_address(value)
|
||||
new_value = str(ip_address(value))
|
||||
if value != new_value:
|
||||
raise ValueError(f'should be {new_value}')
|
||||
except ValueError:
|
||||
raise ValueError()
|
||||
|
||||
|
@ -27,7 +27,7 @@ from itertools import chain
|
||||
from .baseoption import BaseOption, submulti, STATIC_TUPLE
|
||||
from ..i18n import _
|
||||
from ..setting import undefined, OptionBag, Undefined
|
||||
from ..autolib import Calculation, Params, ParamValue, ParamOption
|
||||
from ..autolib import Calculation, Params, ParamOption, ParamInformation
|
||||
from ..error import (ConfigError, ValueWarning, ValueErrorWarning, PropertiesOptionError,
|
||||
ValueOptionError, display_list)
|
||||
from .syndynoption import SynDynOption
|
||||
@ -50,6 +50,7 @@ class Option(BaseOption):
|
||||
#
|
||||
'_validators',
|
||||
#
|
||||
'_dependencies_information',
|
||||
'_leadership',
|
||||
'_choice_values',
|
||||
'_choice_values_params',
|
||||
@ -66,6 +67,7 @@ class Option(BaseOption):
|
||||
warnings_only: bool=False,
|
||||
extra: Optional[Dict]=None):
|
||||
_setattr = object.__setattr__
|
||||
_dependencies_information = []
|
||||
if not multi and default_multi is not None:
|
||||
raise ValueError(_("default_multi is set whereas multi is False"
|
||||
" in option: {0}").format(name))
|
||||
@ -84,7 +86,9 @@ class Option(BaseOption):
|
||||
is_multi = True
|
||||
_multi = submulti
|
||||
else:
|
||||
raise ValueError(_('invalid multi type "{}"').format(multi))
|
||||
raise ValueError(_('invalid multi type "{}" for "{}"').format(multi,
|
||||
name,
|
||||
))
|
||||
if _multi != 1:
|
||||
_setattr(self, '_multi', _multi)
|
||||
if multi is not False and default is None:
|
||||
@ -93,17 +97,18 @@ class Option(BaseOption):
|
||||
doc,
|
||||
properties=properties,
|
||||
is_multi=is_multi)
|
||||
if __debug__:
|
||||
if validators is not None:
|
||||
if not isinstance(validators, list):
|
||||
raise ValueError(_('validators must be a list of Calculation for "{}"').format(name))
|
||||
for validator in validators:
|
||||
if not isinstance(validator, Calculation):
|
||||
raise ValueError(_('validators must be a Calculation for "{}"').format(name))
|
||||
for param in chain(validator.params.args, validator.params.kwargs.values()):
|
||||
if isinstance(param, ParamOption):
|
||||
param.option._add_dependency(self)
|
||||
self._has_dependency = True
|
||||
if validators is not None:
|
||||
if __debug__ and not isinstance(validators, list):
|
||||
raise ValueError(_('validators must be a list of Calculation for "{}"').format(name))
|
||||
for validator in validators:
|
||||
if __debug__ and not isinstance(validator, Calculation):
|
||||
raise ValueError(_('validators must be a Calculation for "{}"').format(name))
|
||||
for param in chain(validator.params.args, validator.params.kwargs.values()):
|
||||
if isinstance(param, ParamOption):
|
||||
param.option._add_dependency(self)
|
||||
self._has_dependency = True
|
||||
elif isinstance(param, ParamInformation):
|
||||
_dependencies_information.append(param.information_name)
|
||||
|
||||
self._validators = tuple(validators)
|
||||
if extra is not None and extra != {}:
|
||||
@ -154,29 +159,37 @@ class Option(BaseOption):
|
||||
self.sync_impl_validate(default,
|
||||
option_bag,
|
||||
check_error=False)
|
||||
self.value_dependencies(default)
|
||||
self.value_dependencies(default, _dependencies_information)
|
||||
if (is_multi and default != []) or \
|
||||
(not is_multi and default is not None):
|
||||
if is_multi and isinstance(default, list):
|
||||
default = tuple(default)
|
||||
_setattr(self, '_default', default)
|
||||
if _dependencies_information:
|
||||
self._dependencies_information = _dependencies_information
|
||||
|
||||
def value_dependencies(self,
|
||||
value: Any) -> Any:
|
||||
value: Any,
|
||||
_dependencies_information: List[str],
|
||||
) -> Any:
|
||||
if isinstance(value, list):
|
||||
for val in value:
|
||||
if isinstance(value, list):
|
||||
self.value_dependencies(val)
|
||||
self.value_dependencies(val, _dependencies_information)
|
||||
elif isinstance(value, Calculation):
|
||||
self.value_dependency(val)
|
||||
self.value_dependency(val, _dependencies_information)
|
||||
elif isinstance(value, Calculation):
|
||||
self.value_dependency(value)
|
||||
self.value_dependency(value, _dependencies_information)
|
||||
|
||||
def value_dependency(self,
|
||||
value: Any) -> Any:
|
||||
value: Any,
|
||||
_dependencies_information: List[str],
|
||||
) -> Any:
|
||||
for param in chain(value.params.args, value.params.kwargs.values()):
|
||||
if isinstance(param, ParamOption):
|
||||
param.option._add_dependency(self)
|
||||
elif isinstance(param, ParamInformation):
|
||||
_dependencies_information.append(param.information_name)
|
||||
|
||||
#__________________________________________________________________________
|
||||
# option's information
|
||||
@ -190,6 +203,9 @@ class Option(BaseOption):
|
||||
def impl_is_dynsymlinkoption(self) -> bool:
|
||||
return False
|
||||
|
||||
def get_dependencies_information(self) -> List[str]:
|
||||
return getattr(self, '_dependencies_information', [])
|
||||
|
||||
def get_type(self) -> str:
|
||||
# _display_name for compatibility with older version than 3.0rc3
|
||||
return getattr(self, '_type', self._display_name)
|
||||
@ -336,7 +352,9 @@ class Option(BaseOption):
|
||||
if ((check_error and not calc_is_warnings_only) or
|
||||
(not check_error and calc_is_warnings_only)):
|
||||
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:
|
||||
soption_bag = option_bag
|
||||
else:
|
||||
@ -347,17 +365,6 @@ class Option(BaseOption):
|
||||
await validator.execute(soption_bag,
|
||||
leadership_must_have_index=True,
|
||||
**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:
|
||||
warnings.warn_explicit(ValueWarning(val,
|
||||
self._display_name,
|
||||
@ -365,7 +372,7 @@ class Option(BaseOption):
|
||||
'{0}'.format(warn),
|
||||
_index),
|
||||
ValueWarning,
|
||||
self.__class__.__name__, 316)
|
||||
self.__class__.__name__, 356)
|
||||
|
||||
async def do_validation(_value,
|
||||
_index):
|
||||
|
@ -30,7 +30,9 @@ from ..error import ConfigError, ConflictError
|
||||
|
||||
|
||||
class CacheOptionDescription(BaseOption):
|
||||
__slots__ = ('_cache_force_store_values',)
|
||||
__slots__ = ('_cache_force_store_values',
|
||||
'_cache_dependencies_information',
|
||||
)
|
||||
|
||||
def impl_already_build_caches(self) -> bool:
|
||||
return self.impl_is_readonly()
|
||||
@ -42,7 +44,9 @@ class CacheOptionDescription(BaseOption):
|
||||
currpath: List[str]=None,
|
||||
cache_option=None,
|
||||
force_store_values=None,
|
||||
display_name=None) -> None:
|
||||
dependencies_information=None,
|
||||
display_name=None,
|
||||
) -> None:
|
||||
"""validate options and set option has readonly option
|
||||
"""
|
||||
# _consistencies is None only when we start to build cache
|
||||
@ -52,6 +56,7 @@ class CacheOptionDescription(BaseOption):
|
||||
if __debug__:
|
||||
cache_option = []
|
||||
force_store_values = []
|
||||
dependencies_information = {}
|
||||
currpath = []
|
||||
else:
|
||||
init = False
|
||||
@ -73,8 +78,11 @@ class CacheOptionDescription(BaseOption):
|
||||
sub_currpath,
|
||||
cache_option,
|
||||
force_store_values,
|
||||
dependencies_information,
|
||||
display_name)
|
||||
else:
|
||||
for information in option.get_dependencies_information():
|
||||
dependencies_information.setdefault(information, []).append(option)
|
||||
is_multi = option.impl_is_multi()
|
||||
if not option.impl_is_symlinkoption():
|
||||
properties = option.impl_getproperties()
|
||||
@ -102,6 +110,7 @@ class CacheOptionDescription(BaseOption):
|
||||
option._set_readonly()
|
||||
if init:
|
||||
self._cache_force_store_values = force_store_values
|
||||
self._cache_dependencies_information = dependencies_information
|
||||
self._path = self._name
|
||||
self._set_readonly()
|
||||
|
||||
|
@ -59,7 +59,9 @@ class SynDynOption:
|
||||
return self.opt.impl_getname() + self.suffix
|
||||
|
||||
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:
|
||||
return self.suffix
|
||||
|
@ -25,14 +25,11 @@ from copy import deepcopy
|
||||
|
||||
class Values:
|
||||
__slots__ = ('_values',
|
||||
'_informations',
|
||||
'_storage',
|
||||
'__weakref__')
|
||||
def __init__(self, storage):
|
||||
"""init plugin means create values storage
|
||||
"""
|
||||
#self._values = ([], [], [], [])
|
||||
#self._informations = {}
|
||||
self._storage = storage
|
||||
|
||||
def _setvalue_info(self, nb, idx, value, index, follower_idx=None):
|
||||
@ -296,6 +293,18 @@ class Values:
|
||||
connection):
|
||||
self._storage.set_informations({})
|
||||
|
||||
async def exportation_informations(self,
|
||||
connection,
|
||||
):
|
||||
return deepcopy(self._storage.get_informations())
|
||||
|
||||
async def importation_informations(self,
|
||||
connection,
|
||||
informations,
|
||||
):
|
||||
#deepcopy(informations)
|
||||
return self._storage.set_informations(informations)
|
||||
|
||||
async def exportation(self,
|
||||
connection):
|
||||
return deepcopy(self._storage.get_values())
|
||||
|
@ -231,6 +231,24 @@ class Values:
|
||||
await connection.execute("DELETE FROM information WHERE session_id = $1",
|
||||
self._storage.database_id)
|
||||
|
||||
async def exportation_informations(self,
|
||||
connection,
|
||||
):
|
||||
informations = {}
|
||||
for path, key, value in await connection.fetch("SELECT path, key, value FROM information WHERE session_id = $1", self._storage.database_id):
|
||||
path = self._storage.load_path(path)
|
||||
informations.setdefault(path, {})[key] = loads(value)
|
||||
return informations
|
||||
|
||||
async def importation_informations(self,
|
||||
connection,
|
||||
informations,
|
||||
):
|
||||
for path, path_infos in informations.items():
|
||||
for key, value in path_infos.items():
|
||||
await connection.execute("INSERT INTO information(key, value, session_id, path) VALUES "
|
||||
"($1, $2, $3, $4)", key, dumps(value), self._storage.database_id, path)
|
||||
|
||||
async def exportation(self,
|
||||
connection):
|
||||
# log.debug('exportation')
|
||||
|
@ -198,15 +198,27 @@ class Values:
|
||||
# now try to get default value:
|
||||
value = await self.calc_value(option_bag,
|
||||
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 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())
|
||||
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 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())
|
||||
return value
|
||||
|
||||
async def calculate_reset_cache(self,
|
||||
@ -229,7 +241,8 @@ class Values:
|
||||
await option_bag.config_bag.context.cfgimpl_reset_cache(option_bag)
|
||||
|
||||
async def calculate_value(self,
|
||||
option_bag: OptionBag) -> Any:
|
||||
option_bag: OptionBag,
|
||||
) -> Any:
|
||||
|
||||
# if value has callback, calculate value
|
||||
callback, callback_params = option_bag.option.impl_get_callback()
|
||||
|
Reference in New Issue
Block a user