"""This is a WSGI adaptor for Aquarium."""
__docformat__ = "restructuredtext"
# Created: Fri Apr 29 16:56:10 PDT 2005
# Author: Ksenia Marasanova, Shannon -jj Behrens
# Email: ksenia.marasanova@gmail.com, jjinux@users.sourceforge.net
#
# Copyright (c) Ksenia Marasanova. All rights reserved.
from cgi import FieldStorage
from cStringIO import StringIO
from aquarium.util.Aquarium import Aquarium
from aquarium.util import HTTPResponses
from aquarium.wsadaptor.WebServerAdaptor import WebServerAdaptor
class WSGIAdaptor(WebServerAdaptor):
"""This is a WSGI adaptor for Aquarium.
The following properties are used:
environ
This is the WSGI context object.
status
This is the HTTP response ``(code, msg)``.
wfile
Output will be buffered here.
"""
def __init__(self, environ):
self.environ = environ
self.status = (str(HTTPResponses.OK), "")
self.wfile = StringIO()
def setResponseCode(self, code=HTTPResponses.OK,
msg="Script output follows"):
self.status = (str(code), msg)
def writeHeaders(self, headers):
self.headers = headers
def getCgiEnv(self):
return self.environ
def getForm(self):
env = self.getCgiEnv()
return FieldStorage(fp=env['wsgi.input'], environ=env)
def write(self, s):
self.wfile.write(s)
def handler(environ, start_response):
"""This is WSGI callable that can be used with WSGI server.
Example::
#!/usr/bin/env python
# Set your packagePath and/or sys.path.
from aquarium.wsadaptor.WSGIAdaptor import handler
# Now you can pass "handler" to your favorite WSGI server.
If you need to use your own subclass of ``aquarium.util.Aquarium``, do
this::
from aquarium.wsadaptor import WSGIAdaptor
WSGIAdaptor.Aquarium = SpecialAquariumSubclass
from WSGIAdaptor import WSGIApp
...as before...
"""
wsadaptor = WSGIAdaptor(environ)
Aquarium(wsadaptor)()
start_response(" ".join(wsadaptor.status), wsadaptor.headers)
yield wsadaptor.wfile.getvalue()
wsadaptor.wfile.close()
|