src/pyams_utils/container.py
changeset 195 65b6d930b42b
parent 72 9049384a2bd4
child 225 8d7721d95b68
equal deleted inserted replaced
194:9313b3c435eb 195:65b6d930b42b
    15 
    15 
    16 # import standard library
    16 # import standard library
    17 
    17 
    18 # import interfaces
    18 # import interfaces
    19 from zope.container.interfaces import IContainer, IContained
    19 from zope.container.interfaces import IContainer, IContained
       
    20 from zope.lifecycleevent.interfaces import IObjectMovedEvent
    20 from zope.location.interfaces import ISublocations
    21 from zope.location.interfaces import ISublocations
    21 
    22 
    22 # import packages
    23 # import packages
    23 from BTrees.OOBTree import OOBTree
    24 from BTrees.OOBTree import OOBTree
    24 from persistent.list import PersistentList
    25 from persistent.list import PersistentList
    34     """
    35     """
    35 
    36 
    36     def __init__(self):
    37     def __init__(self):
    37         self._data = OOBTree()
    38         self._data = OOBTree()
    38         self._order = PersistentList()
    39         self._order = PersistentList()
       
    40 
       
    41 
       
    42 class ParentSelector(object):
       
    43     """Interface based parent selector
       
    44 
       
    45     This selector can be used as a subscriber predicate on IObjectAddedEvent to define
       
    46     an interface that the new parent must support for the event to be applied::
       
    47 
       
    48     .. code-block:: python
       
    49 
       
    50         from pyams_utils.interfaces.site import ISiteRoot
       
    51 
       
    52         @subscriber(IObjectAddedEvent, parent_selector=ISiteRoot)
       
    53         def siteroot_object_added_event_handler(event):
       
    54             '''This is an event handler for an ISiteRoot object added event'''
       
    55     """
       
    56 
       
    57     def __init__(self, ifaces, config):
       
    58         if not isinstance(ifaces, (list, tuple, set)):
       
    59             ifaces = (ifaces,)
       
    60         self.interfaces = ifaces
       
    61 
       
    62     def text(self):
       
    63         return 'parent_selector = %s' % str(self.interfaces)
       
    64 
       
    65     phash = text
       
    66 
       
    67     def __call__(self, event):
       
    68         if not IObjectMovedEvent.providedBy(event):
       
    69             return False
       
    70         for intf in self.interfaces:
       
    71             try:
       
    72                 if intf.providedBy(event.newParent):
       
    73                     return True
       
    74             except (AttributeError, TypeError):
       
    75                 if isinstance(event.newParent, intf):
       
    76                     return True
       
    77         return False
    39 
    78 
    40 
    79 
    41 @adapter_config(context=IContained, provides=ISublocations)
    80 @adapter_config(context=IContained, provides=ISublocations)
    42 class ContainerSublocationsAdapter(ContextAdapter):
    81 class ContainerSublocationsAdapter(ContextAdapter):
    43     """Contained object sub-locations adapter
    82     """Contained object sub-locations adapter