[Twisted-Python] Yet another coil refactor

Itamar Shtull-Trauring twisted at itamarst.org
Sun Apr 7 13:27:55 EDT 2002

As I said in my previous email - coil will now use components.

What this means:

Collections used by coil are collections of Interfaces objects. That means 
you need an Interface for anything that is addable in a coil collection.

I needed to add about 4 interface classes to the non-coil parts of Twisted 
for coil to work: protocol factory, service, web resource, dns domain. 
That's it. So I don't think this is such a bother.

In fact. it's much cleaner for things like DNS domains where there wasn't a 
base class for all Domains (same goes for mail domains.) Which is after all 
the purpose of interfaces. It does mean extra typing of __implements__ in 
certain cases, but all in all it's more flexible, and webcoil is cleaner.

I have this all working now - not I just need to clean up.

Here's an example of the different sort of things you can do now - notice 
how static.Files allow you to configure mime-types and resources.

class SiteConfigurator(coil.Configurator):
     """Configurator for web sites."""

     __implements__ = [coil.IConfigurator, coil.IStaticCollection]

     configurableClass = server.Site
     configTypes = {'resource': [resource.IResource, "Resource",
                    "The resource at the site's root."] }
     configName = 'HTTP Web Site'

     def listStaticEntities(self):
         return [['resource', self.instance.resource]]

     def getStaticEntity(self, name):
         if name == 'resource':
             return self.instance.resource

components.registerAdapter(SiteConfigurator, server.Site, coil.ICollection)

# ... deleted from example ...

class MimeTypeCollection(coil.ConfigCollection):

     entityType = types.StringType

class ResourceCollection:
     """Wrap an existing Resource as a ConfigCollection."""

     __implements__ = [coil.IConfigCollection]

     def __init__(self, resource):
         self._resource = resource

     def __getattr__(self, attr):
         return getattr(self._resource, attr)

class StaticConfigurator(coil.Configurator, coil.StaticCollection):

     __implements__ = [coil.IConfigurator, coil.IStaticCollection]

     configurableClass = static.File

     configTypes = {'path': [types.StringType, "Path", "The path in the 
filesystem to be served."],
          'execCGI': ['boolean', "Execute CGIs", "Support running CGI"],
          'execEPY': ['boolean', "Execute EPYs", "Support running EPY"],
          'defaultType': [types.StringType, "Default MIME Type",""]

     configName = 'Web Filesystem Access'

     def __init__(self, instance):
         coil.Configurator.__init__(self, instance)
         self.putEntity("Mime-types", \
         self.putEntity("Resources", ResourceCollection(self.instance))

     # ... deleted, the config interfce is unchanged from CVS ...

def staticFactory(container, name):
     return static.File("somewhere/outthere")

coil.registerConfigurator(StaticConfigurator, staticFactory)
components.registerAdapter(StaticConfigurator, static.File,coil.ICollection)

More information about the Twisted-Python mailing list