"""quixote.mod_python_handler

mod_python handler for Quixote.  See the "mod_python configuration"
section of doc/web-server.txt for details.
"""

__revision__ = "$Id: mod_python_handler.py,v 1.5 2001/09/28 21:03:22 gward Exp $"

# adapted from Erno Kuusela's quixote_handler.py, 2001/09/19 GPW

import os, sys
from mod_python import apache
from quixote import Publisher, enable_ptl

class ModPythonPublisher(Publisher):
    def publish_modpython(self, req):
        """publish_modpython() -> None
        
        Entry point from mod_python.
        """
        real_env = os.environ
        try:
            os.environ = apache.build_cgi_env(req)
            self.publish(apache.CGIStdin(req),
                         apache.CGIStdout(req),
                         sys.stderr)
        finally:
            os.environ = real_env
        return apache.OK

enable_ptl()

name2publisher = {}

def handler(req):
    opts = req.get_options()
    try:
        package = opts['quixote-root-namespace']
    except KeyError:
        package = None

    if not package:
        return apache.HTTP_INTERNAL_SERVER_ERROR
    else:
        if not name2publisher.has_key(package):
            name2publisher[package] = ModPythonPublisher(package)
        return name2publisher[package].publish_modpython(req)
