metaconfig support

This commit is contained in:
Emmanuel Garette 2018-08-17 22:12:50 +02:00
parent 7a4a22f52d
commit bf519499d4
3 changed files with 52 additions and 20 deletions

View File

@ -40,7 +40,7 @@ def make_metaconfig(double=False):
meta = MetaConfig([conf1, conf2], session_id='meta') meta = MetaConfig([conf1, conf2], session_id='meta')
if double: if double:
meta.owner.set(owners.meta2) meta.owner.set(owners.meta2)
meta = MetaConfig([meta]) meta = MetaConfig([meta], session_id='doublemeta')
meta.property.read_write() meta.property.read_write()
meta.owner.set(owners.meta1) meta.owner.set(owners.meta1)
return meta return meta
@ -199,16 +199,28 @@ def test_meta_meta_set():
errors2 = meta.value.set('od1.i6', 7, only_config=True) errors2 = meta.value.set('od1.i6', 7, only_config=True)
assert len(errors1) == 0 assert len(errors1) == 0
assert len(errors2) == 2 assert len(errors2) == 2
meta = meta._config.getconfig('meta') conf1 = meta.config('meta.conf1')._config.context
conf1 = meta.getconfig('conf1') conf2 = meta.config('meta.conf2')._config.context
conf2 = meta.getconfig('conf2')
assert meta.config('meta.conf1').option('od1.i1').value.get() == meta.config('meta.conf2').option('od1.i1').value.get() == 7 assert meta.config('meta.conf1').option('od1.i1').value.get() == meta.config('meta.conf2').option('od1.i1').value.get() == 7
assert [conf1, conf2] == meta.config.find('i1', value=7, first=True).cfgimpl_get_children() #
dconfigs = []
for conf in meta.config.find('i1', value=7, first=True).config.list():
dconfigs.append(conf._config)
assert [conf1, conf2] == dconfigs
meta.config('meta.conf1').option('od1.i1').value.set(8) meta.config('meta.conf1').option('od1.i1').value.set(8)
assert [conf1, conf2] == meta.config.find('i1', first=True).cfgimpl_get_children() #
assert [conf2] == meta.config.find('i1', value=7, first=True).cfgimpl_get_children() dconfigs = []
assert [conf1] == meta.config.find('i1', value=8, first=True).cfgimpl_get_children() for conf in meta.config.find('i1', first=True).config.list():
assert [conf1, conf2] == meta.config.find('i5', value=2, first=True).cfgimpl_get_children() dconfigs.append(conf._config)
assert [conf1, conf2] == dconfigs
assert conf2 == list(meta.config.find('i1', value=7, first=True).config.list())[0]._config
assert conf1 == list(meta.config.find('i1', value=8, first=True).config.list())[0]._config
#
dconfigs = []
for conf in meta.config.find('i5', value=2, first=True).config.list():
dconfigs.append(conf._config)
assert [conf1, conf2] == dconfigs
#
raises(AttributeError, "meta.config.find('i1', value=10, first=True)") raises(AttributeError, "meta.config.find('i1', value=10, first=True)")
raises(AttributeError, "meta.config.find('not', value=10, first=True)") raises(AttributeError, "meta.config.find('not', value=10, first=True)")
raises(AttributeError, "meta.config.find('i6', first=True)") raises(AttributeError, "meta.config.find('i6', first=True)")
@ -245,7 +257,9 @@ def test_group_find_firsts():
conf1 = Config(od2, session_id='conf1') conf1 = Config(od2, session_id='conf1')
conf2 = Config(od2, session_id='conf2') conf2 = Config(od2, session_id='conf2')
grp = GroupConfig([conf1, conf2]) grp = GroupConfig([conf1, conf2])
assert [conf1._config, conf2._config] == grp.config.find('i1', first=True).cfgimpl_get_children() itr = grp.config.find('i1', first=True).config.list()
conf1._config == next(itr)._config
conf2._config == next(itr)._config
def test_group_group(): def test_group_group():
@ -293,13 +307,21 @@ def test_meta_master_slaves():
conf2 = Config(od, session_id='conf2') conf2 = Config(od, session_id='conf2')
meta = MetaConfig([conf1, conf2]) meta = MetaConfig([conf1, conf2])
meta.property.read_only() meta.property.read_only()
assert [conf1._config, conf2._config] == meta.config.find('ip_admin_eth0', first=True).cfgimpl_get_children() itr = meta.config.find('ip_admin_eth0', first=True).config.list()
assert [conf1._config, conf2._config] == meta.config.find('netmask_admin_eth0', first=True).cfgimpl_get_children() assert conf1._config == next(itr)._config
assert conf2._config == next(itr)._config
itr = meta.config.find('netmask_admin_eth0', first=True).config.list()
assert conf1._config == next(itr)._config
assert conf2._config == next(itr)._config
meta.property.read_write() meta.property.read_write()
raises(AttributeError, "meta.config.find('netmask_admin_eth0', first=True)") raises(AttributeError, "meta.config.find('netmask_admin_eth0', first=True)")
assert [conf1._config, conf2._config] == meta.unrestraint.config.find('netmask_admin_eth0', first=True).cfgimpl_get_children() itr = meta.unrestraint.config.find('netmask_admin_eth0', first=True).config.list()
assert conf1._config == next(itr)._config
assert conf2._config == next(itr)._config
meta.property.read_only() meta.property.read_only()
assert [conf1._config, conf2._config] == meta.config.find('netmask_admin_eth0', first=True).cfgimpl_get_children() itr = meta.config.find('netmask_admin_eth0', first=True).config.list()
assert conf1._config == next(itr)._config
assert conf2._config == next(itr)._config
def test_meta_master_slaves_value2(): def test_meta_master_slaves_value2():

