[mod_python] psp without apache

Jim Gallacher jpg at jgassociates.ca
Thu Jun 1 11:56:19 EDT 2006


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:
> 
>>>> from mod_python import psp
> 
> Traceback (most recent call last):
>   File "<pyshell#0>", line 1, in -toplevel-
>     from mod_python import psp
>   File "/usr/lib/python2.4/site-packages/mod_python/psp.py", line 20, in -toplevel-
>     import apache, Session, util, _psp
>   File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 28, in -toplevel-
>     import _apache
> ImportError: No module named _apache
> 
> Any ideas?

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

def external_psp_runner_thing():
     filename = 'test.psp'
     req = FakeRequestObject('output.txt')
     vars = {'req': req}
     source = _psp.parse(filename)
     code = compile(source, filename, "exec")
     global_scope = globals().copy()
     global_scope.update(vars)
     exec code in global_scope


class FakeRequestObject(object):
     ''' We need an object which has a write method that will take
         a length parameter the same as req.write(). For testing
         we'll just wrap a file object and write to that.
     '''
     def __init__(self, filename):
         self.fout = open(filename, 'w')

     def write(self, value, length):
         self.fout.write(value)

Jim


More information about the Mod_python mailing list