Yes, this is interesting, thank you for sharing this. I had done some things with xml schemas as zope schemas a while back and had also seen other work in this area.


Jim Washington wrote:
Thierry Florac wrote:

I'm looking for a way to handle what I've called "dynamic interfaces".
My problem is quite simple : I have an application where an
administrator can define "content types" ; for each content type, a set
of common predefined attributes are used, but the administrator can
define for each content type a set of custom attributes, each attribute
being defined by a name, a datatype (text, textline, date, boolean...)
and eventually a list of references values (for choices) ; definition of
these custom attributes is persistent and stored in the ZODB.
When creating data, contributor can select the data type, according to
which the matching set of attributes will be available in forms.

The main point is probably that I'd like to use z3c.form package "as
usual", and so integrate this attributes management into Zope3
interfaces machinery... ; I suppose I should need a king of "interface
factory", but couldn't find any link about that.

Thanks for any help,

  Thierry Florac
Hi, Thierry

I was experimenting with dynamic interfaces a while back, and this is where I ended up.

Theoretically, Interfaces are just python class objects, not importantly different than any others.

You just need to create a python class dynamically:

import new
from zope.interface import Interface

dyn_iface = new.classobj(name,(Interface,),definitions)


name is a string, the name of your interface class
Interface is zope.interface.Interface or a descendent
definitions is a dict of class members {'__doc__':docstring,'name':Schema,'name2':Schema2}


if your class could be defined in an interfaces file as

class IMyClass(Interface):
   """IMyClass interface"""
   yourName=TextLine(title=u'Your name',required=True)

you can dynamically create it like this:

import new
from zope.interface import Interface
import zope.schema

definitions = {}
# __doc__ needs to be there; empty string is fine
definitions['__doc__'] = 'IMyClass Interface'
definitions['yourName'] = zope.schema.TextLine(title=u'Your name',required=True)


myiface = new.classobj(iface_name.encode('utf-8'),(Interface,),definitions)

At this point, myiface should be usable as if generated in an interfaces file.

According to the python docs, you need to be careful when using "new", so appropriate caution is advised.


- Jim Washington

Zope3-users mailing list

Zope3-users mailing list

Reply via email to