src/pyams_utils/context.py
changeset 380 c062ab4db6cd
parent 292 b338586588ad
child 408 cf2304af0fab
equal deleted inserted replaced
379:07fe5bb08599 380:c062ab4db6cd
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
       
    13 """PyAMS_utils.context module
       
    14 
       
    15 This module provides a "context" selector which can be used as Pyramid's subscriber
       
    16 predicate. Matching argument can be a class or an interface: for subscriber to be actually called,
       
    17 subscriber's argument should inherit from it (if it's a class) or implement it (if it's an
       
    18 interface).
       
    19 """
       
    20 
    13 __docformat__ = 'restructuredtext'
    21 __docformat__ = 'restructuredtext'
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 
       
    20 # import packages
       
    21 
    22 
    22 
    23 
    23 class ContextSelector(object):
    24 class ContextSelector(object):
    24     """Interface based context selector
    25     """Interface based context selector
    25 
    26 
    26     This selector can be used as a subscriber predicate to define
    27     This selector can be used as a predicate to define a class or an interface that the context
    27     an interface that the context must support for the event to be applied::
    28     must inherit from or implement for the subscriber to be called::
    28 
    29 
    29     .. code-block:: python
    30     .. code-block:: python
    30 
    31 
       
    32         from zope.lifecycleevent.interfaces import IObjectModifiedEvent
    31         from pyams_utils.interfaces.site import ISiteRoot
    33         from pyams_utils.interfaces.site import ISiteRoot
    32 
    34 
    33         @subscriber(IObjectModifiedEvent, context_selector=ISiteRoot)
    35         @subscriber(IObjectModifiedEvent, context_selector=ISiteRoot)
    34         def siteroot_modified_event_handler(event):
    36         def siteroot_modified_event_handler(event):
    35             '''This is an event handler for an ISiteRoot object modification event'''
    37             '''This is an event handler for an ISiteRoot object modification event'''
    39         if not isinstance(ifaces, (list, tuple, set)):
    41         if not isinstance(ifaces, (list, tuple, set)):
    40             ifaces = (ifaces,)
    42             ifaces = (ifaces,)
    41         self.interfaces = ifaces
    43         self.interfaces = ifaces
    42 
    44 
    43     def text(self):
    45     def text(self):
       
    46         """Return selector """
    44         return 'context_selector = %s' % str(self.interfaces)
    47         return 'context_selector = %s' % str(self.interfaces)
    45 
    48 
    46     phash = text
    49     phash = text
    47 
    50 
    48     def __call__(self, event):
    51     def __call__(self, event):