[mod_python] psp module

Graham Dumpleton grahamd at dscpl.com.au
Wed May 18 21:12:19 EDT 2005


Wouter van Marle wrote ..
> Hi all,
> 
> Is there a way to catch the html code as produced by the psp module?
> 
> 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.
> 
> Now is it possible to catch this html code in a string, instead of
> sending it to the browser? This as I'd like to use the psp templating
> for creating an e-mail body.

At first glance there seems to be no feature to do it. What you might
try (I haven't), is that presuming that all output is generated through
calls to req.write(), is to substitute the "req" object with a wrapper
class that provides its own req.write() which is actually the write()
method on a StringIO object.

class Wrapper:

  def __init__(self,req,write):
    self.__req = req
    self.write = write

  def __getattr__(self,name):
    return getattr(self,__req,name)

s = StringIO.StringIO()

page = psp.PSP(Wrapper(req,s.write),  "template.psp")
page.run({"spam": 1, "eggs": 5})

req.log_error(s.getvalue())

If you were also stashing stuff in "req" from the PSP page, would also
need a __setattr__() method in the wrapper.

Graham
  


More information about the Mod_python mailing list