[Grok-dev] Catalog Utility

Ruslan Spivak ruslan.spivak at gmail.com
Wed Mar 25 19:47:36 EDT 2009

Vanderson Mota dos Santos <vanderson.mota at gmail.com> writes:

> Hi,
> I'm developing with some friends a "real world" grok application, and by
> now, i've used the hurry.query, according with the site documentation.
> I would like to know if is there any implementation of catalog like plone's
> portal_catalog, because i don't want to create a new grok.Indexes class just
> to index some attribute in a diferent model/context.
> If such thing doesn't exists, can someone give an opnion about building it
> or if is there a better option instead of making a centralized catalog
> utility?

I whipped out some code that you can use a basis for registering indexes
in one place:

import grok

from grok.meta import IndexesSetupSubscriber
from zope.app.catalog.field import FieldIndex
from zope.app.catalog.text import TextIndex
from zc.catalog.catalogindex import SetIndex

# Your interfaces
from zope.dublincore.interfaces import IZopeDublinCore
from zope.index.text.interfaces import ISearchableText

def setup_indexes(catalog):
    """Custom indexes to populate catalog"""

    # index_name, index_class,
    # attr_name, attr_interface, attr_is_callable
    indexes = (
        ('title', TextIndex,
         'title', None, False),

        ('created', FieldIndex,
         'created', IZopeDublinCore, False),

        ('searchable', TextIndex,
         'getSearchableText', ISearchableText, True),

    for (index_name, index_class,
         name, iface, callable) in indexes:

        catalog[index_name] = index_class(

# Hack Grok's IndexesSetupSubscriber
class CustomIndexesSetupSubscriber(IndexesSetupSubscriber):

    def __init__(self):
        """Override base class constructor."""
        self.catalog_name = u''

    def __call__(self, site, event):
        """Override base class's __call__"""
        catalog = self._createCatalog(site)


# Your application class
class Sample(grok.Application, grok.Container):

@grok.subscribe(Sample, grok.IObjectAddedEvent)
def registerCatalogIndexes(app, event):
    subscriber = CustomIndexesSetupSubscriber()
    # call it to set up catalog and indexes
    subscriber(app, event)

Main steps:
1) subscriber handler for your site's IObjectAddedEvent

2) use Grok's IndexesSetupSubscriber with overridden
   __init__ and __call__ and hook your index creation
   routine (as done above).

3) add indexes to catalog without using Grok's Indexes

For more information about indexes and catalog check out:


Hope it helps you to move further.



More information about the Grok-dev mailing list