src/pyams_utils/container.py
changeset 72 9049384a2bd4
parent 56 01de65ad00fb
child 195 65b6d930b42b
equal deleted inserted replaced
71:01d01045a2b7 72:9049384a2bd4
    26 from pyramid.threadlocal import get_current_registry
    26 from pyramid.threadlocal import get_current_registry
    27 from zope.container.ordered import OrderedContainer
    27 from zope.container.ordered import OrderedContainer
    28 
    28 
    29 
    29 
    30 class BTreeOrderedContainer(OrderedContainer):
    30 class BTreeOrderedContainer(OrderedContainer):
    31     """BTree based ordered container"""
    31     """BTree based ordered container
       
    32 
       
    33     This container maintain a manual order of it's contents
       
    34     """
    32 
    35 
    33     def __init__(self):
    36     def __init__(self):
    34         self._data = OOBTree()
    37         self._data = OOBTree()
    35         self._order = PersistentList()
    38         self._order = PersistentList()
    36 
    39 
    37 
    40 
    38 @adapter_config(context=IContained, provides=ISublocations)
    41 @adapter_config(context=IContained, provides=ISublocations)
    39 class ContainerSublocationsAdapter(ContextAdapter):
    42 class ContainerSublocationsAdapter(ContextAdapter):
    40     """Container sub-locations adapter"""
    43     """Contained object sub-locations adapter
       
    44 
       
    45     This adapter checks for custom ISublocations interface adapters which can
       
    46     be defined by any component to get access to inner locations, defined for
       
    47     example via annotations.
       
    48     """
    41 
    49 
    42     def sublocations(self):
    50     def sublocations(self):
       
    51         """See `zope.location.interfaces.ISublocations` interface"""
    43         context = self.context
    52         context = self.context
    44         # Check for adapted sub-locations first...
    53         # Check for adapted sub-locations first...
    45         registry = get_current_registry()
    54         registry = get_current_registry()
    46         for name, adapter in registry.getAdapters((context,), ISublocations):
    55         for name, adapter in registry.getAdapters((context,), ISublocations):
    47             if not name:  # don't reuse default adapter!!
    56             if not name:  # don't reuse default adapter!!
    59 
    68 
    60     The condition is a Python callable object that takes an object as
    69     The condition is a Python callable object that takes an object as
    61     argument and must return a boolean result.
    70     argument and must return a boolean result.
    62 
    71 
    63     All sub-objects of the root will also be searched recursively.
    72     All sub-objects of the root will also be searched recursively.
       
    73 
       
    74     :param object root: the parent object from which search is started
       
    75     :param callable condition: a callable object which may return true for a given
       
    76         object to be selected
       
    77     :param boolean ignore_root: if *True*, the root object will not be returned, even if it matches
       
    78         the given condition
       
    79     :return: an iterator for all root's sub-objects matching condition
    64     """
    80     """
    65     if (not ignore_root) and condition(root):
    81     if (not ignore_root) and condition(root):
    66         yield root
    82         yield root
    67     locations = ISublocations(root, None)
    83     locations = ISublocations(root, None)
    68     if locations is not None:
    84     if locations is not None:
    75 
    91 
    76 def find_objects_providing(root, interface):
    92 def find_objects_providing(root, interface):
    77     """Find all objects in root that provide the specified interface
    93     """Find all objects in root that provide the specified interface
    78 
    94 
    79     All sub-objects of the root will also be searched recursively.
    95     All sub-objects of the root will also be searched recursively.
       
    96 
       
    97     :param object root: object; the parent object from which search is started
       
    98     :param Interface interface: interface; an interface that sub-objects should provide
       
    99     :return: an iterator for all root's sub-objects that provide the given interface
    80     """
   100     """
    81     for match in find_objects_matching(root, interface.providedBy):
   101     for match in find_objects_matching(root, interface.providedBy):
    82         yield match
   102         yield match