[Checkins] SVN: grok/trunk/s Add SetIndex support to grok.

Martijn Faassen faassen at infrae.com
Tue Jul 3 13:51:07 EDT 2007


Log message for revision 77371:
  Add SetIndex support to grok.
  

Changed:
  U   grok/trunk/setup.py
  A   grok/trunk/src/grok/ftests/catalog/indexes_set.py
  U   grok/trunk/src/grok/index.py

-=-
Modified: grok/trunk/setup.py
===================================================================
--- grok/trunk/setup.py	2007-07-03 16:54:02 UTC (rev 77370)
+++ grok/trunk/setup.py	2007-07-03 17:51:06 UTC (rev 77371)
@@ -17,5 +17,6 @@
     zip_safe=False,    
     install_requires=['setuptools',
                       'martian',
-                      'simplejson'],
+                      'simplejson',
+                      'zc.catalog == 1.1.1'],
 )

Added: grok/trunk/src/grok/ftests/catalog/indexes_set.py
===================================================================
--- grok/trunk/src/grok/ftests/catalog/indexes_set.py	                        (rev 0)
+++ grok/trunk/src/grok/ftests/catalog/indexes_set.py	2007-07-03 17:51:06 UTC (rev 77371)
@@ -0,0 +1,77 @@
+"""
+We now demonstrate the use of a SetIndex with Grok::
+
+  >>> import grok
+  >>> grok.grok('grok.ftests.catalog.indexes_set')
+
+Let's set up a site in which we manage a couple of objects::
+
+  >>> from grok.ftests.catalog.indexes_set import Herd, Mammoth
+  >>> herd = Herd()
+  >>> getRootFolder()['herd'] = herd
+  >>> from zope.app.component.hooks import setSite
+  >>> setSite(herd)
+
+Now we add some indexable objects to the site::
+
+  >>> herd['alpha'] = Mammoth('Alpha', ['big', 'brown'])
+  >>> herd['beta'] = Mammoth('Beta', ['big', 'black', 'friendly'])
+  >>> herd['gamma'] = Mammoth('Gamma', ['brown', 'friendly', 'gorgeous'])
+
+Let's query the set index::
+
+  >>> from zope.app.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, features={'any_of': ['brown']})
+  ['Alpha', 'Gamma']
+  >>> sortedResults(catalog, features={'any_of': ['big']})
+  ['Alpha', 'Beta']
+  >>> sortedResults(catalog, features={'any_of': ['friendly']})
+  ['Beta', 'Gamma']
+  
+Nuke the catalog and intids in the end, so as not to confuse
+other tests::
+
+  >>> sm = herd.getSiteManager()
+  >>> from zope.app.catalog.interfaces import ICatalog
+  >>> sm.unregisterUtility(catalog, provided=ICatalog)
+  True
+  >>> from zope.app.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
+from zope import schema
+
+import grok
+from grok import index
+
+class Herd(grok.Container, grok.Application):
+    pass
+
+class IMammoth(Interface):
+    features = Attribute('Features')
+    
+class MammothIndexes(grok.Indexes):
+    grok.site(Herd)
+    grok.context(IMammoth)
+
+    features = index.Set()
+
+class Mammoth(grok.Model):
+    grok.implements(IMammoth)
+
+    def __init__(self, name, features):
+        self.name = name
+        self.features = features
+    

Modified: grok/trunk/src/grok/index.py
===================================================================
--- grok/trunk/src/grok/index.py	2007-07-03 16:54:02 UTC (rev 77370)
+++ grok/trunk/src/grok/index.py	2007-07-03 17:51:06 UTC (rev 77371)
@@ -5,6 +5,7 @@
 
 from zope.app.catalog.field import FieldIndex
 from zope.app.catalog.text import TextIndex
+from zc.catalog.catalogindex import SetIndex
 
 from martian.error import GrokError, GrokImportError
 from martian.util import frame_is_class
@@ -54,3 +55,6 @@
 
 class Text(IndexDefinition):
     index_class = TextIndex
+
+class Set(IndexDefinition):
+    index_class = SetIndex



More information about the Checkins mailing list