[mod_python] Import a module into a variable value

Graham Dumpleton grahamd at dscpl.com.au
Sat Dec 2 18:39:52 EST 2006


On 02/12/2006, at 9:26 PM, Clodoaldo wrote:

> I'm doing a very light templating in a publisher program.

What features are you after that have made you use publisher in the
first place? Ie., which of the following is it:

1. Mapping of URL to different handler files.

2. Mapping of additional path information to objects within a handler  
file.

3. Mapping of form arguments to function arguments.

I ask as depending on what of the above you are wanting, in  
mod_python 3.3
there are possibly other ways of doing it which may be more  
attractive and
also possibly give you more flexibility.

> There is a html template read from the index.html file in which
> '%(variable)s' are replaced.
>
> This code reads the template from the file:
>
> f = open('/var/www/html/carconsumption.com/index.html', 'r')
> _html = ''.join(f.readlines())
> f.close()
>
> And then inside index() at return time the usual substitution:
>
>   return C._html % _d
>
> Editing a file with the extension .html is nice with editors with html
> syntax highlighting, completion and other things. The only problem
> with this approach is that the file index.html is not reloaded when
> changed.
>
> I know i could just use a generic handler with PSP as templating. But
> the publisher is so convenient and as the new importer in 3.3 is full
> of tricks like importing modules with any extension i would like to
> know if there is some way to import a whole module into a variable
> value preventing it from being interpreted as python code.
>
> If the above is nonsense what would be a better approach for
> templating within publisher programs or just to solve the not
> reloadable index.html file?

As Jorey pointed out, the new module importer only handles Python  
code files
and not arbitrary types of files.

One could use your own caching system. An example of one is:

   http://svn.dscpl.com.au/vampire/trunk/software/vampire/markup.py

This one ends up turning the page into a template object for a  
particular
templating sytsem, but that could be stripped out, leaving it to only  
cache
the raw page source.

Another page caching system is actually in the mod_python source at
present and was added to implement the interim module importer in 3.2  
used
by publisher. This will be removed from mod_python in a future  
version though,
thus shouldn't be used. You could though go get the original code for  
it and
use it in your own code. This is at:

   http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302997

Graham


More information about the Mod_python mailing list