[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/observable/ Changed the observable framework to use adapters.

Jim Fulton jim at zope.com
Fri May 21 12:15:48 EDT 2004


Log message for revision 24851:
Changed the observable framework to use adapters.
(Rather than just subscribers.)




-=-
Modified: Zope3/trunk/src/zope/app/observable/README.txt
===================================================================
--- Zope3/trunk/src/zope/app/observable/README.txt	2004-05-21 15:37:46 UTC (rev 24850)
+++ Zope3/trunk/src/zope/app/observable/README.txt	2004-05-21 16:15:47 UTC (rev 24851)
@@ -1,24 +1,27 @@
-$Id$
+Instance Based Event Subscription Support
+=========================================
 
-Instance and Based Event Subscription Support
-
-Package Description
-===================
-
 This package implements support for subscribing to events for a
 particular instance of an object.  This package
 implements the proposal found at
 http://dev.zope.org/Zope3/InstanceAndTypeBasedSubscriptions .
 
 The package provides an event channel for dispatching events to the
-appropriate instance as well as an adapter from IObservable to
-IAnnotatable.  This is important because an object must support
+appropriate instance as well as an adapter from IAnnotatable to
+IObservable.  This is important because an object must support
 IAnnotatable (and therefore IAnnotations) in order to support
-instance-based subscriptions. 
+instance-based subscriptions.
 
 Subscriptions for a particular instance are stored in the instance's
 annotations, in a key defined in zope.app.observable.observable.key
 (currently 'zope.app.observable').  The annotation stored in the key
-is actually an Observers object, which is local registry that is not
+is actually an Observers object, which is a local registry that is not
 aware of the global registry in any way.  More information on the
 Observers object is available in observers.txt.
+
+
+
+
+
+
+$Id$

Modified: Zope3/trunk/src/zope/app/observable/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/observable/interfaces.py	2004-05-21 15:37:46 UTC (rev 24850)
+++ Zope3/trunk/src/zope/app/observable/interfaces.py	2004-05-21 16:15:47 UTC (rev 24851)
@@ -20,8 +20,16 @@
 
 class IObservable(Interface):
 
-    def notify(event, provided):
-        """Notify all subscribers that the event provided has occured."""
+    def notify(event):
+        """Notify the ISubscriber subscribers"""
 
     def subscribe(required, provided, subscriber):
-        """Subscribe to an event for a particular instance."""
+        """Subscribe to an event for a particular instance.
+
+
+        Note that the providd interface should be or extend ISubscriber.
+
+        In the future, we will provide a subscribers function that
+        allows other interfaces to be used.
+        
+        """

Modified: Zope3/trunk/src/zope/app/observable/observable.py
===================================================================
--- Zope3/trunk/src/zope/app/observable/observable.py	2004-05-21 15:37:46 UTC (rev 24850)
+++ Zope3/trunk/src/zope/app/observable/observable.py	2004-05-21 16:15:47 UTC (rev 24851)
@@ -19,6 +19,7 @@
 from zope.app.observable.interfaces import IObservable
 from zope.app.annotation.interfaces import IAnnotations
 from zope.app.observable.observers import Observers
+from zope.app.event.interfaces import ISubscriber
 
 key = 'zope.app.observable'
 
@@ -46,11 +47,11 @@
             # if there is no registry, we can't unsubscribe
             registry.unsubscribe(required, provided, subscriber)
 
-    def notify(self, event, provided):
+    def notify(self, event):
         annotations = IAnnotations(self.context)
         registry = annotations.get(key)
 
         if registry is not None:
             for subscriber in registry.subscriptions([providedBy(event)],
-                                                     provided):
-                subscriber.notify(event)
+                                                     ISubscriber):
+                subscriber(event).notify(event)

Modified: Zope3/trunk/src/zope/app/observable/observerevent.py
===================================================================
--- Zope3/trunk/src/zope/app/observable/observerevent.py	2004-05-21 15:37:46 UTC (rev 24850)
+++ Zope3/trunk/src/zope/app/observable/observerevent.py	2004-05-21 16:15:47 UTC (rev 24851)
@@ -28,7 +28,7 @@
         adapter = IObservable(event.object, None)
 
         if adapter is not None:
