[Zope3-dev] Re: a new zcml directive?

Martijn Faassen faassen at infrae.com
Wed Mar 15 12:25:46 EST 2006


Jim Fulton wrote:
> Martijn Faassen wrote:
> ...
> 
>> I just tried to write AnnotationAdapter. Unfortunately, I haven't 
>> gotten very far yet, as it turns out I need to understand the 
>> internals of the implementation of the zope:adapter ZCML statement.
> 
> No, you don't.
> 
>> zope:adapter has a bunch of automation to look for the interface of 
>> something and what it adapts on the factory. This works if the factory 
>> is the class itself, but in this case, the factory is a class which 
>> has instances which can provide an instance of the annotation class 
>> when called.
> 
> This may not be documented as well as it should be, but not for lack of
> trying.
> 
> 1. You can always provide the interface provided by an adapter
>    when you register it.

I realize this, but I was trying to implement the example as suggested, 
also because it's clear that the tendency is away from specifying 'for' 
and 'implements' in ZCML. I know I wouldn't have a problem if I use 
'for' and 'implements' in ZCML.

> 2. If a factory declares that it implements a single interface,
>    then you can omit the interface when registering the adapter.
> 
>    If a factory is a class, it typically declares that it implements
>    an interface via the implements call in the class statement.

Well, I found this out by reading the code in zope.app.component.

>    If a factory is not a class, and if it allows attributes to be
>    set on it, then the interface.implementor function can be used to make
>    declarations for it.  This is documeted in zope/interfaces/README.txt
>    and zope/component/README.txt.

This is one bit I was missing, thanks.

Unfortunately I read in zope/interface/README.txt that the 'implementer' 
function cannot be used for classes yet, so this will change the design 
somewhat (I was using __call__, looks like I'll have to exploit lexical 
scoping and generate a function on the fly).

Using implements() on the class seems like lying, as the class is just 
implementing a factory, not the functionality itself. Besides, I'd have 
to change the class each time it gets called (it's using implementedBy 
to check).

Then that leaves convincing 'adaptedBy()'. One hack is to write the 
'for_' argument that's passed to the AnnotationAdapter() constructor 
directly to an attribute called __component_adapts__. I don't think I 
can use 'adapts()' on a lexically scoped function...

I stand by my conclusions on this approach sounding simple in theory, 
but still being a bit harder than it should be in practice. :)

Regards,

Martijn


More information about the Zope3-dev mailing list