[Checkins] SVN: grokcore.startup/branches/zodbless/s basic working

Christian Klinger cvs-admin at zope.org
Fri Apr 27 13:57:51 UTC 2012


Log message for revision 125329:
  basic working

Changed:
  U   grokcore.startup/branches/zodbless/setup.py
  U   grokcore.startup/branches/zodbless/src/grokcore/startup/README.txt
  A   grokcore.startup/branches/zodbless/src/grokcore/startup/nozodb.py
  A   grokcore.startup/branches/zodbless/src/grokcore/startup/utils.py

-=-
Modified: grokcore.startup/branches/zodbless/setup.py
===================================================================
--- grokcore.startup/branches/zodbless/setup.py	2012-04-27 13:56:42 UTC (rev 125328)
+++ grokcore.startup/branches/zodbless/setup.py	2012-04-27 13:57:47 UTC (rev 125329)
@@ -55,6 +55,7 @@
         'paste.app_factory': [
             'main = grokcore.startup:application_factory',
             'debug = grokcore.startup:debug_application_factory',
+            'nozodb = grokcore.startup:nozodb_factory',
             ]
     },
 )

Modified: grokcore.startup/branches/zodbless/src/grokcore/startup/README.txt
===================================================================
--- grokcore.startup/branches/zodbless/src/grokcore/startup/README.txt	2012-04-27 13:56:42 UTC (rev 125328)
+++ grokcore.startup/branches/zodbless/src/grokcore/startup/README.txt	2012-04-27 13:57:47 UTC (rev 125329)
@@ -339,6 +339,53 @@
     >>> import shutil
     >>> shutil.rmtree(temp_dir)
 
+
+Environment without a ZODB
+--------------------------
+
+To setup a grok environment which works without the zodb you
+have to replace the paster-application-factory which typically is
+located in the debug.ini and in the deploy.ini: To be concrete
+replace grokcore.startup#... with megrok.nozodb#nozodb
+
+    [app:grok]
+    use = egg:megrok.nozodb#nozodb
+
+
+We have to create a simple site definition file, which is also quite
+plain::
+
+   >>> import os, tempfile
+   >>> temp_dir = tempfile.mkdtemp()
+
+   >>> sitezcml = os.path.join(temp_dir, 'site.zcml')
+   >>> open(sitezcml, 'w').write('<configure />')
+
+   >>> zope_conf = os.path.join(temp_dir, 'zope.conf')
+   >>> open(zope_conf, 'wb').write('''
+   ... site-definition %s
+   ...
+   ... <zodb>
+   ... </zodb>
+   ...
+   ... <eventlog>
+   ...   <logfile>
+   ...     path STDOUT
+   ...   </logfile>
+   ... </eventlog>
+   ... ''' %sitezcml)
+
+
+   >>> from grokcore.startup import nozodb_factory
+   >>> app_factory = nozodb_factory({'zope_conf': zope_conf})
+
+  Clean up the temp_dir
+
+    >>> import shutil
+    >>> shutil.rmtree(temp_dir)
+
+
+
 .. _grok: http://pypi.python.org/pypi/grok
 .. _grokproject: http://pypi.python.org/pypi/grokproject
 .. _Paste: http://pythonpaste.org/

Added: grokcore.startup/branches/zodbless/src/grokcore/startup/nozodb.py
===================================================================
--- grokcore.startup/branches/zodbless/src/grokcore/startup/nozodb.py	                        (rev 0)
+++ grokcore.startup/branches/zodbless/src/grokcore/startup/nozodb.py	2012-04-27 13:57:47 UTC (rev 125329)
@@ -0,0 +1,14 @@
+# -*- coding: utf-8 -*-
+
+from grokcore.startup.utils import config, NOZODBWSGIPublisherApplication
+
+def nozodb_factory(global_conf, **local_conf):
+    """ this factory creates an wsgi-application, which don't
+        have a releation to zodb
+    """
+    zope_conf = local_conf.get('zope_conf')
+    if zope_conf is None:
+        zope_conf = global_conf['zope_conf']
+    config(zope_conf)
+    app = NOZODBWSGIPublisherApplication(handle_errors=True)
+    return app

Added: grokcore.startup/branches/zodbless/src/grokcore/startup/utils.py
===================================================================
--- grokcore.startup/branches/zodbless/src/grokcore/startup/utils.py	                        (rev 0)
+++ grokcore.startup/branches/zodbless/src/grokcore/startup/utils.py	2012-04-27 13:57:47 UTC (rev 125329)
@@ -0,0 +1,121 @@
+# -*- coding: utf-8 -*-
+
+import os
+import sys
+import logging
+import ZConfig
+
+from zope import component
+from zope.site.hooks import setSite
+from zope.site.interfaces import IRootFolder
+
+from grokcore.view.publication import GrokBrowserPublication
+
+from zope.app.appsetup import appsetup, product
+from zope.publisher.interfaces import ISkinnable
+from zope.app.wsgi import WSGIPublisherApplication
+from zope.publisher.skinnable import setDefaultSkin
+from zope.app.publication.httpfactory import chooseClasses
+
+
+# XXX We have to subclass from WSGIPublisherApplication because the
+#     ZODB-Stuff is very hard linked into it.
+class NOZODBWSGIPublisherApplication(WSGIPublisherApplication):
+    """
+    """
+
+    def __init__(self, handle_errors):
+        self.handleErrors = handle_errors
+        self.requestFactory = SimplePublicationRequestFactory()
+
+
+class SimplePublicationRequestFactory(object):
+    """ This Publication Request Factory does not have a link to
+        ZODB.
+    """
+
+    def __call__(self, input_stream, env):
+        method = env.get('REQUEST_METHOD', 'GET').upper()
+        request_class, publication_class = chooseClasses(method, env)
+
+        publication = BrowserPublication()
+        request = request_class(input_stream, env)
+        request.setPublication(publication)
+        if ISkinnable.providedBy(request):
+            # only ISkinnable requests have skins
+            setDefaultSkin(request)
+        return request
+
+
+class BrowserPublication(GrokBrowserPublication):
+    """ Browser Publication with NoZODB
+    """
+
+    def __init__(self):
+        app = component.queryUtility(IRootFolder)
+        if not app:
+            raise NotImplementedError("""
+                You have to register your own IRootFolder utility
+                to be able to register your stuff on it:
+                class AppRoot(grok.GlobalUtility):
+                    grok.implements(IRootFolder)""")
+        else:
+            setSite(app)
+            self._app = app
+
+    def getApplication(self, request):
+        return self._app
+
+
+# Maybe we can include a smarter version
+# in zope.app.wsgi.__init__ config for this...
+def config(configfile, schemafile=None, features=()):
+    # Load the configuration schema
+    if schemafile is None:
+        schemafile = os.path.join(
+            os.path.dirname(appsetup.__file__), 'schema', '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]
+
+    # Parse product configs
+    product.setProductConfigurations(
+        options.product_config)
+
+    # Setup the event log
+    options.eventlog()
+
+    # Setup other defined loggers
+    for logger in options.loggers:
+        logger()
+
+    # Insert the devmode feature, if turned on
+    if options.devmode:
+        features += ('devmode',)
+        logging.warning("Developer mode is enabled: this is a security risk "
+            "and should NOT be enabled on production servers. Developer mode "
+            "can usually be turned off by setting the `devmode` option to "
+            "`off` or by removing it from the instance configuration file "
+            "completely.")
+
+    # Execute the ZCML configuration.
+    appsetup.config(options.site_definition, features=features)
+    return None



More information about the checkins mailing list