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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""import mod_python apache modules safely
if they are imported from the command-line environment, errors will be trapped
this allows testing of python scripts from outside apache
"""
# Copyright 2002, 2003 St James Software
#
# This file is part of jToolkit.
#
# jToolkit is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# jToolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with jToolkit; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from jToolkit import errors
class FakeConnection:
user = 'testuser'
class FakeRequest:
connection = FakeConnection()
content_type = 'not externally set'
def send_http_header(self):
print 'send_http_header():'
print self.content_type
def write(self,towrite):
print 'write():'
print towrite
class FakeUtil:
fakestorage = {}
def FieldStorage(self,req):
return self.fakestorage
class FakeApache:
OK = "FakeApache returned OK"
APLOG_ERR = 0
APLOG_DEBUG = 1
def log_error(*args):
pass
class Fake_Apache:
def _global_lock(self, server, something, ID):
print "Fake_Apache just pretended to lock something"
def _global_unlock(self, server, something, ID):
print "Fake_Apache just pretended to unlock something"
# fakereq is useful for testing in Python interpreter
fakereq = FakeRequest()
try:
from mod_python import apache
except:
apache = FakeApache()
try:
import _apache
except:
_apache = Fake_Apache()
try:
from mod_python import util # for FieldStorage class
except:
util = FakeUtil()
######################
# Area to work around the crippled sys module in Apache
######################
realapache = not isinstance(_apache, Fake_Apache)
import sys
class ApacheErrorHandler(errors.ErrorHandler):
"""a class that handles error logging etc"""
def __init__(self, instance, webserver):
errors.ErrorHandler.__init__(self, instance)
self.webserver = webserver
def logerror(self, msg):
if isinstance(msg, unicode):
msg = msg.encode("UTF-8")
if self.webserver is None:
apache.log_error(msg, apache.APLOG_ERR)
else:
apache.log_error(msg, apache.APLOG_ERR, self.webserver.server)
errors.ErrorHandler.logerror(self, msg)
def logtrace(self, msg):
if isinstance(msg, unicode):
msg = msg.encode("UTF-8")
if self.webserver is None:
apache.log_error(msg, apache.APLOG_INFO)
else:
apache.log_error(msg, apache.APLOG_INFO, self.webserver.server)
errors.ErrorHandler.logtrace(self, msg)
|