better way to manage follower in MetaConfig

This commit is contained in:
Emmanuel Garette 2019-08-07 08:38:32 +02:00
parent 2d7d237e19
commit b6ec77aa23
7 changed files with 35 additions and 50 deletions

View file

@ -949,8 +949,6 @@ def autocheck_default_owner_with_value_permissive(cfg, mcfg, pathread, pathwrite
else:
assert cfg_.forcepermissive.option(pathread, 0).owner.isdefault() is True
assert cfg_.forcepermissive.option(pathread, 1).owner.isdefault() is False
#FIXME else:
# raises(PropertiesOptionError, "cfg.config(conf).forcepermissive.option(pathread).owner.isdefault()")
do(confwrite)
if confwrite != confread:
do(confread)

View file

@ -464,7 +464,7 @@ def test_meta_leadership():
assert conf2._config_bag.context == next(itr)._config_bag.context
def test_meta_leadership_value2():
def test_meta_leadership_value():
ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0])
@ -472,21 +472,28 @@ def test_meta_leadership_value2():
conf1 = Config(od, session_id='conf1')
conf2 = Config(od, session_id='conf2')
meta = MetaConfig([conf1, conf2], session_id="meta")
meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.8'])
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
#FIXME devrait raise ! assert meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0', 0).value.get() == None
conf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.8'])
assert conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
raises(APIError, "conf1.option('ip_admin_eth0.ip_admin_eth0', 0).value.get()")
#
meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.reset()
conf1.option('ip_admin_eth0.ip_admin_eth0').value.reset()
#
meta.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
meta.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.255.0')
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.255.0'
assert conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.255.0'
meta.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.0.0')
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
assert conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
#
meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
conf1.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
#
meta.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.2.1', '192.168.3.1'])
meta.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('255.255.255.0')
#
assert conf1.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1']
assert conf1.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
def test_meta_leadership_value_default():
@ -509,7 +516,7 @@ def test_meta_leadership_value_default():
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
#
meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
def test_meta_leadership_owners():
@ -545,7 +552,7 @@ def test_meta_leadership_owners():
#
meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').owner.get() == owners.user
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).owner.get() == owners.default
assert meta.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).owner.get() == owners.meta1
def test_meta_force_default():
@ -810,7 +817,7 @@ def test_meta_callback_follower():
assert cfg1.value.dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
#
meta.option('val1.val2', 0).value.set('val2')
assert cfg1.value.dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
assert cfg1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
#
meta.option('val1.val1').value.set(['val'])
assert cfg1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}

View file

@ -363,7 +363,7 @@ def test_mix_leadership_value2():
assert mix.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
#
mix.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert mix.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert mix.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
def test_mix_leadership_value_default():
@ -386,7 +386,7 @@ def test_mix_leadership_value_default():
assert mix.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
#
mix.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert mix.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == None
assert mix.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() == '255.255.0.0'
def test_mix_leadership_owners():
@ -422,7 +422,7 @@ def test_mix_leadership_owners():
#
mix.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.1'])
assert mix.config('conf1').option('ip_admin_eth0.ip_admin_eth0').owner.get() == owners.user
assert mix.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).owner.get() == owners.default
assert mix.config('conf1').option('ip_admin_eth0.netmask_admin_eth0', 0).owner.get() == owners.mix1
def test_mix_force_default():
@ -667,7 +667,7 @@ def test_mix_callback_follower():
assert mix.config('cfg1').value.dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
#
mix.option('val1.val2', 0).value.set('val2')
assert mix.config('cfg1').value.dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
assert mix.config('cfg1').value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
#
mix.option('val1.val1').value.set(['val'])
assert mix.config('cfg1').value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}

View file

@ -345,17 +345,17 @@ class TiramisuOptionOwner(CommonTiramisuOption):
def get(self):
"""Get owner for a specified option"""
option = self._option_bag.option
self._option_bag.option
return self._values.getowner(self._option_bag)
def isdefault(self):
"""Is option has defaut value"""
option = self._option_bag.option
self._option_bag.option
return self._values.is_default_owner(self._option_bag)
def set(self, owner):
"""Get owner for a specified option"""
option = self._option_bag.option
self._option_bag.option
try:
obj_owner = getattr(owners, owner)
except AttributeError:

View file

@ -45,7 +45,6 @@ class Leadership(OptionDescription):
children: List[BaseOption],
requires=None,
properties=None) -> None:
super().__init__(name,
doc,
children,

View file

@ -81,3 +81,7 @@ class SynDynOption:
def impl_is_dynsymlinkoption(self) -> bool:
return True
def impl_get_leadership(self):
return self.opt.impl_get_leadership().to_dynoption(self.rootpath,
self.suffix)

View file

@ -259,12 +259,10 @@ class Values(object):
#______________________________________________________________________
# set value
def setvalue(self,
value,
option_bag,
_commit):
context = option_bag.config_bag.context
owner = self.get_context_owner()
if 'validator' in option_bag.config_bag.properties:
@ -295,7 +293,6 @@ class Values(object):
def setvalue_validation(self,
value,
option_bag):
settings = option_bag.config_bag.context.cfgimpl_get_settings()
# First validate properties with this value
opt = option_bag.option
@ -317,7 +314,6 @@ class Values(object):
value,
owner,
commit=True):
option_bag.config_bag.context.cfgimpl_reset_cache(option_bag)
if isinstance(value, list):
# copy
@ -329,8 +325,8 @@ class Values(object):
commit)
def _get_modified_parent(self,
option_bag):
""" Search in differents parents a Config with a modified value and return it
option_bag: OptionBag) -> Optional[OptionBag]:
""" Search in differents parents a Config with a modified value
If not found, return None
For follower option, return the Config where leader is modified
"""
@ -342,26 +338,6 @@ class Values(object):
doption_bag.config_bag = config_bag
return doption_bag
if option_bag.option.impl_is_follower():
leader = option_bag.option.impl_get_leadership().get_leader()
leaderpath = leader.impl_getpath()
if self._p_.hasvalue(leaderpath,
index=None):
return None
config_bag = option_bag.config_bag
leader_option_bag = OptionBag()
leader_option_bag.set_option(leader,
leaderpath,
None,
config_bag)
leader_option_bag = self._get_modified_parent(leader_option_bag)
if leader_option_bag is None:
return None
new_config_bag = leader_option_bag.config_bag
if not new_config_bag.context.cfgimpl_get_values()._p_.hasvalue(option_bag.path,
index=option_bag.index):
return None
return build_option_bag(option_bag, new_config_bag.context)
for parent in option_bag.config_bag.context.get_parents():
doption_bag = build_option_bag(option_bag, parent)
if 'force_metaconfig_on_freeze' in option_bag.properties:
@ -377,6 +353,7 @@ class Values(object):
only_default=True)
if parent_owner != owners.default:
return doption_bag
return None
@ -417,7 +394,7 @@ class Values(object):
if only_default:
if self._p_.hasvalue(option_bag.path,
option_bag.index):
owner = option_bag
owner = 'not_default'
else:
owner = owners.default
else: