find raises an exception if no option found Fixes #4721

This commit is contained in:
gwen 2013-01-28 09:55:51 +01:00
parent c66dcd09ee
commit e2a02c5b7a
2 changed files with 18 additions and 6 deletions

View file

@ -157,6 +157,11 @@ def test_find_in_config():
assert conf.find_first(byattrs= dict(default=2.3)) == conf.unwrap_from_name('float')
assert conf.find_first(byvalue=False, byname='dummy', byattrs=dict(default=False)) == conf.unwrap_from_name('dummy')
def test_does_not_find_in_config():
descr = make_description()
conf = Config(descr)
raises(NotFoundError, "conf.find(byname='IDontExist')")
#def test_validator():
# "validates the integrity of an option towards a whole configuration"
# def my_validator_1(config):

View file

@ -540,7 +540,10 @@ class Config(object):
def _find(self, bytype, byname, byvalue, byattrs, first):
"""
convenience method for finding an option that lives only in the subtree
:param first: return only one option if True, a list otherwise
:return: find list or an exception if nothing has been found
"""
def _filter_by_attrs():
if byattrs is None:
@ -549,8 +552,11 @@ class Config(object):
if not hasattr(option, key):
return False
else:
try:
if getattr(option, key) != value:
return False
except:
pass # a property restricts the access of the value
else:
continue
return True
@ -569,7 +575,7 @@ class Config(object):
value = getattr(self, path)
if value == byvalue:
return True
except Exception, e: # a property restricts the acces to value
except: # a property restricts the access of the value
pass
return False
def _filter_by_type():
@ -595,8 +601,9 @@ class Config(object):
return option
else:
find_results.append(option)
if first:
return None
if find_results == []:
raise NotFoundError("no option found in config with these criteria")
else:
return find_results