ModPythonAdaptor.py :  » Web-Frameworks » Aquarium » aquarium-2.3 » aquarium » wsadaptor » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Aquarium 
Aquarium » aquarium 2.3 » aquarium » wsadaptor » ModPythonAdaptor.py
"""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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.