[Zope3-checkins] CVS: Zope3/src/zope/app/event - globalservice.py:1.2 __init__.py:1.4

Steve Alexander steve@cat-box.net
Mon, 27 Jan 2003 13:16:23 -0500


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

Modified Files:
	globalservice.py __init__.py 
Log Message:
Completed phase 1 of making the event service use queryAdapter rather
than isImplementedBy for getting an ISubscriber to notify and for
getting an ISubscribingAware to make aware of subscriptions.

This is complete for all the interfaces, and for the global event service,
but I still need to write tests and code for the local service.


=== Zope3/src/zope/app/event/globalservice.py 1.1 => 1.2 ===
--- Zope3/src/zope/app/event/globalservice.py:1.1	Mon Dec 30 09:03:02 2002
+++ Zope3/src/zope/app/event/globalservice.py	Mon Jan 27 13:16:20 2003
@@ -20,6 +20,7 @@
 __metaclass__ = type
 
 from zope.interface.type import TypeRegistry
+from zope.component import getAdapter, queryAdapter
 from zope.exceptions import NotFoundError
 from zope.proxy.introspection import removeAllProxies
 
@@ -93,25 +94,26 @@
 
 class GlobalSubscribable:
     """A global mix-in"""
-    
+
     __implements__ = IGlobalSubscribable
 
     def __init__(self):
         self._registry = TypeRegistry()
         self._subscribers = [] # use dict?
-        
+
     _clear = __init__
 
     def globalSubscribe(self, subscriber, event_type=IEvent, filter=None):
         checkEventType(event_type)
         clean_subscriber = removeAllProxies(subscriber)
-        
-        if ISubscribingAware.isImplementedBy(subscriber):
-            subscriber.subscribedTo(self, event_type, filter)
-        
+
+        subscribingaware = queryAdapter(subscriber, ISubscribingAware)
+        if subscribingaware is not None:
+            subscribingaware.subscribedTo(self, event_type, filter)
+
         if event_type is IEvent:
             event_type = None # optimization
-        
+
         subscribers = self._registry.setdefault(event_type, [])
         subscribers.append((clean_subscriber, filter))
 
@@ -124,15 +126,15 @@
                 break
         else:
             self._subscribers.append((clean_subscriber, {event_type: 1}))
-            
+
         # Trigger persistence, if pertinent
         # self._registry = self._registry
-        
-    
+
+
     def unsubscribe(self, subscriber, event_type=None, filter=None):
         checkEventType(event_type, allow_none=True)
         clean_subscriber = removeAllProxies(subscriber)
-        
+
         for subscriber_index in range(len(self._subscribers)):
             sub = self._subscribers[subscriber_index]
             if sub[0] == clean_subscriber:
@@ -145,9 +147,9 @@
                 # this was a generic unsubscribe all request; work may have
                 # been done by a local service
                 return
-        
-        do_alert = ISubscribingAware.isImplementedBy(clean_subscriber)
-        
+
+        subscribingaware = queryAdapter(subscriber, ISubscribingAware)
+
         if event_type:
             ev_type = event_type
             if event_type is IEvent:
@@ -161,12 +163,12 @@
                 subscriptions.remove((clean_subscriber, filter))
             except ValueError:
                 raise NotFoundError(subscriber, event_type, filter)
-            if do_alert:
-                subscriber.unsubscribedFrom(self, event_type, filter)
+            if subscribingaware is not None:
+                subscribingaware.unsubscribedFrom(self, event_type, filter)
             ev_set[ev_type] -= 1
             if ev_set[ev_type] < 1:
-                for sub in subscriptions:
-                    if sub[0] == clean_subscriber:
+                for asubscriber, afilter in subscriptions:
+                    if asubscriber == clean_subscriber:
                         break
                 else:
                     if len(ev_set) > 1:
@@ -180,11 +182,14 @@
                     ev_type = IEvent
                 subs = subscriptions[:]
                 subscriptions[:] = []
-                for sub in subs:
-                    if sub[0] == clean_subscriber: # deleted (not added back)
-                        if do_alert:
-                            subscriber.unsubscribedFrom(self, ev_type, sub[1])
-                    else:  # kept (added back)
+                for asubscriber, afilter in subs:
+                    if asubscriber == clean_subscriber:
+                        # deleted (not added back)
+                        if subscribingaware is not None:
+                            subscribingaware.unsubscribedFrom(
+                                    self, ev_type, afilter)
+                    else:
+                        # kept (added back)
                         subscriptions.append(sub)
             del self._subscribers[subscriber_index]
         # Trigger persistence, if pertinent
@@ -193,7 +198,7 @@
     def listSubscriptions(self, subscriber, event_type=None):
         checkEventType(event_type, allow_none=True)
         subscriber = removeAllProxies(subscriber)
-        
+
         result = []
         if event_type:
             ev_type = event_type
@@ -223,31 +228,29 @@
         return result
 
 
+def globalNotifyOrPublish(self, event):
+    assert IEvent.isImplementedBy(event)
+    subscriptionsForEvent = self._registry.getAllForObject(event)
+    for subscriptions in subscriptionsForEvent:
+        for subscriber, filter in subscriptions:
+            if filter is not None and not filter(event):
+                continue
+            getAdapter(subscriber, ISubscriber).notify(event)
+
+
 class GlobalEventChannel(GlobalSubscribable):
-    
+
     __implements__ = IGlobalSubscribable, ISubscriber
-        
-    def notify(self, event):
-        subscriptionsForEvent = self._registry.getAllForObject(event)
-        for subscriptions in subscriptionsForEvent:
-            for subscriber, filter in subscriptions:
-                if filter is not None and not filter(event):
-                    continue
-                subscriber.notify(event)
+
+    notify = globalNotifyOrPublish
 
 
 class GlobalEventPublisher(GlobalSubscribable):
-    
+
     __implements__ = IGlobalSubscribable, IPublisher
 
-    def publish(self, event):
-        assert IEvent.isImplementedBy(event)
-        subscriptionsForEvent = self._registry.getAllForObject(event)
-        for subscriptions in subscriptionsForEvent:
-            for subscriber, filter in subscriptions:
-                if filter is not None and not filter(event):
-                    continue
-                subscriber.notify(event)
+    publish = globalNotifyOrPublish
+
 
 # Repeated here, and in zope/app/event/__init__.py to avoid circular import.
 def globalSubscribeMany(subscriber, event_types=(IEvent,), filter=None):
@@ -263,4 +266,4 @@
 from zope.testing.cleanup import addCleanUp
 addCleanUp(_clear)
 del addCleanUp
-    
+


=== Zope3/src/zope/app/event/__init__.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/event/__init__.py:1.3	Mon Dec 30 09:03:02 2002
+++ Zope3/src/zope/app/event/__init__.py	Mon Jan 27 13:16:20 2003
@@ -40,4 +40,4 @@
 
 def globalListSubscriptions(subscriber, event_type=None):
     return eventPublisher.listSubscriptions(subscriber, event_type)
-        
+