diff --git a/test/test_config_api.py b/test/test_config_api.py index 3695049..fcfd7e9 100644 --- a/test/test_config_api.py +++ b/test/test_config_api.py @@ -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): diff --git a/tiramisu/config.py b/tiramisu/config.py index 4bec77e..9e254d9 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -540,7 +540,10 @@ class Config(object): def _find(self, bytype, byname, byvalue, byattrs, first): """ - :param first: return only one option if True, a list otherwise + 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: - if getattr(option, key) != value: - return False + 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