[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/observable/ Changed the observer framework to use handlers rather than

Jim Fulton jim at zope.com
Wed May 26 05:26:20 EDT 2004


Log message for revision 24982:
Changed the observer framework to use handlers rather than
subscribers.



-=-
Modified: Zope3/trunk/src/zope/app/observable/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/observable/interfaces.py	2004-05-26 09:22:25 UTC (rev 24981)
+++ Zope3/trunk/src/zope/app/observable/interfaces.py	2004-05-26 09:26:20 UTC (rev 24982)
@@ -23,13 +23,15 @@
     def notify(event):
         """Notify the ISubscriber subscribers"""
 
-    def subscribe(required, provided, subscriber):
-        """Subscribe to an event for a particular instance.
+    def handle(interfaces, handler):
+        """Register a handler for some interfaces
 
-
-        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.
+        The handler will be called with object's implementing the
+        interface.  Typically, one of the objects will be an
+        event. Other objects may be associated objects.
         
         """
+
+    def unhandle(interfaces, handler):
+        """Unregister a handler for some interfaces
+        """

Modified: Zope3/trunk/src/zope/app/observable/observable.py
===================================================================
--- Zope3/trunk/src/zope/app/observable/observable.py	2004-05-26 09:22:25 UTC (rev 24981)
+++ Zope3/trunk/src/zope/app/observable/observable.py	2004-05-26 09:26:20 UTC (rev 24982)
@@ -19,7 +19,6 @@
 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'
 
@@ -30,22 +29,22 @@
     def __init__(self, context):
         self.context = context
 
-    def subscribe(self, required, provided, subscriber):
+    def handle(self, required, subscriber):
         annotations = IAnnotations(self.context)
         registry = annotations.get(key)
         
         if registry is None:
             annotations[key] = registry = Observers()
 
-        registry.subscribe(required, provided, subscriber)
+        registry.subscribe(required, None, subscriber)
 
-    def unsubscribe(self, required, provided, subscriber):
+    def unhandle(self, required, subscriber):
         annotations = IAnnotations(self.context)
         registry = annotations.get(key)
 
         if registry is not None:
             # if there is no registry, we can't unsubscribe
-            registry.unsubscribe(required, provided, subscriber)
+            registry.unsubscribe(required, None, subscriber)
 
     def notify(self, event):
         annotations = IAnnotations(self.context)
@@ -53,5 +52,5 @@
 
         if registry is not None:
             for subscriber in registry.subscriptions([providedBy(event)],
-                                                     ISubscriber):
-                subscriber(event).notify(event)
+                                                     None):
+                subscriber(event)

Modified: Zope3/trunk/src/zope/app/observable/observers.py
===================================================================
--- Zope3/trunk/src/zope/app/observable/observers.py	2004-05-26 09:22:25 UTC (rev 24981)
+++ Zope3/trunk/src/zope/app/observable/observers.py	2004-05-26 09:26:20 UTC (rev 24982)
@@ -109,7 +109,7 @@
 
 
 from persistent import Persistent
-from zope.interface.adapter import Default
+from zope.interface.adapter import Default, Null
 from zope.interface.adapter import Surrogate, AdapterRegistry
 
 class LocalSurrogate(Surrogate):
@@ -165,6 +165,9 @@
         if len(required) == 0:
             raise ValueError("required can not be zero length")
 
+        if provided is None:
+            provided = Null
+
         akey = required[0]
         if akey is None:
             akey = Default
@@ -178,6 +181,9 @@
         AdapterRegistry.__init__(self)
 
     def unsubscribe(self, required, provided, subscriber):
+        if provided is None:
+            provided = Null
+
         akey = required[0]
         if akey is None:
             akey = Default

Modified: Zope3/trunk/src/zope/app/observable/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/observable/tests.py	2004-05-26 09:22:25 UTC (rev 24981)
+++ Zope3/trunk/src/zope/app/observable/tests.py	2004-05-26 09:26:20 UTC (rev 24982)
@@ -22,7 +22,6 @@
 from zope.interface import implements
 from zope.app.observable.observable import ObservableAdapter, key
 from zope.app.observable.interfaces import IObservable
-from zope.app.event.interfaces import ISubscriber
 from zope.app.annotation.interfaces import IAnnotations
 from zope.app.container.interfaces import IObjectAddedEvent
 from zope.app.container.interfaces import IObjectRemovedEvent
@@ -31,32 +30,24 @@
 class DummyAnnotationsClass(dict):
     implements(IAnnotations)
 
-class DummySubscriber:
 
-    implements(ISubscriber)
-
-    events = []
-
-    def __init__(self, event):
-        self.event = event
-        
-    def notify(self, event):
-        self.events.append(self.event)
-
 class DummyEvent:
     implements(IObjectAddedEvent)
     
-def test_subscribe():
+def test_handle():
     """
     First create an annotatable object and an adapter
 
       >>> obj = DummyAnnotationsClass()
       >>> adapter = ObservableAdapter(obj)
 
-    Make a subscriber and make a faux subscription
+    Make a handler and make a faux subscription
 
-      >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, DummySubscriber)
+      >>> def handler(event):
+      ...     pass
 
+      >>> adapter.handle([IObjectAddedEvent], handler)
+
     Make sure an ObjectAdapterRegistry was created
 
       >>> obj[key] is not None
@@ -69,23 +60,24 @@
 
     """
 
-def test_unsubscribe():
+def test_unhandle():
     """
     First create an annotatable object and an adapter
 
       >>> obj = DummyAnnotationsClass()
       >>> adapter = ObservableAdapter(obj)
 
-    Make a subscriber and make a faux subscription
+    Make a handler and make a faux subscription
 
-      >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, DummySubscriber)
+      >>> def handler(event):
+      ...     pass
+      >>> adapter.handle([IObjectAddedEvent], handler)
 
-    Now unsubscribe from the registry
+    Now unhandle from the registry
 
-      >>> adapter.unsubscribe([IObjectAddedEvent], ISubscriber,
-      ...                     DummySubscriber)
+      >>> adapter.unhandle([IObjectAddedEvent], handler)
 
-    There should be no subscribers for IObjectAddedEvent after unsubscription.
+    There should be no handlers for IObjectAddedEvent after unsubscription.
 
       >>> obj[key].adapters[IObjectAddedEvent]
       {}
@@ -98,9 +90,12 @@
       >>> obj = DummyAnnotationsClass()
       >>> adapter = ObservableAdapter(obj)
 
-    Make a subscriber and make a faux subscription
+    Make a handler and make a faux subscription
 
-      >>> adapter.subscribe([IObjectAddedEvent], ISubscriber, DummySubscriber)
+      >>> events = []
+      >>> def handler(event):
+      ...     events.append(event)
+      >>> adapter.handle([IObjectAddedEvent], handler)
 
     Make sure an ObjectAdapterRegistry was created
 
@@ -111,7 +106,7 @@
 
       >>> event = DummyEvent()
       >>> adapter.notify(event)
-      >>> DummySubscriber.events == [event]
+      >>> events == [event]
       True
 
     """
@@ -148,7 +143,7 @@
 def testObservableEvents(self):
     """
     When an object that has subscriptions change, the
-    subscribers are notified.
+    handlers are notified.
 
     >>> event = DummyObservableEvent()
     >>> notifier = observerevent.ObserverEventNotifier()




More information about the Zope3-Checkins mailing list