config.py: separate getattr and get_subconfig

This commit is contained in:
Emmanuel Garette 2018-08-02 19:01:54 +02:00
parent 1c2bbc59fd
commit ebeaee620b
5 changed files with 70 additions and 44 deletions

View File

@ -104,7 +104,7 @@ def test_unknown_option():
od2 = OptionDescription('od', '', [od1])
api = getapi(Config(od2))
# test is an option, not an optiondescription
raises(AttributeError, "api.option('od.test.unknown').value.get()")
raises(TypeError, "api.option('od.test.unknown').value.get()")
# unknown is an unknown option
raises(AttributeError, "api.option('unknown').value.get()")
# unknown is an unknown option

View File

@ -156,7 +156,7 @@ def test_slots_config():
'a',
None,
ConfigBag(c))
sc = c.getattr('a', option_bag)
sc = c.get_subconfig('a', option_bag)
assert isinstance(sc, SubConfig)
raises(AttributeError, "sc.x = 1")
raises(AttributeError, "sc.cfgimpl_x = 1")

View File

@ -652,12 +652,12 @@ class TiramisuOption(CommonTiramisu):
fullpath=False):
"""return dict with path as key and value for an optiondescription (only for optiondescription)"""
self._get_option()
return self.config_bag.config.getattr(self._path,
self.option_bag).make_dict(config_bag=self.config_bag,
flatten=flatten,
fullpath=fullpath,
withoption=withoption,
withvalue=withvalue)
return self.config_bag.config.get_subconfig(self._path,
self.option_bag).make_dict(config_bag=self.config_bag,
flatten=flatten,
fullpath=fullpath,
withoption=withoption,
withvalue=withvalue)
def _find(self,
name: str,
@ -732,8 +732,12 @@ class TiramisuOption(CommonTiramisu):
path,
None,
self.config_bag)
self.subconfig.getattr(name,
option_bag)
if opt.impl_is_optiondescription():
self.subconfig.get_subconfig(name,
option_bag)
else:
self.subconfig.getattr(name,
option_bag)
option = self._get_option()
name = option.impl_getname()
@ -743,8 +747,8 @@ class TiramisuOption(CommonTiramisu):
path,
None,
self.config_bag)
subconfig = self.subconfig.getattr(name,
option_bag)
subconfig = self.subconfig.get_subconfig(name,
option_bag)
for opt in option.impl_getchildren(self.config_bag):
try:
subsubconfig = _filter(opt)

View File

@ -64,8 +64,12 @@ class SubConfig(object):
if descr is not None and (not isinstance(descr, (BaseOption, SynDynOptionDescription)) or not descr.impl_is_optiondescription()):
error = True
if error:
raise TypeError(_('descr must be an optiondescription, not {0}'
).format(type(descr)))
try:
msg = descr.impl_get_displayname()
except AttributeError:
msg = descr
raise TypeError(_('"{0}" must be an optiondescription, not an {1}'
).format(msg, type(descr)))
self._impl_descr = descr
self._impl_context = context
self._impl_path = subpath
@ -212,8 +216,8 @@ class SubConfig(object):
config_bag)
if fromconsistency is not None:
option_bag.fromconsistency = fromconsistency
self = self.getattr(step,
option_bag)
self = self.get_subconfig(step,
option_bag)
if not isinstance(self, SubConfig):
raise AttributeError(_('unknown option {}').format(path[-1]))
return self, path[-1]
@ -285,6 +289,30 @@ class SubConfig(object):
subpath = self._impl_path + '.' + name
return subpath
def get_subconfig(self,
name,
option_bag):
if '.' in name:
if option_bag.fromconsistency:
fromconsistency = option_bag.fromconsistency.copy()
else:
fromconsistency = None
self, name = self.cfgimpl_get_home_by_path(name,
option_bag.config_bag,
fromconsistency)
elif option_bag.fromconsistency:
fromconsistency = option_bag.fromconsistency.copy()
else:
fromconsistency = None
if option_bag.config_bag.setting_properties:
self.cfgimpl_get_settings().validate_properties(option_bag)
return SubConfig(option_bag.option,
self._impl_context,
option_bag.config_bag,
option_bag.path,
fromconsistency)
def getattr(self,
name,
option_bag):
@ -318,16 +346,6 @@ class SubConfig(object):
if config_bag.setting_properties:
self.cfgimpl_get_settings().validate_properties(option_bag)
if option.impl_is_optiondescription():
if option_bag.fromconsistency:
fromconsistency = option_bag.fromconsistency.copy()
else:
fromconsistency = None
return SubConfig(option,
self._impl_context,
config_bag,
option_bag.path,
fromconsistency)
if option.impl_is_master_slaves('slave'):
length = self.cfgimpl_get_length_slave(option_bag)
@ -546,23 +564,27 @@ class SubConfig(object):
fullpath=False):
try:
option = option_bag.option
if not option.impl_is_optiondescription() and option.impl_is_master_slaves('slave'):
ret = []
length = self.cfgimpl_get_length_slave(option_bag)
if length:
for idx in range(length):
soption_bag = OptionBag()
soption_bag.set_option(option,
option_bag.path,
idx,
option_bag.config_bag)
ret.append(self.getattr(name,
soption_bag))
elif option_bag.config_bag.setting_properties:
self.cfgimpl_get_settings().validate_properties(option_bag)
if not option.impl_is_optiondescription():
if option.impl_is_master_slaves('slave'):
ret = []
length = self.cfgimpl_get_length_slave(option_bag)
if length:
for idx in range(length):
soption_bag = OptionBag()
soption_bag.set_option(option,
option_bag.path,
idx,
option_bag.config_bag)
ret.append(self.getattr(name,
soption_bag))
elif option_bag.config_bag.setting_properties:
self.cfgimpl_get_settings().validate_properties(option_bag)
else:
ret = self.getattr(name,
option_bag)
else:
ret = self.getattr(name,
option_bag)
ret = self.get_subconfig(name,
option_bag)
except PropertiesOptionError:
pass
else:

View File

@ -593,8 +593,8 @@ class Values(object):
path,
None,
config_bag)
subconfig = config.getattr(name,
option_bag)
subconfig = config.get_subconfig(name,
option_bag)
except PropertiesOptionError as err:
pass
else: