[Zope-CVS] CVS: Products/Event - EventRegistry.py:1.1

Martijn Pieters mj@zope.com
Thu, 19 Sep 2002 20:03:42 -0400


Update of /cvs-repository/Products/Event
In directory cvs.zope.org:/tmp/cvs-serv26579

Added Files:
	EventRegistry.py 
Log Message:
This time round, actually add the new files.


=== Added File Products/Event/EventRegistry.py ===
""" Global registry for available event interfaces

Intended usage:

    Various generic Event processors may want to be able to list possible
    events, such as for listing them for selection in a UI.

    - Event interfaces should be registered with this registry through the
      registerEvent call, passing in both the interface and a title, and
      optionally a definition. If no definition is given, the Interface
      docstring will be used instead.

    - TODO: fill in listEvents interface.

$Id: EventRegistry.py,v 1.1 2002/09/20 00:03:41 mj Exp $
"""
from zLOG import LOG, WARNING

_registry = {}


class EventRegistry:
    """Registry for Event Interfaces.
    
    Modules make Event interfaces available for listing by registering them
    here.

    """

    def listEvents(self, with_metadata=0):
        """List the registered event interfaces.
        
        - If 'with_metadata', then return a sequence of (interface, title,
          description) tuples.

        - Otherwise, return a sequence of keys.

        """
        if not with_metadata:
            return _registry.keys()

        return [(k, v['title'], v['description']) for k, v in _registry.items()]

    def getEvent(self, key):
        """Look up the event information.

        Returns a dict with title, description and interface keys.

        """
        return _registry.get(key)

    def getEventTitle(self, key):
        """Look up the title for a given event key."""
        data = _registry.get(key)
        return data and data['title']

    def getEventDescription(self, key):
        """Look up the description for a given event key."""
        data = _registry.get(key)
        return data and data['description']

    def getEventInterface(self, key):
        """Look up the event interface for a given event key."""
        data = _registry.get(key)
        return data and data['interface']

    def registerEvent(self, key, interface, title, description=None):
        """Register an event interface.

        'key'           unique key for this event (preferably a dotted path)
        'interface'     the event interface to register
        'title'         the title of the interface
        'description'   documentation for the event. If not given, the docstring
                        of the event interface is used instead.
        """
        if _registry.has_key( key ):
            LOG('EventRegistry', WARNING,
                'Duplicate registration for event: %s' % key)
            return

        if description is None:
            description = interface.__doc__

        _registry[key] = {'interface': interface, 'title': title,
                          'description': description}

eventRegistry = EventRegistry()