[Twisted-Python] Component Architecture in coil

Itamar Shtull-Trauring twisted at itamarst.org
Fri Apr 5 05:06:27 MST 2002


This is all based on Zope3 component architecture:
http://cvs.zope.org/Docs/ZopeComponentArchitecture/PythonProgrammerTutorial/Chapter1/

Specificially the 5th part, but I recommend going through the whole thing.


Quick intro - classes can say they implement interfaces.

  class A:
       __implements__ == (IProducer, IBlarg)

Notice that interfaces are separate from inheritance.

Next, we have adapters. This is a registry of classes, kinda like the 
Configurator registry for coil.

getAdapter(o, IFoo) returns an object that wraps o, so that the wrapped 
object implements interface IFoo. If o already implements it, then o is 
returned, otherwise it is looked up in the registry, wrapped, and an 
object implementing IFoo is returned. (There's a PEP that talks about 
something in this vein - http://www.python.org/peps/pep-0246.html).

Now, on to coil. For coil, we really need two interfaces - 
IConfigurator, for properties, and IConfigCollection. So, where before 
hand we wanted configurable classes to inherit from Collection, now we 
have a much better system.

When we want a configurator we do:

    cfg = getAdapter(o, IConfigurator)

More or less like now. When we want a collection-like interface, we do:

    collection = getAdapter(o, IConfigCollection)

Now, note the benefits. We can write a configuration class that 
implements both IConfigurator and IConfigCollection for our class, and 
then it will be returned in both cases - regardless of what o's class 
inherits from. So, we don't have to worry about what our classes inherit 
from, classes can implement IConfigurator or IConfigCollection 
themselves if they want, etc..

In other words, we have a much more flexible system, and we solved the 
"what if both Configurator and configurable inherit from Collection" 
issue, and in general we got rid of all the horrible special cases in 
webcoil. And this is generally extendable.

I'm sure we will find other uses for this as well - we already are using 
a hard-coded system for this in Configurators, and I'm sure there are 
many more places where this is useful.






More information about the Twisted-Python mailing list