[Zope3-Users] Re: Can interfaces be mutated cleanly?

Philipp von Weitershausen philipp at weitershausen.de
Fri Apr 13 05:05:47 EDT 2007


Christian Theune wrote:
> I didn't find anything in the documentation or google on this:
> 
> I want to define an interface like:
> 
> class IMySchema(zope.interface.Interface):
>    pass
> 
> Later, I want to use ore.alchemist which currently is able to take a
> SQLAlchemy table description and turn it into a new interface/schema.
> 
> I want to extend this code so that I can pass it an existing interface
> to modify it (update) with the schema definition.
> 
> I need this so that the interface is:
> 
> a) present in the code when reading it
> 
> b) available for ZCML configuration (it's a problem for us right now
> because the database/SQLAlchemy stuff is setup up a little bit later
> after executing a couple of ZCML actions)
> 
> While looking into it, we found that interfaces do not offer a public
> way to modify the attributes they have, and modifying the internal data
> structures would be both evil and cumbersome.
> 
> Any hints?

I would recommend creating a *sub*interface of the IMySchema on-the-fly. 
That way registrations for IMySchema will also apply to the 
subinterface, but you won't have to worry about mutating IMySchema.

This is easily possible:

   IMySchemaSub = InterfaceClass('IMySchemaSub', (IMySchema,),
                                 {'field1': TextLine(), ...},
                                 __doc__='...', __module__='...')

Note that if you attach the interface on persistent objects using 
alsoProvides/directlyProvides, you need to put the interface in a module 
so the ZODB can continue to import it when waking up these objects. Put 
it in the same module that you gave the InterfaceClass constructor 
(__module__).


-- 
http://worldcookery.com -- Professional Zope documentation and training



More information about the Zope3-users mailing list