[mod_python] psp without apache

Wouter van Marle wouter at squirrel-systems.com
Thu Jun 1 12:06:25 EDT 2006

On Thu, 2006-06-01 at 11:56 -0400, Jim Gallacher wrote:
> Wouter van Marle wrote:
> > Hi all,
> > 
> > I'd like to use the psp templating module outside of the apache context:
> > this as I'm trying to create an off-line version of my website. But I
> > get errors when attempting to import psp. Instead of publishing the
> > resulting html code in apache, I want to save them to disk as static
> > html files.
> > 
> > But when importing psp on the command line I get an error:
> Don't use psp directly. The really interesting stuff happens in the _psp 
> parser module. Stripping away the mod_python specific code, PSP 
> basically boils down to the following:
> from mod_python import _psp

Ah... stupid me... I import psp but don't even use it in the code :)
Copies a line too many from my web server scripts. My solution is as
follows. It allows me to give a psp file with variables and get back a
string with the page in html.

def psp2html(filename, variables={}):

    # Read the psp file in filename, run it through the parser and
    # the interpretor, and return the html code as result.
    directory, fname = os.path.split(filename)
    directory += "/"

    # invoke the parser
    pycode = _psp.parse(fname, directory).splitlines()
    pycode = [s.rstrip() for s in pycode]
    code = ""
    for s in pycode:
        code += s.replace(",0)", ")")+"\n"
    variables["req"] = StringIO.StringIO()
    exec code in variables
    return variables["req"].getvalue()

More information about the Mod_python mailing list