"""This subclass of ``WebServerAdaptor`` is for mod_python."""
__docformat__ = "restructuredtext"
# Created: Fri Apr 13 19:10:54 PDT 2001
# Author: Shannon -jj Behrens
# Email: jjinux@users.sourceforge.net
#
# Copyright (c) Shannon -jj Behrens. All rights reserved.
# Delay imports:
#
# * For epyDoc's sake, don't assume the user has mod_python at all.
# * Because we want to set __main__.packagePath from within the handler
# method, avoid other aquarium imports.
from aquarium.wsadaptor.WebServerAdaptor import WebServerAdaptor
class ModPythonAdaptor(WebServerAdaptor):
"""This subclass of WebServerAdaptor_ is for mod_python_.
There are many ways to configure mod_python. In the example below, any
request for "/aquarium" is given to Aquarium. Note that this configuration
even removes the need for a separate entry point::
<Location "/aquarium">
# Update your path, if necessary:
# PythonPath "sys.path + ['/path/to']"
SetHandler python-program
PythonHandler aquarium.wsadaptor.ModPythonAdaptor
PythonOption AQUARIUM_PACKAGE_PATH /var/www/site-packages
PythonDebug On
</Location>
Be careful if you are using SELinux. It can cause errors that look like
Aquarium is not even installed! Keep an eye on both the server log and
``/var/log/messages`` if things aren't working.
The following urlscheme modules can be used with this Web server adaptor:
QueryParameters_, PathInfo_.
The following attributes are used:
req
This is the request object given to us by mod_python.
.. _WebServerAdaptor:
aquarium.wsadaptor.WebServerAdaptor.WebServerAdaptor-class.html
.. _mod_python: http://www.modpython.org
.. _QueryParameters:
aquarium.urlscheme.QueryParameters.QueryParameters-class.html
.. _PathInfo:
aquarium.urlscheme.PathInfo.PathInfo-class.html
"""
def __init__(self, req):
"""Receive the request object."""
self.req = req
def setResponseCode(self, code=None, __ignored_msg=None):
"""Set the Web server response code and message.
Keword arguments:
code
The default means "OK".
__ignored_msg
This is an unused part of the API.
"""
if code is None:
from aquarium.util import HTTPResponses
code = HTTPResponses.OK
self.req.status = code
def write(self, s):
"""Output a string."""
self.req.write(s)
def writeHeaders(self, headersList):
"""Output a list of header strings.
The list will contains tuples like::
("Content-type", "text/html")
"""
req = self.req
allKeys = []
for (key, value) in headersList:
if key.lower() == "content-type":
req.content_type = value
elif key in allKeys:
req.headers_out.add(key, value) # Duplicate.
else:
allKeys.append(key)
req.headers_out[key] = value
req.send_http_header()
def getCgiEnv(self):
"""Return CGI-like environmental variables."""
from mod_python.apache import build_cgi_env
if not hasattr(self, "_cgiEnv"):
self._cgiEnv = build_cgi_env(self.req)
return self._cgiEnv
def getForm(self):
"""Instantiate some ``cgi.FieldStorage`` and return the instance."""
from mod_python.util import FieldStorage
return FieldStorage(self.req)
def handler(req):
"""Use this as your mod_python handler.
If you need to use your own subclass of ``aquarium.util.Aquarium``, create
a new modules that has a ``handler`` function like this one. You can still
make use of this module's ``ModPythonAdaptor`` class and
``updatePackagePath`` function.
This function calls ``updatePackagePath``.
"""
updatePackagePath(req)
from aquarium.util.Aquarium import Aquarium
from aquarium.util import HTTPResponses
from mod_python import apache
adaptor = ModPythonAdaptor(req)
Aquarium(adaptor)()
if req.status == HTTPResponses.OK:
return apache.OK # apache.OK != HTTPResponses.OK
else:
return req.status
def updatePackagePath(req):
"""Update ``__main__.packagePath``.
If the mod_python PythonOption AQUARIUM_PACKAGE_PATH is set, then set
``__main__.packagePath`` and reload the aquarium package.
"""
options = req.get_options()
if options.has_key("AQUARIUM_PACKAGE_PATH"):
import __main__
__main__.packagePath = options["AQUARIUM_PACKAGE_PATH"].split(":")
import aquarium
reload(aquarium)
|