[Zope3-dev] Re: a new zcml directive?
Martijn Faassen
faassen at infrae.com
Wed Mar 15 09:27:03 EST 2006
Jim Fulton wrote:
> Philipp von Weitershausen wrote:
>
>> Marius Gedminas wrote:
>>
>>> I'd prefer
>>>
>>> from zope.annotation.adapter import AnnotationAdapter
>>>
>>> getFoo = AnnotationAdapter(for_=IBar,
>>> interface=IFoo,
>>> factory=Foo,
>>> key=FOO_KEY)
>>> # I suppose the key could be optional; you could use a dotted
>>> # interface name by default
>>>
>>> and then the ordinary
>>>
>>> <zope:adapter factory=".foo.getFoo" />
>>
>>
>>
>> +10
>
> Likewise.
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.
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.
So, I need to come up with trickery so that instances of
AnnotationAdapter return the 'for_' argument when asked for by
zope.interface.implementedBy() and the 'factory' argument when called
with component.adaptedBy(). Since the instances of AnnotationAdapter are
actually not doing any adaptation or implementing any of the interface,
but are actually just providing a factory, it seems like this trickery
would amount to actually lying.
This is hairy enough for me to give up for now.
Unless I'm missing something, I conclude that implementing
AnnotationAdapter is currently at least as difficult as just
implementing a new directive. The other conclusion is that figuring out
what is happening in case something goes wrong is also at least as hard
to understand as the innards of a directive, due to the trickery
necessary to make it work.
Using <zope:adapter..> with an explicit 'for' and 'provides' would avoid
the problem, but this seems to be going against the suggestions given in
this thread.
Regards,
Martijn
More information about the Zope3-dev
mailing list