find raises an exception if no option found Fixes #4721
authorgwen <gremond@cadoles.com>
Mon, 28 Jan 2013 08:55:51 +0000 (09:55 +0100)
committergwen <gremond@cadoles.com>
Mon, 28 Jan 2013 08:55:51 +0000 (09:55 +0100)
test/test_config_api.py
tiramisu/config.py

index 3695049..fcfd7e9 100644 (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):
index 4bec77e..9e254d9 100644 (file)
@@ -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