[Zope3-checkins] SVN: Zope3/branches/jim-index/src/zope/app/index/
Checking in some partial refactoring of indxes to not use events.
Jim Fulton
jim at zope.com
Fri Jun 11 11:26:23 EDT 2004
Log message for revision 25354:
Checking in some partial refactoring of indxes to not use events.
-=-
Modified: Zope3/branches/jim-index/src/zope/app/index/__init__.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/index/__init__.py 2004-06-11 15:22:11 UTC (rev 25353)
+++ Zope3/branches/jim-index/src/zope/app/index/__init__.py 2004-06-11 15:26:22 UTC (rev 25354)
@@ -1,77 +1 @@
-##############################################################################
#
-# Copyright (c) 2003 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""
-$Id$
-"""
-from zope.interface import implements
-from zope.app.index.interfaces import IInterfaceIndexer
-from zope.app.event.interfaces import ISubscriber
-
-from zope.app.hub.interfaces import \
- 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 = self._interface(object, None)
- 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.providedBy(event) or
- IObjectModifiedHubEvent.providedBy(event)):
- value = self._getValue(event.object)
- if value is not None:
- self.index_doc(event.hubid, value)
- elif IObjectUnregisteredHubEvent.providedBy(event):
- try:
- self.unindex_doc(event.hubid)
- except KeyError:
- pass
-
Modified: Zope3/branches/jim-index/src/zope/app/index/field/index.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/index/field/index.py 2004-06-11 15:22:11 UTC (rev 25353)
+++ Zope3/branches/jim-index/src/zope/app/index/field/index.py 2004-06-11 15:26:22 UTC (rev 25354)
@@ -29,7 +29,7 @@
from zope.app.hub.interfaces import IRegistrationHubEvent
from zope.app.index.interfaces.field import IUIFieldIndex, IUIFieldCatalogIndex
from zope.app.catalog.interfaces.index import ICatalogIndex
-from zope.app.index import InterfaceIndexingSubscriber
+from zope.app.index.ifaceindex import InterfaceIndexingSubscriber
class FieldCatalogIndex(InterfaceIndexingSubscriber, FieldIndexWrapper,
Contained):
@@ -39,36 +39,3 @@
implements(IUIFieldIndex)
- currentlySubscribed = False # Default subscription state
-
- def subscribe(self, channel=None, update=True):
- if self.currentlySubscribed:
- raise RuntimeError, "already subscribed; please unsubscribe first"
- channel = self._getChannel(channel)
- channel.subscribe(self, IRegistrationHubEvent)
- channel.subscribe(self, IObjectModifiedHubEvent)
- if update:
- self._update(channel.iterObjectRegistrations())
- self.currentlySubscribed = True
-
- def unsubscribe(self, channel=None):
- if not self.currentlySubscribed:
- raise RuntimeError, "not subscribed; please subscribe first"
- channel = self._getChannel(channel)
- channel.unsubscribe(self, IObjectModifiedHubEvent)
- channel.unsubscribe(self, IRegistrationHubEvent)
- self.currentlySubscribed = False
-
- def isSubscribed(self):
- return self.currentlySubscribed
-
- def _getChannel(self, channel):
- if channel is None:
- channel = getService(self, HubIds)
- return channel
-
- def _update(self, registrations):
- for location, hubid, wrapped_object in registrations:
- value = self._getValue(wrapped_object)
- if value is not None:
- self.index_doc(hubid, value)
Copied: Zope3/branches/jim-index/src/zope/app/index/ifaceindex.py (from rev 24947, Zope3/trunk/src/zope/app/index/__init__.py)
===================================================================
--- Zope3/trunk/src/zope/app/index/__init__.py 2004-05-24 20:24:21 UTC (rev 24947)
+++ Zope3/branches/jim-index/src/zope/app/index/ifaceindex.py 2004-06-11 15:26:22 UTC (rev 25354)
@@ -0,0 +1,137 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+from zope.interface import implements
+from zope.app.index.interfaces import IInterfaceIndexer
+
+class InterfaceIndexingSubscriber(object):
+ """Index interface-defined fields
+
+ Mixin for indexing a particular field name, after first adapting the
+ object to be indexed to an interface.
+
+ The class is meant to be mixed with a base class that defines an
+ index_doc method:
+
+ >>> class BaseIndex(object):
+ ... def __init__(self):
+ ... self.data = []
+ ... def index_doc(self, id, value):
+ ... self.data.append((id, value))
+
+ The class does two things. The first is to get a named field
+ from an object:
+
+ >>> class Data:
+ ... def __init__(self, v):
+ ... self.x = v
+
+ >>> class Index(InterfaceIndexingSubscriber, BaseIndex):
+ ... pass
+
+ >>> index = Index('x')
+ >>> index.index_doc(11, Data(1))
+ >>> index.index_doc(22, Data(2))
+ >>> index.data
+ [(11, 1), (22, 2)]
+
+ A method can be indexed:
+
+ >>> Data.z = lambda self: self.x + 20
+ >>> index = Index('z')
+ >>> index.index_doc(11, Data(1))
+ >>> index.index_doc(22, Data(2))
+ >>> index.data
+ [(11, 21), (22, 22)]
+
+ The class can also adapt an object to an interface:
+
+ >>> from zope.interface import Interface
+ >>> class I(Interface):
+ ... pass
+
+ >>> class Data:
+ ... def __init__(self, v):
+ ... self.x = v
+ ... def __conform__(self, iface):
+ ... if iface is I:
+ ... return Data2(self.x)
+
+ >>> class Data2:
+ ... def __init__(self, v):
+ ... self.y = v*v
+
+ >>> index = Index('y', I)
+ >>> index.index_doc(11, Data(3))
+ >>> index.index_doc(22, Data(2))
+ >>> index.data
+ [(11, 9), (22, 4)]
+
+ When you define an index class, you can define a default
+ interface and/or a default interface:
+
+ >>> class Index(InterfaceIndexingSubscriber, BaseIndex):
+ ... default_interface = I
+ ... default_field_name = 'y'
+
+ >>> index = Index()
+ >>> index.index_doc(11, Data(3))
+ >>> index.index_doc(22, Data(2))
+ >>> index.data
+ [(11, 9), (22, 4)]
+
+ """
+ implements(IInterfaceIndexer)
+ 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 = self._interface(object, None)
+ 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 index_doc(self, docid, object):
+ value = self._getValue(object)
+ return super(InterfaceIndexingSubscriber, self).index_doc(docid, value)
Added: Zope3/branches/jim-index/src/zope/app/index/tests/test_ifaceindex.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/index/tests/test_ifaceindex.py 2004-06-11 15:22:11 UTC (rev 25353)
+++ Zope3/branches/jim-index/src/zope/app/index/tests/test_ifaceindex.py 2004-06-11 15:26:22 UTC (rev 25354)
@@ -0,0 +1,28 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Test interface indexers
+
+$Id$
+"""
+import unittest
+from zope.testing.doctestunit import DocTestSuite
+
+def test_suite():
+ return unittest.TestSuite((
+ DocTestSuite('zope.app.index.ifaceindex'),
+ ))
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
+
Modified: Zope3/branches/jim-index/src/zope/app/index/tests/test_objectretrievingprocessor.py
===================================================================
--- Zope3/branches/jim-index/src/zope/app/index/tests/test_objectretrievingprocessor.py 2004-06-11 15:22:11 UTC (rev 25353)
+++ Zope3/branches/jim-index/src/zope/app/index/tests/test_objectretrievingprocessor.py 2004-06-11 15:26:22 UTC (rev 25354)
@@ -57,7 +57,8 @@
def test_IVerify(self):
verifyObject(IRankedObjectRecord, RankedObjectRecord(None, None))
- verifyObject(IRankedObjectIterator, RankedObjectIterator([], None, 0, 20, 4))
+ verifyObject(IRankedObjectIterator,
+ RankedObjectIterator([], None, 0, 20, 4))
verifyObject(IQueryProcessor, ObjectRetrievingProcessor())
def test_RankedObjectRecord(self):
More information about the Zope3-Checkins
mailing list