[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/index/text - index.py:1.2

Guido van Rossum guido@python.org
Wed, 4 Dec 2002 09:22:59 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/index/text
In directory cvs.zope.org:/tmp/cvs-serv7938

Modified Files:
	index.py 
Log Message:
Add subscribe and unsubscribe; refactored; added unit test for these.

=== Zope3/lib/python/Zope/App/index/text/index.py 1.1 => 1.2 ===
--- Zope3/lib/python/Zope/App/index/text/index.py:1.1	Wed Dec  4 06:10:24 2002
+++ Zope3/lib/python/Zope/App/index/text/index.py	Wed Dec  4 09:22:58 2002
@@ -21,7 +21,10 @@
 
 from Zope.Event.ISubscriber import ISubscriber
 
+from Zope.ComponentArchitecture import getService
+
 from Zope.App.OFS.Services.ObjectHub.IHubEvent import \
+     IRegistrationHubEvent, \
      IObjectRegisteredHubEvent, \
      IObjectUnregisteredHubEvent, \
      IObjectModifiedHubEvent
@@ -41,16 +44,46 @@
         """An event occurred.  Index or unindex the object in response."""
         if (IObjectRegisteredHubEvent.isImplementedBy(event) or
             IObjectModifiedHubEvent.isImplementedBy(event)):
-            adapted = queryAdapter(event.object,
-                                   ISearchableText,
-                                   context=wrapped_self)
-            if adapted is None:
-                return
-            texts = adapted.getSearchableText()
-            wrapped_self.index_doc(event.hubid, texts)
+            texts = wrapped_self._getTexts(event.object)
+            if texts is not None:
+                wrapped_self.index_doc(event.hubid, texts)
         elif IObjectUnregisteredHubEvent.isImplementedBy(event):
             try:
                 wrapped_self.unindex_doc(event.hubid)
             except KeyError:
                 pass
     notify = ContextMethod(notify)
+
+    def _getTexts(wrapped_self, object):
+        adapted = queryAdapter(object, ISearchableText, context=wrapped_self)
+        if adapted is None:
+            return None
+        return adapted.getSearchableText()
+    _getTexts = ContextMethod(_getTexts)
+
+    def subscribe(wrapped_self, channel=None, update=1):
+        if channel is None:
+            channel = wrapped_self._getChannel()
+        channel.subscribe(wrapped_self, IRegistrationHubEvent)
+        channel.subscribe(wrapped_self, IObjectModifiedHubEvent)
+        if update:
+            wrapped_self._update(channel.iterObjectRegistrations()) 
+    subscribe = ContextMethod(subscribe)
+
+    def unsubscribe(wrapped_self, channel=None):
+        if channel is None:
+            channel = wrapped_self._getChannel()
+        channel.unsubscribe(wrapped_self, IObjectModifiedHubEvent)
+        channel.unsubscribe(wrapped_self, IRegistrationHubEvent)
+    unsubscribe = ContextMethod(unsubscribe)
+
+    def _getChannel(wrapped_self):
+        return getService(wrapped_self, "ObjectHubService")
+    _getChannel = ContextMethod(_getChannel)
+
+    def _update(wrapped_self, registrations):
+        for location, hubid, wrapped_object in registrations:
+            texts = wrapped_self._getTexts(wrapped_object)
+            if texts is not None:
+                wrapped_self.index_doc(hubid, texts)
+    _update = ContextMethod(_update)