[mod_python] PSP Notes (fwd)

Gregory (Grisha) Trubetskoy grisha at apache.org
Fri Aug 29 17:23:24 EST 2003


---------- Forwarded message ----------
Date: Wed, 13 Aug 2003 14:34:14 -0400 (EDT)
From: "Gregory (Grisha) Trubetskoy" <grisha at apache.org>
To: python-dev at httpd.apache.org
Subject: PSP Notes


As an excercise in PSP usability, I've been going through JSP examples
from Tomcat and rewriting them using PSP, here are random notes on the
subject.


* JSP has a notion of an application (e.g. "/apps/mywebapp") with a
document root (e.g. /home/www/blah). Some stuff (e.g. location of classes)
then becomes relative to those paths. In PSP there isn't really a clear
concept of an application, but the closest thing to that is the directory
in which the PythonHandler directive is specified. This is nothing new,
btw, it's always how mod_python worked. So if you have:

<Directory /home/www/psp>
  AddHandler mod_python .psp   <--note the new clever handler name
  PythonHandler mod_python.psp
</Directory>

...then /home/www/psp will be prepended to sys.path, therefore if you
create a directory /home/www/psp/pylib, you could import a module from a
psp file like this:

<%
from pylib import mymodule
%>

This way you have your entire application under /home/www/psp, you can
use, say, /home/www/psp/html for psp/html and all your Python modules are
in /home/www/pylib, giving you a clear separation of logic and interface.


* Next thing that I ran into is having to reload mymodule if it changes.
There is now a new recommended way of doing this:

mymodule = apache.import_module("pylib.mymodule")

This is the same function that mod_python uses internally for autorelad
and takes care of all your reload needs. It does stat the file every time,
so when you're sure you won't need to reload the page, just change it to:

mymodule = apache.import_module("pylib.mymodule", autoreload=0)


* JSP page import directive

JSP:

<%@ page import = "num.NumberGuessBean" %>

PSP:

<%
num = apache.import_module("pylib.num")
%>
... now you've got num.NumberGuessBean, you get the idea


* JSP useBean action

JSP:

<jsp:useBean id="numguess" class="num.NumberGuessBean" scope="session"/>

This means that numguess is an instance of num.NumberGuessBean and it will
persist for as long as the session exists.

PSP (again, no need for special syntax):

<%
numguess = session.setdefault("numguess", num.NumberGuessBean())
%>


* JSP setProperty

This will match up form elements and set them in a Java object. So if the
object defines setColor() and the form contains a field "Color", JSP will
call setColor passing value of color as the argument.

JSP:

<jsp:setProperty name="numguess" property="*"/>

PSP:

Well, there isn't exactly this functionality, but there is something
different. You can use the same functionality that the publisher handler
uses to match up form elements with a function:

class Car:

    def __init__(self, color):

         # do something

Then a psp page called as result of a form submition can do this:

<%
car = psp.apply_data(Car)
%>

This will call the callable object Car (classes are callable), passing it
the value of "color", and resulting in an instance of Car which is
assigned to car.


* <%@ page session="false"%>

In JSP this means don't create a session for this page. This is really
easy in PSP - just don't mention the "session" variable in your page. The
psp handler examines the code for mentions of "session" and generates (and
locks) one if it encounters such mentions.


* page errorPage

JSP allows you to specify a special error page to be displayed when an
exception is thrown.

JSP:
<%@ page errorPage="errorpge.jsp" %>

PSP:
<%
psp.set_error_page("errorpge.psp")
%>

When (if) errorpge.psp is invoked, it will have a global "exception"
variable which will contain same info as returned by sys.exc_info().

* include

Of course, PSP supports <%@ include file="blah.html"%>



Grisha


More information about the Mod_python mailing list