[mod_python] handling virtual files

Graham Dumpleton grahamd at dscpl.com.au
Sat Jul 16 20:55:37 EDT 2005


On 17/07/2005, at 12:07 AM, Alejandro Mery wrote:

>> What base handler were you thinking of using, were you thinking
>> of starting with something like mod_python.publisher or writing
>> your own handlers from scratch?
>
> my own PythonHandler-s from scratch. I want to write handlers for the  
> different kind of folders and for the different kind of file, but  
> keeping file handlers also usable as direct handlers. for example a  
> handler to render dir/foo.xmi as png and dir/foo.xmi?height=200 with  
> that fixed height and scaling. or html from .tex files, with  
> foo.txt?pdf=yes&papersize=letter options even if files are real or  
> not.
> i mean, to be called by folder handler or directly by apache.

If your virtual file is notionally adjacent to an actual physical file,
the quickest way to get something working may be to use Vampire. This
is because Vampire was designed to make this specific sort of thing
very easy.

In Vampire it does dispatching to a Python module associated with a
physical file and can then within that Python module select a handler
based on the extension to the request.

Consider the ".tex" case first. Here you would have "foo.tex" present
and next to it you would create a "foo.py" Python module. In it you  
could
have:

   def handler_html(req):
     # render req.filename as HTML

   def handler_pdf(req,papersize="letter"):
     # render req.filename as PDF

You could have a customised Python module associated with each ".tex"  
file
or, because the process could be the same for all ".tex" files in a  
directory,
you could specify a default handler for ".html" or ".pdf" which is  
triggered
for any matching request against the directory and it would process the  
".tex"
file appropriately.

For an actual example, see:

   http://svn.dscpl.com.au/vampire/trunk/examples/reportlab/
   http://svn.dscpl.com.au/vampire/trunk/examples/reportlab/_handler.py

In this case rather than a ".tex" file as input, it is a ".rml" file
with output being ".pdf".

For another example of this idea of multiple views of a physical
resource, see:

    
http://svn.dscpl.com.au/vampire/trunk/examples/handler/complex_views.py
    
http://svn.dscpl.com.au/vampire/trunk/examples/handler/ 
complex_views.csv

Here there is a physical ".csv" file, but also virtual resource  
handlers in
the associated Python module for returning the data as HTML or tab  
separated
data.

Note how the Python module even has a handler for a ".csv" request so  
that
some checking can be done, with req.sendfile() being used to return the
actual ".csv" data.

You could do something similar for your ".xmi" files, although, it may  
make
sense to have a handler for ".png" which transforms ".xmi" file to PNG  
and
sends the response. Ie., use the appropriate extension type for the top
of content being returned.

> i have nothing serious written or designed yet, if you think i need  
> WSGI lets go there :)

Based on how you describe what you want, you might try Vampire first.  
More
complicated things can still be done with Vampire, but depends on how  
you
want that to work.

BTW, Vampire is available from:

   http://www.dscpl.com.au/projects/vampire

This is not the volatile package I talked of, that is something  
different I
am working on. Vampire is stable, with only known issue being an obscure
thing related to module reloading which you unlikely to run up against  
quickly.

There are various examples included with the source code, including all  
the
above.

Graham



More information about the Mod_python mailing list