[Checkins] SVN: grok/trunk/ add value index component
Jan-Wijbrand Kolman
janwijbrand at gmail.com
Mon Mar 21 08:08:26 EDT 2011
Log message for revision 121053:
add value index component
Changed:
U grok/trunk/CHANGES.txt
A grok/trunk/src/grok/ftests/catalog/indexes_valueindex.py
U grok/trunk/src/grok/index.py
-=-
Modified: grok/trunk/CHANGES.txt
===================================================================
--- grok/trunk/CHANGES.txt 2011-03-20 12:55:16 UTC (rev 121052)
+++ grok/trunk/CHANGES.txt 2011-03-21 12:08:25 UTC (rev 121053)
@@ -4,9 +4,8 @@
1.6 (unreleased)
================
-- Nothing changed yet.
+- Added grok.index.Value component.
-
1.5 (2011-02-14)
================
Added: grok/trunk/src/grok/ftests/catalog/indexes_valueindex.py
===================================================================
--- grok/trunk/src/grok/ftests/catalog/indexes_valueindex.py (rev 0)
+++ grok/trunk/src/grok/ftests/catalog/indexes_valueindex.py 2011-03-21 12:08:25 UTC (rev 121053)
@@ -0,0 +1,73 @@
+"""
+We now demonstrate the use of a ValueIndex with Grok::
+
+Let's set up a site in which we manage a couple of objects::
+
+ >>> herd = Herd()
+ >>> getRootFolder()['herd'] = herd
+ >>> from zope.site.hooks import setSite
+ >>> setSite(herd)
+
+Now we add some indexable objects to the site::
+
+ >>> herd['alpha'] = SabreTooth('Alpha', 'tolerant')
+ >>> herd['beta'] = SabreTooth('Beta', 'narrowminded')
+ >>> herd['gamma'] = SabreTooth('Gamma', 'friendly')
+
+Let's query the set index::
+
+ >>> from zope.catalog.interfaces import ICatalog
+ >>> from zope.component import getUtility, queryUtility
+ >>> catalog = getUtility(ICatalog)
+ >>> def sortedResults(catalog, **kw):
+ ... result = list(catalog.searchResults(**kw))
+ ... result.sort(key=lambda x:x.name)
+ ... return [item.name for item in result]
+ >>> sortedResults(catalog, feature={'any_of': ['tolerant', 'friendly']})
+ ['Alpha', 'Gamma']
+
+ >>> sortedResults(catalog, feature={'any_of': ['narrowminded', 'foo']})
+ ['Beta']
+
+ >>> sortedResults(catalog, feature={'any_of': ['narrowminded', 'friendly']})
+ ['Beta', 'Gamma']
+
+Nuke the catalog and intids in the end, so as not to confuse
+other tests::
+
+ >>> sm = herd.getSiteManager()
+ >>> from zope.catalog.interfaces import ICatalog
+ >>> sm.unregisterUtility(catalog, provided=ICatalog)
+ True
+ >>> from zope.intid.interfaces import IIntIds
+ >>> from zope import component
+ >>> intids = component.getUtility(IIntIds)
+ >>> sm.unregisterUtility(intids, provided=IIntIds)
+ True
+
+Unfortunately ftests don't have good isolation from each other yet.
+"""
+
+from zope.interface import Interface, Attribute
+
+import grok
+from grok import index
+
+class Herd(grok.Container, grok.Application):
+ pass
+
+class ISabreTooth(Interface):
+ feature = Attribute('Feature')
+
+class SabreToothIndexes(grok.Indexes):
+ grok.site(Herd)
+ grok.context(ISabreTooth)
+
+ feature = index.Value()
+
+class SabreTooth(grok.Model):
+ grok.implements(ISabreTooth)
+
+ def __init__(self, name, features):
+ self.name = name
+ self.feature = features
Modified: grok/trunk/src/grok/index.py
===================================================================
--- grok/trunk/src/grok/index.py 2011-03-20 12:55:16 UTC (rev 121052)
+++ grok/trunk/src/grok/index.py 2011-03-21 12:08:25 UTC (rev 121053)
@@ -20,7 +20,7 @@
from zope.catalog.field import FieldIndex
from zope.catalog.text import TextIndex
-from zc.catalog.catalogindex import SetIndex
+from zc.catalog.catalogindex import SetIndex, ValueIndex
from martian.error import GrokError, GrokImportError
from martian.util import frame_is_class
@@ -107,3 +107,14 @@
class Set(IndexDefinition):
"""A :class:`grok.Indexes` index supporting keyword searches of a field."""
index_class = SetIndex
+
+
+class Value(IndexDefinition):
+ """A :class:`grok.Indexes` index similar to, but more flexible than
+ :class:`grok.Field` index.
+
+ The index allows searches for documents that contain any of a set of
+ values; between a set of values; any (non-None) values; and any empty
+ values.
+ """
+ index_class = ValueIndex
More information about the checkins
mailing list