[Zope3-dev] a new zcml directive?

Marius Gedminas mgedmin at b4net.lt
Fri Mar 10 19:49:19 EST 2006


On Fri, Mar 10, 2006 at 04:19:44PM +0100, Martijn Faassen wrote:
> Hi there,
> 
> I notice a pattern in code that uses annotations that looks like this:
> 
> class Foo(Persistent, Contained):
>     implements(interfaces.IFoo)
> 
> def getFoo(context):
>     annotations = IAnnotations(context)
>     try:
>         return annotations[FOO_KEY]
>     except KeyError:
>         foo = Foo()
>         annotations[FOO_KEY] = foo
>         # to please security...
>         zope.app.container.contained.contained(
>             foo, context, 'foo')
>         return foo
> 
> # Convention to make adapter introspectable
> getFoo.factory = Foo
> 
> If I'm doing this quite a bit, this looks like something that would be 
> better expressed in a... new ZCML directive (..waiting for the crowd to 
> start flinging stones).
> For instance, one that looks like this:
> 
> <zope:annotation for="IBar" factory="Foo" />
>
> where the factory actually points to whatever creates the real 
> annotation (such as the Foo class in this case), not to the boilerplate 
> to attach it to the proper object. The boilerplate would go away from my 
> code, you could use the facility too without thought, and we'd all be 
> happy. :)

-1

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" />

I think this is exactly the same as Jeff Shell's suggestion, but its
3am, and I'm too tired to read his entire message.

Marius Gedminas
-- 
We have an advanced scalable groupware communication environment (email)
	-- Alan Cox
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-dev/attachments/20060311/59f58701/attachment-0001.bin


More information about the Zope3-dev mailing list