[Zope3-checkins] SVN: Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/ Added a helper function for server code to use during setup of the WSGI

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Sep 8 23:27:29 EDT 2005


Log message for revision 38416:
  Added a helper function for server code to use during setup of the WSGI 
  application including a test that shows it works.
  
  

Changed:
  U   Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/README.txt
  U   Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/__init__.py
  U   Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/tests.py

-=-
Modified: Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/README.txt
===================================================================
--- Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/README.txt	2005-09-09 03:24:35 UTC (rev 38415)
+++ Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/README.txt	2005-09-09 03:27:29 UTC (rev 38416)
@@ -93,6 +93,36 @@
   i.e. by calling a method on the server that sets the application.
 
 
+Creating A WSGI Application
+---------------------------
+
+We do not always want Zope to control the startup process. People want to be
+able to start their favorite server and then register Zope simply as a WSGI
+application. For those cases we provide a very high-level function called
+``getWSGIApplication()`` that only requires the configuration file to set up
+the Zope 3 application server and returns a WSGI application. Here is a simple
+example:
+
+  >>> from cStringIO import StringIO
+  >>> configFile = StringIO('''
+  ... site-definition site.zcml
+  ...
+  ... <zodb>
+  ...   <mappingstorage />
+  ... </zodb>
+  ...
+  ... <eventlog>
+  ...   <logfile>
+  ...     path STDOUT
+  ...   </logfile>
+  ... </eventlog>
+  ... ''')
+
+  >>> app = wsgi.getWSGIApplication(configFile)
+  >>> app
+  <zope.app.wsgi.WSGIPublisherApplication object at ...>
+
+
 About WSGI
 ----------
 

Modified: Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/__init__.py
===================================================================
--- Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/__init__.py	2005-09-09 03:24:35 UTC (rev 38415)
+++ Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/__init__.py	2005-09-09 03:27:29 UTC (rev 38416)
@@ -15,10 +15,16 @@
 
 $Id$
 """
+import os
+import sys
+import ZConfig
+
+from zope.event import notify
 from zope.interface import implements
 from zope.publisher.publish import publish
 from zope.publisher.interfaces.http import IHeaderOutput
 
+from zope.app.appsetup import appsetup
 from zope.app.publication.httpfactory import HTTPPublicationRequestFactory
 from zope.app.wsgi import interfaces
 
@@ -53,3 +59,48 @@
 
         # Return the result body iterable.
         return response.consumeBodyIter()
+
+
+def getWSGIApplication(configfile, schemafile=None,
+                       requestFactory=HTTPPublicationRequestFactory):
+    # Load the configuration schema
+    if schemafile is None:
+        schemafile = os.path.join(
+            os.path.dirname(appsetup.__file__), 'schema.xml')
+
+    # Let's support both, an opened file and path
+    if isinstance(schemafile, basestring):
+        schema = ZConfig.loadSchema(schemafile)
+    else:
+        schema = ZConfig.loadSchemaFile(schemafile)
+
+    # Load the configuration file
+    # Let's support both, an opened file and path
+    try:
+        if isinstance(configfile, basestring):
+            options, handlers = ZConfig.loadConfig(schema, configfile)
+        else:
+            options, handlers = ZConfig.loadConfigFile(schema, configfile)
+    except ZConfig.ConfigurationError, msg:
+        sys.stderr.write("Error: %s\n" % str(msg))
+        sys.exit(2)
+
+    # Insert all specified Python paths
+    if options.path:
+        sys.path[:0] = [os.path.abspath(p) for p in options.path]
+
+    # Setup the event log
+    options.eventlog()
+
+    # Configure the application
+    appsetup.config(options.site_definition)
+
+    # Connect to and open the database
+    # XXX: Handle multiple databases.
+    db = options.database.open()
+
+    # Send out an event that the database has been opened
+    notify(appsetup.interfaces.DatabaseOpened(db))
+
+    # Create the WSGI application
+    return WSGIPublisherApplication(db, requestFactory)

Modified: Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/tests.py
===================================================================
--- Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/tests.py	2005-09-09 03:24:35 UTC (rev 38415)
+++ Zope3/branches/srichter-twisted-integration2/src/zope/app/wsgi/tests.py	2005-09-09 03:27:29 UTC (rev 38416)
@@ -21,10 +21,11 @@
 
 def test_suite():
     return unittest.TestSuite((
-        doctest.DocFileSuite('README.txt',
-                             setUp=placelesssetup.setUp,
-                             tearDown=placelesssetup.tearDown,
-                             optionflags=doctest.NORMALIZE_WHITESPACE),
+        doctest.DocFileSuite(
+            'README.txt',
+            setUp=placelesssetup.setUp,
+            tearDown=placelesssetup.tearDown,
+            optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
         ))
 
 if __name__ == '__main__':



More information about the Zope3-Checkins mailing list