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: else:
assert cfg_.forcepermissive.option(pathread, 0).owner.isdefault() is True assert cfg_.forcepermissive.option(pathread, 0).owner.isdefault() is True
assert cfg_.forcepermissive.option(pathread, 1).owner.isdefault() is False assert cfg_.forcepermissive.option(pathread, 1).owner.isdefault() is False
#FIXME else:
# raises(PropertiesOptionError, "cfg.config(conf).forcepermissive.option(pathread).owner.isdefault()")
do(confwrite) do(confwrite)
if confwrite != confread: if confwrite != confread:
do(confread) do(confread)

View file

@ -464,7 +464,7 @@ def test_meta_leadership():
assert conf2._config_bag.context == next(itr)._config_bag.context 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) ip_admin_eth0 = StrOption('ip_admin_eth0', "ip", multi=True)
netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',)) netmask_admin_eth0 = StrOption('netmask_admin_eth0', "mask", multi=True, properties=('hidden',))
interface1 = Leadership('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) 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') conf1 = Config(od, session_id='conf1')
conf2 = Config(od, session_id='conf2') conf2 = Config(od, session_id='conf2')
meta = MetaConfig([conf1, conf2], session_id="meta") meta = MetaConfig([conf1, conf2], session_id="meta")
meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.8']) 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 assert 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 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']) 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') 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') 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']) 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 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(): 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' 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']) 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(): 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']) 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.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(): 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'} assert cfg1.value.dict() == {'val1.val2': ['val'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': 'val'}
# #
meta.option('val1.val2', 0).value.set('val2') 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']) meta.option('val1.val1').value.set(['val'])
assert cfg1.value.dict() == {'val1.val2': ['val2'], 'val1.val1': ['val'], 'val1.val3': ['val'], 'val': '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' 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']) 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(): 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' 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']) 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(): 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']) 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.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(): 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'} 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') 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']) mix.option('val1.val1').value.set(['val'])
assert mix.config('cfg1').value.dict() == {'val1.val2': ['val2'], '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'}

View file

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

View file

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

View file

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