View File

@ -850,8 +850,12 @@ class TiramisuContextOption(TiramisuContext):
withoption=None, withoption=None,
fullpath=False): fullpath=False):
"""return dict with path as key and value""" """return dict with path as key and value"""
config_bag = self.config_bag.copy() #config_bag = self.config_bag
config_bag._setting_properties = self.config_bag.setting_properties - {'warnings'} if self.config_bag.setting_properties is None:
config_bag = self.config_bag
else:
config_bag = self.config_bag.copy()
config_bag._setting_properties = self.config_bag.setting_properties - {'warnings'}
return config_bag.context.make_dict(config_bag, return config_bag.context.make_dict(config_bag,
flatten=flatten, flatten=flatten,
fullpath=fullpath, fullpath=fullpath,
@ -919,9 +923,9 @@ class TiramisuContextConfig(TiramisuContext):
first: bool=False): first: bool=False):
"""find a path from option name and optionnaly a value to MetaConfig or GroupConfig""" """find a path from option name and optionnaly a value to MetaConfig or GroupConfig"""
if first: if first:
return self.config_bag.context.find_firsts(byname=name, return TiramisuAPI(self.config_bag.context.find_firsts(byname=name,
byvalue=value, byvalue=value,
config_bag=self.config_bag) config_bag=self.config_bag))
else: else:
raise APIError('not implemented yet') raise APIError('not implemented yet')

View File

@ -181,7 +181,8 @@ class ConfigBag:
return 'validator' in self._setting_properties return 'validator' in self._setting_properties
return self._validate return self._validate
if key == 'setting_properties': if key == 'setting_properties':
if self.force_unrestraint or not self._validate: #if self.force_unrestraint or not self._validate:
if self.force_unrestraint:
return None return None
return self._setting_properties return self._setting_properties
if key == '_setting_properties': if key == '_setting_properties':
@ -196,6 +197,11 @@ class ConfigBag:
def __setattr__(self, key, value): def __setattr__(self, key, value):
if key == 'validate': if key == 'validate':
self._validate = value self._validate = value
#FIXME
try:
del self._setting_properties
except AttributeError:
pass
else: else:
super().__setattr__(key, value) super().__setattr__(key, value)