src/pyams_utils/container.py
changeset 444 72ceed22e639
parent 408 cf2304af0fab
--- a/src/pyams_utils/container.py	Mon Sep 28 15:49:40 2020 +0200
+++ b/src/pyams_utils/container.py	Fri Nov 13 12:51:19 2020 +0100
@@ -105,7 +105,7 @@
             yield from context.values()
 
 
-def find_objects_matching(root, condition, ignore_root=False):
+def find_objects_matching(root, condition, ignore_root=False, with_depth=False, initial_depth=0):
     """Find all objects in root that match the condition
 
     The condition is a Python callable object that takes an object as
@@ -118,25 +118,31 @@
         object to be selected
     :param boolean ignore_root: if *True*, the root object will not be returned, even if it matches
         the given condition
+    :param boolean with_depth: if *True*, results are tuples which include items depth
+    :param int initial_depth: initial depth, when depth is required
     :return: an iterator for all root's sub-objects matching condition
     """
     if (not ignore_root) and condition(root):
-        yield root
+        yield (root, initial_depth) if with_depth else root
     locations = ISublocations(root, None)
     if locations is not None:
         for location in locations.sublocations():  # pylint: disable=too-many-function-args
             if condition(location):
-                yield location
-            yield from find_objects_matching(location, condition, ignore_root=True)
+                yield (location, initial_depth+1) if with_depth else location
+            yield from find_objects_matching(location, condition,
+                                             ignore_root=True,
+                                             with_depth=with_depth,
+                                             initial_depth=initial_depth+1)
 
 
-def find_objects_providing(root, interface):
+def find_objects_providing(root, interface, with_depth=False):
     """Find all objects in root that provide the specified interface
 
     All sub-objects of the root will also be searched recursively.
 
     :param object root: object; the parent object from which search is started
     :param Interface interface: interface; an interface that sub-objects should provide
+    :param boolean with_depth: if *True*, results include
     :return: an iterator for all root's sub-objects that provide the given interface
     """
-    yield from find_objects_matching(root, interface.providedBy)
+    yield from find_objects_matching(root, interface.providedBy, with_depth=with_depth)