[Zope-CMF] Re: Tools as local utilities
Martin Aspeli
optilude at gmx.net
Mon Nov 13 10:08:07 EST 2006
> I am experimenting with that right now, but my z3/Five-Fu ran low
> again ;) My problem: calls to zope.component.getUtility
> (interface_class) never return anything. Here's the top part (the
> bottom is just the old way) of my CMFCore.utils.getToolByName:
>
Yay!
> def getToolByName(obj, name, default=_marker):
>
> """ Get the tool, 'toolname', by acquiring it.
> """
> tool_iface = _tool_iface_registry.get(name)
>
> if tool_iface is not None:
> warn('getToolByName is deprecated, please use "getUtility(%
> s)"' % (
> tool_iface.__name__), DeprecationWarning,
> stacklevel=2)
>
> try:
> tool = getUtility(tool_iface, context=obj)
> return tool
> except ComponentLookupError:
> # behave in backwards-compatible way
> if default is _marker:
> raise AttributeError, name
>
The 'context' argument *should* be superfluous, because Zope's thread-local
should know what the local site is, and a utility is registered with exactly
one local site.
> I have a simple registration going and have one ID -> interface
> registered (in "_tool_iface_registry"), 'portal_actions' is mapped to
> interface class CMFCore.interfaces._tools.IActionsTool. No matter how
> I try to call getUtility, I always get a ComponentLookupError.
>
> I am probably missing some kind of component registration - I have
> not made any ZCML changes or changes to the code in
> CMFCore.ActionsTool, except for one line that causes the tool to be
> registered in the internal "_tool_iface_registry".
>
You'll need to actually register the utility with the local component
registry. I assume we're on Zope 2.10 here? This needs to happen wehn the
CMF site is set up.
Take a look at
http://svn.plone.org/svn/plone/CMFPlone/trunk/setuphandlers.py.
In particular:
def enableSite(self, portal):
"""
Make the portal a Zope3 site and create a site manager.
"""
enableSite(portal, iface=IObjectManagerSite)
components = PersistentComponents()
components.__bases__ = (base,)
portal.setSiteManager(components)
After this is done, you can do:
from zope.component import getSiteManager
site = <CMF site>
actionsTool = site.portal_actions # get the actual object from the ZODB
sm = getSiteManager(site)
sm.registerUtility(provides=IActionTool, component=actionsTool)
Now, you can do this with GenericSetup as well, thanks to Hanno. See
http://svn.plone.org/svn/collective/GSLocalAddons/trunk/
The test is informative:
http://svn.plone.org/svn/collective/GSLocalAddons/trunk/tests/test_components.py
Hanno said he was interested in factoring GSLocalAddOns into CMF itself, so
maybe now's a good time to do so.
Martin
--
View this message in context: http://www.nabble.com/Tools-as-local-utilities-tf2245411.html#a7318607
Sent from the Zope - CMF list2 mailing list archive at Nabble.com.
More information about the Zope-CMF
mailing list