[Zope3-checkins] SVN: Zope3/trunk/ This package provides a WSGI compliant wrapper for the zope publisher

Eckart Hertzler eckart at hertzler.de
Wed Nov 10 06:08:18 EST 2004


Log message for revision 28416:
  
    This package provides a WSGI compliant wrapper for the zope publisher
  
    It includes the application side only. 
    The package provides a WSGI application object in zope.app.wsgi.publisherApp.
    This object uses by default a HTTPPublicationRequestFactory.
   
    
  

Changed:
  A   Zope3/trunk/package-includes/wsgi-configure.zcml
  A   Zope3/trunk/src/zope/app/wsgi/
  A   Zope3/trunk/src/zope/app/wsgi/__init__.py
  A   Zope3/trunk/src/zope/app/wsgi/configure.zcml

-=-
Added: Zope3/trunk/package-includes/wsgi-configure.zcml
===================================================================
--- Zope3/trunk/package-includes/wsgi-configure.zcml	2004-11-10 05:42:42 UTC (rev 28415)
+++ Zope3/trunk/package-includes/wsgi-configure.zcml	2004-11-10 11:08:17 UTC (rev 28416)
@@ -0,0 +1 @@
+<include package="zope.app.wsgi" />


Property changes on: Zope3/trunk/package-includes/wsgi-configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/wsgi/__init__.py
===================================================================
--- Zope3/trunk/src/zope/app/wsgi/__init__.py	2004-11-10 05:42:42 UTC (rev 28415)
+++ Zope3/trunk/src/zope/app/wsgi/__init__.py	2004-11-10 11:08:17 UTC (rev 28416)
@@ -0,0 +1,145 @@
+##############################################################################
+#
+# Copyright (c) 2004 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""A WSGI Application wrapper for zope
+
+$Id$
+"""
+from zope.interface import implements
+from zope.publisher.publish import publish
+from zope.server.interfaces import IHeaderOutput
+
+from zope.app.publication.httpfactory import HTTPPublicationRequestFactory
+
+class WsgiOutput(object):
+    """This class handles the output generated by
+    the publisher. It is used to collect the headers
+    and as an outstream to output the response body.
+
+    When write is first called by the response, it initiates
+    the reponse by invoking the WSGI start_response callable.
+    """
+
+    implements(IHeaderOutput)
+
+    def __init__(self, start_response):
+        self._headers = {}
+        self._accumulatedHeaders = []
+        self._statusString = ""
+        self._headersSent = False
+
+        self.wsgi_write = None
+        self.start_response = start_response
+
+    def setResponseStatus(self,status, reason):
+        """Sets the status code and the accompanying message.
+        """
+        self._statusString = str(status)+' '+reason
+
+    def setResponseHeaders(self, mapping):
+        """Sets headers.  The headers must be Correctly-Cased.
+        """
+        self._headers.update(mapping)
+
+    def appendResponseHeaders(self, lst):
+        """Sets headers that can potentially repeat.
+
+        Takes a list of strings.
+        """
+        accum = self._accumulatedHeaders
+        if accum is None:
+            self._accumulatedHeaders = accum = []
+            accum.extend(lst)
+
+    def wroteResponseHeader(self):
+        """Returns a flag indicating whether the response
+
+        header has already been sent.
+        """
+        return self._headersSent
+
+    def setAuthUserName(self, name):
+        """Sets the name of the authenticated user so the name can be logged.
+        """
+        pass
+
+    def getHeaders(self):
+        """return the response headers as a list of tuples according
+        to the WSGI spec
+        """
+        response_headers = self._headers.items()
+        accum = [ tuple(line.split(':')) for line in self._accumulatedHeaders]
+        response_headers.extend(accum)
+        return response_headers
+
+
+    def write(self, data):
+        """write the response.
+        If the reponse has not begun, call the wsgi servers
+        'start_reponse' callable to begin the response
+        """
+        if not self._headersSent:
+            self.wsgi_write = self.start_response(self._statusString,
+                                         self.getHeaders())
+            self._headersSent = True
+
+        self.wsgi_write(data)
+
+
+class PublisherApp(object):
+    """A WSGI application implemenation for the zope publisher
+
+    Instances of this class can be used as a WSGI application
+    object.
+
+    The class relies on a properly initialized request factory.
+    
+    """
+    
+
+    def __init__(self, db=None, factory=HTTPPublicationRequestFactory):
+        self.request_factory = None
+
+        if db is not None:
+            self.request_factory = factory(db)
+
+    def __call__(self, env, start_response):
+        """makes instances a WSGI callable application object
+        """
+        wsgi_output = WsgiOutput(start_response)
+
+        request = self.request_factory( \
+            env['wsgi.input'], wsgi_output, env)
+
+        request.response.setHeaderOutput(wsgi_output)
+
+        publish(request)
+
+        request.close()
+
+        # since the response is written using the WSGI write callable
+        # return an empty iterable (see 
+        return ""
+
+
+# provides an WSGI application object
+publisherApp = PublisherApp()
+
+def bootStrapSubscriber(event):
+    """subcriber for a database opened event
+    initializes the request factory of the wsgi applicaion object
+    """
+    publisherApp.request_factory = \
+        HTTPPublicationRequestFactory(event.database)
+
+


Property changes on: Zope3/trunk/src/zope/app/wsgi/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/app/wsgi/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/wsgi/configure.zcml	2004-11-10 05:42:42 UTC (rev 28415)
+++ Zope3/trunk/src/zope/app/wsgi/configure.zcml	2004-11-10 11:08:17 UTC (rev 28416)
@@ -0,0 +1,8 @@
+<configure xmlns="http://namespaces.zope.org/zope">
+
+  <subscriber
+      for="zope.app.appsetup.IDatabaseOpenedEvent"
+      factory=".bootStrapSubscriber"
+      />
+
+</configure>


Property changes on: Zope3/trunk/src/zope/app/wsgi/configure.zcml
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Zope3-Checkins mailing list