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]) od2 = OptionDescription('od', '', [od1])
api = getapi(Config(od2)) api = getapi(Config(od2))
# test is an option, not an optiondescription # 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 # unknown is an unknown option
raises(AttributeError, "api.option('unknown').value.get()") raises(AttributeError, "api.option('unknown').value.get()")
# unknown is an unknown option # unknown is an unknown option

View File

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

View File

@ -652,12 +652,12 @@ class TiramisuOption(CommonTiramisu):
fullpath=False): fullpath=False):
"""return dict with path as key and value for an optiondescription (only for optiondescription)""" """return dict with path as key and value for an optiondescription (only for optiondescription)"""
self._get_option() self._get_option()
return self.config_bag.config.getattr(self._path, return self.config_bag.config.get_subconfig(self._path,
self.option_bag).make_dict(config_bag=self.config_bag, self.option_bag).make_dict(config_bag=self.config_bag,
flatten=flatten, flatten=flatten,
fullpath=fullpath, fullpath=fullpath,
withoption=withoption, withoption=withoption,
withvalue=withvalue) withvalue=withvalue)
def _find(self, def _find(self,
name: str, name: str,
@ -732,8 +732,12 @@ class TiramisuOption(CommonTiramisu):
path, path,
None, None,
self.config_bag) self.config_bag)
self.subconfig.getattr(name, if opt.impl_is_optiondescription():
option_bag) self.subconfig.get_subconfig(name,
option_bag)
else:
self.subconfig.getattr(name,
option_bag)
option = self._get_option() option = self._get_option()
name = option.impl_getname() name = option.impl_getname()
@ -743,8 +747,8 @@ class TiramisuOption(CommonTiramisu):
path, path,
None, None,
self.config_bag) self.config_bag)
subconfig = self.subconfig.getattr(name, subconfig = self.subconfig.get_subconfig(name,
option_bag) option_bag)
for opt in option.impl_getchildren(self.config_bag): for opt in option.impl_getchildren(self.config_bag):
try: try:
subsubconfig = _filter(opt) 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()): if descr is not None and (not isinstance(descr, (BaseOption, SynDynOptionDescription)) or not descr.impl_is_optiondescription()):
error = True error = True
if error: if error:
raise TypeError(_('descr must be an optiondescription, not {0}' try:
).format(type(descr))) 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_descr = descr
self._impl_context = context self._impl_context = context
self._impl_path = subpath self._impl_path = subpath
@ -212,8 +216,8 @@ class SubConfig(object):
config_bag) config_bag)
if fromconsistency is not None: if fromconsistency is not None:
option_bag.fromconsistency = fromconsistency option_bag.fromconsistency = fromconsistency
self = self.getattr(step, self = self.get_subconfig(step,
option_bag) option_bag)
if not isinstance(self, SubConfig): if not isinstance(self, SubConfig):
raise AttributeError(_('unknown option {}').format(path[-1])) raise AttributeError(_('unknown option {}').format(path[-1]))
return self, path[-1] return self, path[-1]
@ -285,6 +289,30 @@ class SubConfig(object):
subpath = self._impl_path + '.' + name subpath = self._impl_path + '.' + name
return subpath 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, def getattr(self,
name, name,
option_bag): option_bag):
@ -318,16 +346,6 @@ class SubConfig(object):
if config_bag.setting_properties: if config_bag.setting_properties:
self.cfgimpl_get_settings().validate_properties(option_bag) 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'): if option.impl_is_master_slaves('slave'):
length = self.cfgimpl_get_length_slave(option_bag) length = self.cfgimpl_get_length_slave(option_bag)
@ -546,23 +564,27 @@ class SubConfig(object):
fullpath=False): fullpath=False):
try: try:
option = option_bag.option option = option_bag.option
if not option.impl_is_optiondescription() and option.impl_is_master_slaves('slave'): if not option.impl_is_optiondescription():
ret = [] if option.impl_is_master_slaves('slave'):
length = self.cfgimpl_get_length_slave(option_bag) ret = []
if length: length = self.cfgimpl_get_length_slave(option_bag)
for idx in range(length): if length:
soption_bag = OptionBag() for idx in range(length):
soption_bag.set_option(option, soption_bag = OptionBag()
option_bag.path, soption_bag.set_option(option,
idx, option_bag.path,
option_bag.config_bag) idx,
ret.append(self.getattr(name, option_bag.config_bag)
soption_bag)) ret.append(self.getattr(name,
elif option_bag.config_bag.setting_properties: soption_bag))
self.cfgimpl_get_settings().validate_properties(option_bag) elif option_bag.config_bag.setting_properties:
self.cfgimpl_get_settings().validate_properties(option_bag)
else:
ret = self.getattr(name,
option_bag)
else: else:
ret = self.getattr(name, ret = self.get_subconfig(name,
option_bag) option_bag)
except PropertiesOptionError: except PropertiesOptionError:
pass pass
else: else:

View File

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