-            adapter.notify(event, ISubscriber)
+            adapter.notify(event)
 
 observerEventNotifierInstance = ObserverEventNotifier()
 

Modified: Zope3/trunk/src/zope/app/observable/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/observable/tests.py	2004-05-21 15:37:46 UTC (rev 24850)
+++ Zope3/trunk/src/zope/app/observable/tests.py	2004-05-21 16:15:47 UTC (rev 24851)
@@ -35,73 +35,85 @@
 
     implements(ISubscriber)
 
-    def __init__(self):
-        self.events = []
+    events = []
+
+    def __init__(self, event):
+        self.event = event
         
     def notify(self, event):
+        self.events.append(self.event)
 
-        self.events.append(event)
-
 class DummyEvent:
     implements(IObjectAddedEvent)
     
 def test_subscribe():
     """
     First create an annotatable object and an adapter
-    >>> obj = DummyAnnotationsClass()
-    >>> adapter = ObservableAdapter(obj)
 
+      >>> obj = DummyAnnotationsClass()
+      >>> adapter = ObservableAdapter(obj)
+
     Make a subscriber and make a faux subscription
-    >>> subscriber = DummySubscriber()
-    >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, subscriber)
 
+      >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, DummySubscriber)
+
     Make sure an ObjectAdapterRegistry was created
-    >>> obj[key] is not None
-    True
 
+      >>> obj[key] is not None
+      True
+
     Make sure the registry contains a subscription for the correct event
-    >>> IObjectAddedEvent in obj[key].adapters
-    True
 
+      >>> IObjectAddedEvent in obj[key].adapters
+      True
+
     """
 
 def test_unsubscribe():
     """
     First create an annotatable object and an adapter
-    >>> obj = DummyAnnotationsClass()
-    >>> adapter = ObservableAdapter(obj)
 
+      >>> obj = DummyAnnotationsClass()
+      >>> adapter = ObservableAdapter(obj)
+
     Make a subscriber and make a faux subscription
-    >>> subscriber = DummySubscriber()
-    >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, subscriber)
 
+      >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, DummySubscriber)
+
     Now unsubscribe from the registry
-    >>> adapter.unsubscribe([IObjectAddedEvent], ISubscriber, subscriber)
 
+      >>> adapter.unsubscribe([IObjectAddedEvent], ISubscriber,
+      ...                     DummySubscriber)
+
     There should be no subscribers for IObjectAddedEvent after unsubscription.
-    >>> obj[key].adapters[IObjectAddedEvent]
-    {}
+
+      >>> obj[key].adapters[IObjectAddedEvent]
+      {}
     """
 
 def test_notify():
     """
     First create an annotatable object and an adapter
-    >>> obj = DummyAnnotationsClass()
-    >>> adapter = ObservableAdapter(obj)
 
+      >>> obj = DummyAnnotationsClass()
+      >>> adapter = ObservableAdapter(obj)
+
     Make a subscriber and make a faux subscription
-    >>> subscriber = DummySubscriber()
-    >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, subscriber)
 
+      >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, DummySubscriber)
+
     Make sure an ObjectAdapterRegistry was created
-    >>> obj[key] is not None
-    True
 
+      >>> obj[key] is not None
+      True
+
     Call notify
-    >>> event = DummyEvent()
-    >>> adapter.notify(event, ISubscriber)
-    >>> subscriber.events == [event]
-    True
+
+      >>> event = DummyEvent()
+      >>> adapter.notify(event)
+      >>> DummySubscriber.events == [event]
+      True
+
     """
 
 class DummyObservable:
@@ -110,7 +122,7 @@
     def __init__(self):
         self.flag = False
 
-    def notify(self, event, provided):
+    def notify(self, event):
         self.flag = True
 
 class DummyNotObservable:
@@ -118,7 +130,7 @@
     def __init__(self):
         self.flag = False
         
-    def notify(self, event, provided):
+    def notify(self, event):
         self.flag = True
 
 class DummyObservableEvent:




More information about the Zope3-Checkins mailing list