[Grok-dev] Catalog Utility

Sebastian Ware sebastian at urbantalk.se
Thu Mar 26 02:53:55 EDT 2009


Can this code be used to add indexes to a running application?

Mvh Sebastian

26 mar 2009 kl. 00.47 skrev Ruslan Spivak:

> 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(
>            field_name=name,
>            interface=iface,
>            field_callable=callable
>            )
>
> # 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__"""
>        self._createIntIds(site)
>        catalog = self._createCatalog(site)
>
>        setup_indexes(catalog)
>
>
> # Your application class
> class Sample(grok.Application, grok.Container):
>    pass
>
> @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
>   machinery
>
> For more information about indexes and catalog check out:
>
> http://svn.zope.org/zope.catalog/trunk/src/zope/catalog/
> http://svn.zope.org/zope.index/trunk/src/zope/index/
> http://svn.zope.org/zc.catalog/trunk/src/zc/catalog/
>
> Hope it helps you to move further.
>
> Cheers,
> Ruslan
> --
>
> http://ruslanspivak.com
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev



More information about the Grok-dev mailing list