src/pyams_utils/container.py
changeset 56 01de65ad00fb
parent 36 7398e25bad99
child 72 9049384a2bd4
--- a/src/pyams_utils/container.py	Fri Jan 29 15:39:09 2016 +0100
+++ b/src/pyams_utils/container.py	Mon Apr 11 17:37:23 2016 +0200
@@ -52,3 +52,31 @@
         if IContainer.providedBy(context):
             for key in context:
                 yield context[key]
+
+
+def find_objects_matching(root, condition, ignore_root=False):
+    """Find all objects in root that match the condition
+
+    The condition is a Python callable object that takes an object as
+    argument and must return a boolean result.
+
+    All sub-objects of the root will also be searched recursively.
+    """
+    if (not ignore_root) and condition(root):
+        yield root
+    locations = ISublocations(root, None)
+    if locations is not None:
+        for location in locations.sublocations():
+            if condition(location):
+                yield location
+            for sublocation in find_objects_matching(location, condition, ignore_root=True):
+                yield sublocation
+
+
+def find_objects_providing(root, interface):
+    """Find all objects in root that provide the specified interface
+
+    All sub-objects of the root will also be searched recursively.
+    """
+    for match in find_objects_matching(root, interface.providedBy):
+        yield match