[Twisted-Python] Loading pageproducers dynamically

Andrew Bennetts andrew-twisted at puzzling.org
Tue Jun 24 22:15:01 EDT 2003

On Tue, Jun 24, 2003 at 07:54:34PM +0200, Thomas Weholt wrote:
> I'm porting a project to Twisted where I loaded what was then called
> pageproducer, similar to subclasses of Resource in Twisted. They were in a
> given folder and located, created an instance of at run time and stored in a
> lookup-structure. The files were organized something like this :
> www/index.py
> www/folder1/hello1.py
> www/folder2/subfolder3/hellotoo.py
> [ ... etc ...]
> www/index.py maps to index.html, www/folder1/hello1.py to
> /folder1/hello.html etc. The main point being that the server didn't know
> what classes to import and create before run time. How can I create someting
> similar to this, using Resource's putChild etc and keeping the things
> discussed lately about keeping python modules out of the webtree ??

I think you might you might want to set 'isLeaf = 1' on your resource.  This
will stop Twisted Web calling getChild on that resource, even if there are
more path elements left.  Instead, your resource's .render method will be
called immediately, so resource is then free to deal with the rest of the
path however it likes (it can get the remainder of path looking at
request.postpath, if it cares).

twisted.web.twcgi.CGIScript does this, and it sounds like what you want is
similar in some respects to CGI.  It might be a useful reference.

> I want my users to be able to add pages with ease, not having to edit import
> statements in the main modules, put simple doing something like this in a
> module named helloworld.py, located in www/test/helloworld.py :
> class helloworld(Resource):
>     def render(self, request):
>         return 'hello world'
> and pointing your browser to /test/helloword.py should then show "Hello
> world".

I'm not sure that having these inherit from twisted.web.resource.Resource
really makes sense; the tree of the filesystem doesn't really offer the same
features as a tree of Resources.  That's just a guess though -- I'm actually
not a Twisted Web expert (I just play one on mailing lists ;).

> Most examples discussed lately has imported all needed modules explicitly at
> start up. My setup needs to be more dynamic.

I hope I've helped.


More information about the Twisted-Python mailing list