tiramisu/config.py can specify return type for find ('option', 'value', 'path') and remove get

user need replace config.get(value) by config.find(byname="value", type_='value')
This commit is contained in:
Emmanuel Garette 2013-04-05 12:20:33 +02:00
parent c918191d21
commit d100d66548
3 changed files with 17 additions and 38 deletions

View file

@ -380,10 +380,10 @@ def test_allow_multiple_changes_from_config():
def test_access_by_get():
descr = make_description()
cfg = Config(descr)
raises(NotFoundError, "cfg.get('idontexist')" )
assert cfg.get('wantref') == False
raises(NotFoundError, "cfg.find(byname='idontexist')" )
assert cfg.find_first(byname='wantref', type_='value') == False
assert cfg.gc.dummy == False
assert cfg.get('dummy') == False
assert cfg.find_first(byname='dummy', type_='value') == False
def test_access_by_get_whith_hide():
b1 = BoolOption("b1", "", properties=(('hidden'),))
@ -399,4 +399,4 @@ def test_access_by_get_whith_hide():
c = Config(descr)
setting = c.cfgimpl_get_settings()
setting.read_write()
raises(NotFoundError, "c.get('b1')")
raises(NotFoundError, "c.find(byname='b1')")

View file

@ -40,7 +40,7 @@ def test_base_config():
config = Config(descr)
assert config.creole.general.activer_proxy_client == False
assert config.creole.general.nom_machine == "eoleng"
assert config.get('nom_machine') == "eoleng"
assert config.find_first(byname='nom_machine', type_='value') == "eoleng"
result = {'general.numero_etab': None, 'general.nombre_interfaces': 1,
'general.serveur_ntp': [], 'interface1.ip_admin_eth0.ip_admin_eth0': None,
'general.mode_conteneur_actif': False, 'general.time_zone': 'Paris',

View file

@ -280,23 +280,8 @@ class SubConfig(object):
context_descr = self.cfgimpl_get_context().cfgimpl_get_description()
return context_descr.get_path_by_opt(descr)
def get(self, name):
"""
same as a `find_first()` method in a config that has identical names:
it returns the first item of an option named `name`
much like the attribute access way, except that
the search for the option is performed recursively in the whole
configuration tree.
:returns: option value.
"""
return self.cfgimpl_get_context()._find(byname=name, bytype=None,
byvalue=None, byattrs=None,
first=True, ret='value',
_subpath=self.getpath())
def find(self, bytype=None, byname=None, byvalue=None, byattrs=None):
def find(self, bytype=None, byname=None, byvalue=None, byattrs=None,
type_='option'):
"""
finds a list of options recursively in the config
@ -308,9 +293,11 @@ class SubConfig(object):
"""
return self.cfgimpl_get_context()._find(bytype, byname, byvalue,
byattrs, first=False,
type_=type_,
_subpath=self.getpath())
def find_first(self, bytype=None, byname=None, byvalue=None, byattrs=None):
def find_first(self, bytype=None, byname=None, byvalue=None, byattrs=None,
type_='option'):
"""
finds an option recursively in the config
@ -322,6 +309,7 @@ class SubConfig(object):
"""
return self.cfgimpl_get_context()._find(bytype, byname, byvalue,
byattrs, first=True,
type_=type_,
_subpath=self.getpath())
def make_dict(self, flatten=False, _currpath=None, withoption=None, withvalue=None):
@ -382,7 +370,7 @@ class Config(SubConfig):
"main configuration management entry"
__slots__ = ('_cfgimpl_settings', '_cfgimpl_values')
def __init__(self, descr, valid_opt_names=True):
def __init__(self, descr):
""" Configuration option management master class
:param descr: describes the configuration schema
@ -394,15 +382,6 @@ class Config(SubConfig):
"""
self._cfgimpl_settings = Setting()
self._cfgimpl_values = Values(self)
#if valid_opt_names:
# # some api members shall not be used as option's names !
# #FIXME fait une boucle infini ...
# #methods = getmembers(self, ismethod)
# #slots = tuple([key for key, value in methods
# # if not key.startswith("_")])
# slots = []
#else:
# slots = []
super(Config, self).__init__(descr, None, self) # , slots)
self._cfgimpl_build_all_paths()
@ -454,7 +433,7 @@ class Config(SubConfig):
def getpath(self):
return None
def _find(self, bytype, byname, byvalue, byattrs, first, ret='option',
def _find(self, bytype, byname, byvalue, byattrs, first, type_='option',
_subpath=None):
"""
convenience method for finding an option that lives only in the subtree
@ -500,8 +479,8 @@ class Config(SubConfig):
else:
continue
return True
if ret not in ('option', 'path', 'value'):
raise ValueError('unknown ret type {} for _find'.format(ret))
if type_ not in ('option', 'path', 'value'):
raise ValueError('unknown type_ type {} for _find'.format(type_))
find_results = []
opts, paths = self.cfgimpl_get_description()._cache_paths
for index in range(0, len(paths)):
@ -524,9 +503,9 @@ class Config(SubConfig):
value = getattr(self, path)
except: # a property restricts the access of the value
continue
if ret == 'value':
if type_ == 'value':
retval = value
elif ret == 'path':
elif type_ == 'path':
retval = path
else:
retval = option