[Zope3-dev] a new zcml directive?

Martijn Faassen faassen at infrae.com
Fri Mar 10 10:19:44 EST 2006


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. :)

What do people think?

Regards,

Martijn


More information about the Zope3-dev mailing list