makedict with masterslaves

This commit is contained in:
Emmanuel Garette 2018-08-19 15:19:42 +02:00
parent 5057572115
commit 6ceeb43962
4 changed files with 97 additions and 46 deletions

View File

@ -948,10 +948,26 @@ def test_master_slave_requires():
assert api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() is None assert api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() is None
api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('255.255.255.255') api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.set('255.255.255.255')
assert api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == '255.255.255.255' assert api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get() == '255.255.255.255'
assert api.option.make_dict() == {'ip_admin_eth0.ip_admin_eth0': ['192.168.1.2', '192.168.1.2'],
'ip_admin_eth0.netmask_admin_eth0': [None, '255.255.255.255']}
# #
api.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.2', '192.168.1.1']) api.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.1.2', '192.168.1.1'])
assert api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() is None assert api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() is None
raises(PropertiesOptionError, "api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get()") raises(PropertiesOptionError, "api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get()")
ret = api.option.make_dict()
assert set(ret.keys()) == set(['ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0'])
assert ret['ip_admin_eth0.ip_admin_eth0'] == ['192.168.1.2', '192.168.1.1']
assert len(ret['ip_admin_eth0.netmask_admin_eth0']) == 2
assert ret['ip_admin_eth0.netmask_admin_eth0'][0] == None
assert isinstance(ret['ip_admin_eth0.netmask_admin_eth0'][1], PropertiesOptionError)
#
api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set('255.255.255.255')
ret = api.option.make_dict()
assert set(ret.keys()) == set(['ip_admin_eth0.ip_admin_eth0', 'ip_admin_eth0.netmask_admin_eth0'])
assert ret['ip_admin_eth0.ip_admin_eth0'] == ['192.168.1.2', '192.168.1.1']
assert len(ret['ip_admin_eth0.netmask_admin_eth0']) == 2
assert ret['ip_admin_eth0.netmask_admin_eth0'][0] == '255.255.255.255'
assert isinstance(ret['ip_admin_eth0.netmask_admin_eth0'][1], PropertiesOptionError)
def test_master_slave_requires_no_master(): def test_master_slave_requires_no_master():
@ -981,3 +997,4 @@ def test_master_slave_requires_no_master():
api.option('activate').value.set(False) api.option('activate').value.set(False)
raises(PropertiesOptionError, "api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get()") raises(PropertiesOptionError, "api.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get()")
raises(PropertiesOptionError, "api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get()") raises(PropertiesOptionError, "api.option('ip_admin_eth0.netmask_admin_eth0', 1).value.get()")
assert api.option.make_dict() == {'ip_admin_eth0.ip_admin_eth0': ['192.168.1.2', '192.168.1.1'], 'activate': False}

View File

