[Zope3-checkins] CVS: Zope3/src/zope/event - __init__.py:1.3.2.1 interfaces.py:1.1.2.1

Marius Gedminas marius at pov.lt
Fri Feb 13 14:23:17 EST 2004


Update of /cvs-repository/Zope3/src/zope/event
In directory cvs.zope.org:/tmp/cvs-serv11377/src/zope/event

Added Files:
      Tag: mgedmin-events-branch
	__init__.py interfaces.py 
Log Message:
Initial implementation of zope.event, a simple global event system.



=== Added File Zope3/src/zope/event/__init__.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Global event publication system.

Components that wish to publish events should do the following:

    import zope.event

    event = ...
    zope.event.publish(event)

Components that are interested in events should not subscribe directly to
the event publication system, they should subscribe to specific event channels
instead.  Event channels on the other hand should add themselves to the
subscriber lists like this:

    import zope.event

    class GlobalEventChannel:

        def __call__(self, event):
            ...

    zope.event.subscribe(GlobalEventChannel())


Doctests:

    >>> from zope.interface.verify import verifyObject
    >>> from zope.event.interfaces import IEventPublication
    >>> import zope.event
    >>> verifyObject(IEventPublication, zope.event)
    True

    >>> import zope.event
    >>> events_delivered = []
    >>> zope.event.subscribe(events_delivered.append)
    >>> list(zope.event.iterSubscribers()) == [events_delivered.append]
    True
    >>> event1 = object()
    >>> zope.event.publish(event1)
    >>> events_delivered == [event1]
    True

    >>> from zope.app.tests.placelesssetup import tearDown
    >>> tearDown()
    >>> list(zope.event.iterSubscribers())
    []

$Id: __init__.py,v 1.3.2.1 2004/02/13 19:23:16 mgedmin Exp $
"""

import sets
from zope.interface import moduleProvides
from zope.event.interfaces import IEventPublication
from zope.testing.cleanup import addCleanUp

moduleProvides(IEventPublication)


_subscribers = []


def subscribe(subscriber):
    _subscribers.append(subscriber)


def iterSubscribers():
    return iter(_subscribers)


def publish(event):
    for subscriber in _subscribers:
        subscriber(event)


def _cleanUp():
    del _subscribers[:]

addCleanUp(_cleanUp)



=== Added File Zope3/src/zope/event/interfaces.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Global event publication system."""

from zope.interface import Interface, Attribute


class IEventPublication(Interface):
    """The global event publication system."""

    def subscribe(subscriber):
        """Add subscriber to the list of subscribers.

        A subscriber is a callable that expects a single argument (the
        event that is being published).
        """

    def iterSubscribers():
        """Return an iterator over the list of subscribers."""

    def publish(event):
        """Publish this event to subscribers.

        Event publication is synchronous.  Subscribers are notified in
        the order they were subscribed.  If a subscriber was subscribed
        more than once, it will be notified more than once.  If calling to
        notify a subscriber causes an exception to be raised, no further
        subscribers will be notified.
        """




More information about the Zope3-Checkins mailing list