File: mod_python_handler.py

package info (click to toggle)
quixote1 1.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,020 kB
  • ctags: 1,115
  • sloc: python: 5,339; ansic: 1,297; makefile: 83; sh: 43
file content (81 lines) | stat: -rw-r--r-- 2,175 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""quixote.mod_python_handler
$HeadURL: svn+ssh://svn/repos/trunk/quixote/mod_python_handler.py $
$Id: mod_python_handler.py 25236 2004-09-30 18:25:52Z nascheme $

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

import sys
from mod_python import apache
from quixote import Publisher, enable_ptl
from quixote.config import Config

class ErrorLog:
    def __init__(self, publisher):
        self.publisher = publisher

    def write(self, msg):
        self.publisher.log(msg)

    def close(self):
        pass

class ModPythonPublisher(Publisher):
    def __init__(self, package, config=None):
        Publisher.__init__(self, package, config)
        self.error_log = self.__error_log = ErrorLog(self) # may be overwritten
        self.setup_logs()
        self.__apache_request = None

    def log(self, msg):
        if self.error_log is self.__error_log:
            try:
                self.__apache_request.log_error(msg)
            except AttributeError:
                apache.log_error(msg)
        else:
            Publisher.log(self, msg)

    def publish_modpython(self, req):
        """publish_modpython() -> None

        Entry point from mod_python.
        """
        self.__apache_request = req
        try:
            self.publish(apache.CGIStdin(req),
                         apache.CGIStdout(req),
                         sys.stderr,
                         apache.build_cgi_env(req))

            return apache.OK
        finally:
            self.__apache_request = None

enable_ptl()

name2publisher = {}

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

    try:
        configfile = opts['quixote-config-file']
        config = Config()
        config.read_file(configfile)
    except KeyError:
        config = None

    if not package:
        return apache.HTTP_INTERNAL_SERVER_ERROR

    pub = name2publisher.get(package)
    if pub is None:
        pub = ModPythonPublisher(package, config)
        name2publisher[package] = pub
    return pub.publish_modpython(req)