[Zope-dev] improving the utility and adapter lookup APIs

Chris McDonough chrism at plope.com
Wed Nov 25 12:09:39 EST 2009


Chris McDonough wrote:
> There might should be more obvious APIs for just *retrieving* an adapter based 
> on a set of interfaces; it's useful to be able to retrieve an adapter without 
> invoking it.  Currently this is possible via registry.adapters.loookup, which 
> is fine.
> 
> And I know it's heresy, but sometimes I register something as an "adapter" that 
> is not callable with the number of arguments I'm adapting it with.  Sometimes 
> its convenient to register something that gets adapted using a number of 
> arguments that doesn't match the adaptation arguments.
> 
> If some set of ZCA APIs made it the responsibility of the *caller* to invoke 
> the adapter with arguments would go a long way between normalizing the 
> difference between utilities and adapters (because they would essentially then 
> be the same thing).

I realize this might be too abstract.  Let me provide an example.

Zope views accept (context, request) in their arglist:

   class AView(object):
       def __init__(self, context, request):
           pass

This makes sense for Zope, because in a Zope system, the context is almost 
always "important".  It usually represents persistent data, or at least the 
"subject of the view".

But in other systems, you might want to "adapt" on a context but not require 
people to put it in the argument list of the adapter, e.g. if you want to 
support a request-only calling convention for the adapter ala:

   def view(request):
       pass

...  and have the user be able to get at the context via request.context.
The only way to do this currently is something like the following:

   provides = map(providedBy, (context, request))
   view_callable = registry.adapters.lookup(
                      provides, IView, name=view_name, default=None)
   request.context = context
   response = view_callable(request)

This is actually fine by me, it works great, but it's not very obvious what's 
happening to the casual reader.

getMultiAdapter should probably be have been named callMultiAdapter, and 
getMultiAdapter should have just returned the thing.  Too late for that now, 
but it helps explain my original comment.

- C



More information about the Zope-Dev mailing list