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(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') 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(): #def test_validator():
# "validates the integrity of an option towards a whole configuration" # "validates the integrity of an option towards a whole configuration"
# def my_validator_1(config): # def my_validator_1(config):

View File

@ -540,7 +540,10 @@ class Config(object):
def _find(self, bytype, byname, byvalue, byattrs, first): 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 :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(): def _filter_by_attrs():
if byattrs is None: if byattrs is None:
@ -549,8 +552,11 @@ class Config(object):
if not hasattr(option, key): if not hasattr(option, key):
return False return False
else: else:
try:
if getattr(option, key) != value: if getattr(option, key) != value:
return False return False
except:
pass # a property restricts the access of the value
else: else:
continue continue
return True return True
@ -569,7 +575,7 @@ class Config(object):
value = getattr(self, path) value = getattr(self, path)
if value == byvalue: if value == byvalue:
return True return True
except Exception, e: # a property restricts the acces to value except: # a property restricts the access of the value
pass pass
return False return False
def _filter_by_type(): def _filter_by_type():
@ -595,8 +601,9 @@ class Config(object):
return option return option
else: else:
find_results.append(option) find_results.append(option)
if first:
return None if find_results == []:
raise NotFoundError("no option found in config with these criteria")
else: else:
return find_results return find_results