src/pyams_utils/container.py
changeset 56 01de65ad00fb
parent 36 7398e25bad99
child 72 9049384a2bd4
equal deleted inserted replaced
55:d20591346980 56:01de65ad00fb
    50                 yield location
    50                 yield location
    51         # then yield container items
    51         # then yield container items
    52         if IContainer.providedBy(context):
    52         if IContainer.providedBy(context):
    53             for key in context:
    53             for key in context:
    54                 yield context[key]
    54                 yield context[key]
       
    55 
       
    56 
       
    57 def find_objects_matching(root, condition, ignore_root=False):
       
    58     """Find all objects in root that match the condition
       
    59 
       
    60     The condition is a Python callable object that takes an object as
       
    61     argument and must return a boolean result.
       
    62 
       
    63     All sub-objects of the root will also be searched recursively.
       
    64     """
       
    65     if (not ignore_root) and condition(root):
       
    66         yield root
       
    67     locations = ISublocations(root, None)
       
    68     if locations is not None:
       
    69         for location in locations.sublocations():
       
    70             if condition(location):
       
    71                 yield location
       
    72             for sublocation in find_objects_matching(location, condition, ignore_root=True):
       
    73                 yield sublocation
       
    74 
       
    75 
       
    76 def find_objects_providing(root, interface):
       
    77     """Find all objects in root that provide the specified interface
       
    78 
       
    79     All sub-objects of the root will also be searched recursively.
       
    80     """
       
    81     for match in find_objects_matching(root, interface.providedBy):
       
    82         yield match