@ -473,13 +473,32 @@ class SubConfig(object):
:returns: dict of Option's name (or path) and values :returns: dict of Option's name (or path) and values
""" """
pathsvalues = [] pathsvalues = {}
if _currpath is None: if _currpath is None:
_currpath = [] _currpath = []
if withoption is None and withvalue is not undefined: if withoption is None and withvalue is not undefined:
raise ValueError(_("make_dict can't filtering with value without " raise ValueError(_("make_dict can't filtering with value without "
"option")) "option"))
context = self.cfgimpl_get_context() context = self.cfgimpl_get_context()
self._make_dict(context,
config_bag,
flatten,
_currpath,
withoption,
withvalue,
fullpath,
pathsvalues)
return pathsvalues
def _make_dict(self,
context,
config_bag,
flatten,
_currpath,
withoption,
withvalue,
fullpath,
pathsvalues):
if withoption is not None: if withoption is not None:
mypath = self.cfgimpl_get_path() mypath = self.cfgimpl_get_path()
for path in context.find(bytype=None, for path in context.find(bytype=None,
@ -519,7 +538,10 @@ class SubConfig(object):
_currpath, _currpath,
flatten, flatten,
soption_bag, soption_bag,
fullpath=fullpath) fullpath,
context,
withvalue)
#withoption can be set to None below ! #withoption can be set to None below !
if withoption is None: if withoption is None:
for opt in self.cfgimpl_get_description().impl_getchildren(config_bag, context): for opt in self.cfgimpl_get_description().impl_getchildren(config_bag, context):
@ -530,16 +552,14 @@ class SubConfig(object):
path, path,
None, None,
config_bag) config_bag)
#path = self._get_subpath(name)
self._make_sub_dict(name, self._make_sub_dict(name,
pathsvalues, pathsvalues,
_currpath, _currpath,
flatten, flatten,
soption_bag, soption_bag,
fullpath=fullpath) fullpath,
if _currpath == []: context,
options = dict(pathsvalues) withvalue)
return options
return pathsvalues return pathsvalues
def _make_sub_dict(self, def _make_sub_dict(self,
@ -548,13 +568,35 @@ class SubConfig(object):
_currpath, _currpath,
flatten, flatten,
option_bag, option_bag,
fullpath=False): fullpath,
try: context,
withvalue):
option = option_bag.option option = option_bag.option
if not option.impl_is_optiondescription(): if option.impl_is_optiondescription():
try:
self.cfgimpl_get_settings().validate_properties(option_bag)
subconfig = SubConfig(option_bag.option,
self._impl_context,
option_bag.config_bag,
option_bag.path)
subconfig._make_dict(context,
option_bag.config_bag,
flatten,
_currpath + [name],
None,
withvalue,
fullpath,
pathsvalues)
except PropertiesOptionError:
pass
else:
if option.impl_is_master_slaves('slave'): if option.impl_is_master_slaves('slave'):
ret = [] ret = []
try:
self.cfgimpl_get_settings().validate_properties(option_bag)
length = self.cfgimpl_get_length_slave(option_bag) length = self.cfgimpl_get_length_slave(option_bag)
except PropertiesOptionError:
return
if length: if length:
for idx in range(length): for idx in range(length):
soption_bag = OptionBag() soption_bag = OptionBag()
@ -562,31 +604,24 @@ class SubConfig(object):
option_bag.path, option_bag.path,
idx, idx,
option_bag.config_bag) option_bag.config_bag)
try:
ret.append(self.getattr(name, ret.append(self.getattr(name,
soption_bag)) soption_bag))
self.cfgimpl_get_settings().validate_properties(option_bag) except PropertiesOptionError as err:
ret.append(err)
else: else:
try:
ret = self.getattr(name, ret = self.getattr(name,
option_bag) option_bag)
else:
ret = self.get_subconfig(name,
option_bag)
except PropertiesOptionError: except PropertiesOptionError:
pass return
else:
if option.impl_is_optiondescription():
pathsvalues += ret.make_dict(option_bag.config_bag,
flatten=flatten,
_currpath=_currpath + [name],
fullpath=fullpath)
else:
if flatten: if flatten:
name = option.impl_getname() name_ = option.impl_getname()
elif fullpath: elif fullpath:
name = self._get_subpath(name) name_ = self._get_subpath(name)
else: else:
name = '.'.join(_currpath + [name]) name_ = '.'.join(_currpath + [name])
pathsvalues.append((name, ret)) pathsvalues[name_] = ret
def cfgimpl_get_path(self, def cfgimpl_get_path(self,
dyn=True): dyn=True):

View File

@ -498,7 +498,7 @@ class Settings(object):
is_indexed = False is_indexed = False
if option.impl_is_master_slaves('slave'): if option.impl_is_master_slaves('slave'):
idx = option_bag.index idx = option_bag.index
elif option.impl_is_multi(): elif option.impl_is_multi() and option_bag.index is not None:
is_indexed = True is_indexed = True
config_bag = option_bag.config_bag.copy() config_bag = option_bag.config_bag.copy()
config_bag.set_permissive() config_bag.set_permissive()
@ -514,8 +514,7 @@ class Settings(object):
try: try:
value = context.getattr(reqpath, value = context.getattr(reqpath,
soption_bag) soption_bag)
#if is_indexed: if is_indexed:
if is_indexed and option_bag.index is not None:
value = value[option_bag.index] value = value[option_bag.index]
except PropertiesOptionError as err: except PropertiesOptionError as err:
properties = err.proptype properties = err.proptype