[Zope-CMF] Re: small proposal, catalog tool

whit whit at burningman.com
Sun Aug 28 19:58:54 EDT 2005


tackling this problem a little bit ago, I found it helpful to create 
adapters that returned values to index rather than gronky objects (see 
the first attachment. undoubtably there could be discussion about 
whether this is useful or just lazy, but I think it could promote code 
reuse and less pointlessly named "adapter" vars cluttering the namespace).

This let me use a simple closure and loop to register adapters for 
indexing using plones ExtensibleObjectWrapper:

from Products.CMFPlone.CatalogTool import registerIndexableAttribute

def registerIndexer(idx, iface):
     default = None

     def indexfx(obj, portal, **kw):
         try:
             return iface(obj)
         except TypeError:
             return default

     registerIndexableAttribute(idx, indexfx)

iface_idxs = (('index1', ISomeCalledInterface),
               ('index2', ISomeOtherCalledInterface))

for name, iface in iface_idxs:
     registerIndexer(name, iface)


Getting back to pete's points, I think another tool would be overkill, 
but something like registerIndexer might work as a directive that could 
be associated to content ala:

<registerIndexer
     for=".interfaces.IMyContent"
     provides=".interfaces.ISomeCalledInterface"
     factory=".SomeCallabelAdapter"
     permission="zope2.Public"
/>


-w


-------------- next part --------------
##############################################################################
#
# Copyright (c) 2004, 2005 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (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.
#
##############################################################################
"""
An interface that returns a value if adapter or directly provided object is callable
"""
from zope.interface.interface import InterfaceClass, adapter_hooks
from zope.interface.interfaces import IInterface
from zope.interface.declarations import classImplements

class CalledInterfaceClass(InterfaceClass):

    def _callOrReturn(self, obj):
        if callable(obj):
            return obj()
        else:
            return obj
        
    def __adapt__(self, obj):
        if self.providedBy(obj):
            return self._callOrReturn(obj)

        for hook in adapter_hooks:
            adapter = hook(self, obj)
            if adapter is not None:
                return self._callOrReturn(adapter)
            
classImplements(InterfaceClass, IInterface)

CalledInterface = CalledInterfaceClass("CalledInterface", __module__ = 'Products.calledinterface')

__all__=('CalledInterface')


More information about the Zope-CMF mailing list