[Zope3-checkins] CVS: Zope3/src/zope/app/index - __init__.py:1.3
Anthony Baxter
anthony at interlink.com.au
Tue Aug 5 05:33:58 EDT 2003
Update of /cvs-repository/Zope3/src/zope/app/index
In directory cvs.zope.org:/tmp/cvs-serv24050/app/index
Modified Files:
__init__.py
Log Message:
Refactoring of the Catalog<->Index interface to extract common code.
TextIndexes now index a particular interface+field combination, which
defaults to ISearchableText/getSearchText. Making a new index hook into
the catalog is now a matter of two or three lines of python and some
zcml.
=== Zope3/src/zope/app/index/__init__.py 1.2 => 1.3 ===
--- Zope3/src/zope/app/index/__init__.py:1.2 Wed Dec 25 09:12:54 2002
+++ Zope3/src/zope/app/index/__init__.py Tue Aug 5 04:33:24 2003
@@ -1,2 +1,68 @@
#
# This file is necessary to make this directory a package.
+
+from zope.interface import implements
+from zope.app.interfaces.index import IInterfaceIndexer
+from zope.app.interfaces.event import ISubscriber
+from zope.component import queryAdapter
+from zope.context import ContextMethod
+
+from zope.app.interfaces.services.hub import \
+ IRegistrationHubEvent, \
+ IObjectRegisteredHubEvent, \
+ IObjectUnregisteredHubEvent, \
+ IObjectModifiedHubEvent
+
+
+class InterfaceIndexingSubscriber(object):
+ """Mixin for indexing a particular field name, after first adapting the
+ object to be indexed to an interface.
+ """
+ implements(IInterfaceIndexer, ISubscriber)
+ default_field_name = None
+ default_interface = None
+
+ def __init__(self, field_name=None, interface=None):
+ super(InterfaceIndexingSubscriber, self).__init__()
+ if field_name is None and self.default_field_name is None:
+ raise ValueError, "Must pass a field_name"
+ if field_name is None:
+ self._field_name = self.default_field_name
+ else:
+ self._field_name = field_name
+ if interface is None:
+ self._interface = self.default_interface
+ else:
+ self._interface = interface
+
+ field_name = property(lambda self: self._field_name)
+ interface = property(lambda self: self._interface)
+
+ def _getValue(self, object):
+ if self._interface is not None:
+ object = queryAdapter(object, self._interface)
+ if object is None: return None
+
+ value = getattr(object, self._field_name, None)
+ if value is None: return None
+
+ if callable(value):
+ try: value = value()
+ except: return None
+
+ return value
+
+ def notify(self, event):
+ """An event occurred. Index or unindex the object in response."""
+ if (IObjectRegisteredHubEvent.isImplementedBy(event) or
+ IObjectModifiedHubEvent.isImplementedBy(event)):
+ value = self._getValue(event.object)
+ if value is not None:
+ self.index_doc(event.hubid, value)
+ elif IObjectUnregisteredHubEvent.isImplementedBy(event):
+ try:
+ self.unindex_doc(event.hubid)
+ except KeyError:
+ pass
+ notify = ContextMethod(notify)
+
More information about the Zope3-Checkins
mailing list