[mod_python] feature request: psp templating returns html code to calling script, not to browser.

Wouter van Marle wouter at squirrel-systems.com
Thu May 19 01:27:36 EDT 2005


Regarding my feature request below, I've hacked together a small piece
of code to retrieve the raw python source, and the html result of that
source. This code is heavily inspired by the PSP class of psp.py script
from mod_python.
It works for me, does not support forms and sessions (it is meant to
produce html formatted e-mail bodies), does not support Windows (path
names there use backslashes), and maybe more limitations.

Maybe the developers are interested in adding (something like) this to
the psp.py file?

get_source produces the python source code, get_html compiles the psp
input file to html code.

Wouter.



from mod_python import _psp

import os
import sys

def get_source(filename):
    dir, fname = os.path.split(filename)
    dir += "/"
    pycode = _psp.parse(fname, dir).splitlines()
    pycode = [s.rstrip() for s in pycode]
    for line in pycode:
        left = line.replace("\t", " "*4)
        result += left+"\n"
    return result

def get_html(filename, vars={}):
    code = get_source(filename)
    lines = code.splitlines()
    code = 'html = ""\n'
    for l in lines:
        l = l.replace("req.write(", "html +=").replace(",0)", "")
        code += l+"\n"
    global_scope = globals().copy()
    global_scope.update(vars)
    html = ""
    l = locals()
    try:
        exec code in global_scope, l
    except:
        et, ev, etb = sys.exc_info()
        raise et, ev, etb
    return l["html"]



On Thu, 2005-05-19 at 10:58 +0800, Wouter van Marle wrote:
> Hi all,
> 
> Then hereby I'll make it a feature request:
> 
> I'd like to have a method to catch the html code as produced by the
> run() method of the psp.PSP object for use in e.g. an e-mail body,
> instead of having the run() method automatically pass it to the
> browser.
> 
> I mean: normally one does something like:
> page = psp.PSP(req,  "template.psp")
> page.run({"spam": 1, "eggs": 5})
> and the page is converted to html, and displayed in the browser. I'd
> like to use this templating for creating an e-mail, e.g.:
> 
> page = psp.PSP(req,  "template.psp")
> body = page.create({"spam": 1, "eggs": 5})
> 
> Which basically would do exactly the same as the run() module, except
> for having the html code as output.
> 
> Regards,
> Wouter van Marle 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list