[Checkins] SVN: grokcore.startup/trunk/ implement the debug_applcation_factory function that allows for configuring what exceptions not to reraise when debugging

Jan-Wijbrand Kolman janwijbrand at gmail.com
Fri Oct 2 06:22:30 EDT 2009


Log message for revision 104739:
  implement the debug_applcation_factory function that allows for configuring what exceptions not to reraise when debugging

Changed:
  U   grokcore.startup/trunk/CHANGES.txt
  U   grokcore.startup/trunk/setup.py
  U   grokcore.startup/trunk/src/grokcore/startup/README.txt
  U   grokcore.startup/trunk/src/grokcore/startup/__init__.py
  U   grokcore.startup/trunk/src/grokcore/startup/startup.py

-=-
Modified: grokcore.startup/trunk/CHANGES.txt
===================================================================
--- grokcore.startup/trunk/CHANGES.txt	2009-10-02 10:18:58 UTC (rev 104738)
+++ grokcore.startup/trunk/CHANGES.txt	2009-10-02 10:22:30 UTC (rev 104739)
@@ -4,6 +4,14 @@
 0.3 (unreleased)
 ================
 
+* Add a ``debug_application_factory`` function that allows for the
+  ``exempt-exceptions`` configuration option. The value for this option
+  should be a comma seperated list of dotted names for each of the exceptions
+  that should not be re-raised during debugging.
+
+  This for one allow the IUnauthorized exception to still be handled by zope
+  and thus have the normal authentication mechanisms still work.
+
 * Bring versions.cfg in line with current grok versions.cfg.
 
 0.2 (2009-02-21)

Modified: grokcore.startup/trunk/setup.py
===================================================================
--- grokcore.startup/trunk/setup.py	2009-10-02 10:18:58 UTC (rev 104738)
+++ grokcore.startup/trunk/setup.py	2009-10-02 10:22:30 UTC (rev 104739)
@@ -41,6 +41,11 @@
     zip_safe=False,
     install_requires=['setuptools',
                       'zdaemon',
+                      'zope.interface',
+                      'zope.component',
+                      'zope.security',
+                      'zope.publisher',
+                      'zope.dottedname',
                       'zope.app.wsgi',
                       'zope.app.debug',
                       ],

Modified: grokcore.startup/trunk/src/grokcore/startup/README.txt
===================================================================
--- grokcore.startup/trunk/src/grokcore/startup/README.txt	2009-10-02 10:18:58 UTC (rev 104738)
+++ grokcore.startup/trunk/src/grokcore/startup/README.txt	2009-10-02 10:22:30 UTC (rev 104739)
@@ -54,12 +54,12 @@
 
   # setup.py
   from setuptools import setup, find_packages
-  
+
   setup(name='sampleproject',
         version='0.1dev',
         description="A sample project",
         long_description="""Without a long description.""",
-        classifiers=[], 
+        classifiers=[],
         keywords="",
         author="U.N.Owen",
         author_email="",
@@ -85,7 +85,7 @@
   [buildout]
   develop = .
   parts = app
-  
+
   [app]
   recipe = zc.recipe.egg
   eggs = sampleproject
@@ -121,7 +121,7 @@
   <zodb>
     <mappingstorage />
   </zodb>
-  
+
   <eventlog>
     <logfile>
       path STDOUT
@@ -139,12 +139,12 @@
 
   [app:main]
   use = egg:sampleproject
-  
+
   [server:main]
   use = egg:Paste#http
   host = 127.0.0.1
   port = 8080
-  
+
   [DEFAULT]
   zope_conf = %(here)s/zope.conf
 
@@ -196,11 +196,63 @@
   Now we can call ``application_factory`` to get a WSGI application::
 
     >>> from grokcore.startup import application_factory
-    >>> app_factory = application_factory(dict(zope_conf = zope_conf))
+    >>> app_factory = application_factory({'zope_conf': zope_conf})
     >>> app_factory
     <zope.app.wsgi.WSGIPublisherApplication object at 0x...>
 
+  There's a second application factory that can be used when debugging
+  the application, especially when using the ``z3c.evalexception`` middleware.
 
+  When debugging zope is instructed not to handle any raised exceptions
+  itself. The ``z3c.evalexception`` middleware then catches the exceptions
+  and provides an user interfaces for debugging in the webbrowser.
+
+  As a result also the IUnauthorized execption would not be handled by zope
+  the authentication mechanisms of zope are not triggered. As a result, when
+  debugging one cannot login.
+
+  The ``debug_application_factory`` function accepts the "exempt-execptions"
+  configuration option. The value for this option should be a comma seperated
+  list of dotted names for each of the execptions that should *still* be
+  handled by zope and not re-raised to be catched by the middleware.
+
+    >>> from grokcore.startup import debug_application_factory
+    >>> app_factory = debug_application_factory({'zope_conf': zope_conf})
+    >>> app_factory
+    <zope.app.wsgi.WSGIPublisherApplication object at 0x...>
+
+    >>> from zope.interface import implements
+    >>> from zope.security.interfaces import IUnauthorized
+    >>> class UnauthorizedException(object):
+    ...     implements(IUnauthorized)
+    >>>
+    >>> from zope.component import queryAdapter
+    >>> from zope.publisher.interfaces import IReRaiseException
+
+Since the ``exempt-execptions`` configuration option was not passed, there's
+no IReRaiseException adapter registered for any type of exceptions including
+IUnauthorized:
+
+    >>> error = UnauthorizedException()
+    >>> reraise = queryAdapter(error, IReRaiseException, default=None)
+    >>> reraise is None
+    True
+
+When the option is passed, the adapter will be registered. Calling this
+adapter yields ``False``, telling zope not to reraise this particular
+exception.
+
+    >>> app_factory = debug_application_factory(
+    ...     {'zope_conf': zope_conf},
+    ...     **{'exempt-exceptions': 'zope.security.interfaces.IUnauthorized'})
+    >>>
+    >>> reraise = queryAdapter(error, IReRaiseException, default=None)
+    >>> reraise is None
+    False
+    >>> reraise()
+    False
+
+
 ``interactive_debug_prompt(zope_conf_path)``
 --------------------------------------------
 

Modified: grokcore.startup/trunk/src/grokcore/startup/__init__.py
===================================================================
--- grokcore.startup/trunk/src/grokcore/startup/__init__.py	2009-10-02 10:18:58 UTC (rev 104738)
+++ grokcore.startup/trunk/src/grokcore/startup/__init__.py	2009-10-02 10:22:30 UTC (rev 104739)
@@ -13,5 +13,6 @@
 ##############################################################################
 # Make this a package.
 from grokcore.startup.startup import (application_factory,
+                                      debug_application_factory,
                                       interactive_debug_prompt,
                                       zdaemon_controller)

Modified: grokcore.startup/trunk/src/grokcore/startup/startup.py
===================================================================
--- grokcore.startup/trunk/src/grokcore/startup/startup.py	2009-10-02 10:18:58 UTC (rev 104738)
+++ grokcore.startup/trunk/src/grokcore/startup/startup.py	2009-10-02 10:22:30 UTC (rev 104739)
@@ -5,11 +5,36 @@
 import zope.app.wsgi
 import zope.app.debug
 
+from zope.component import provideAdapter
+from zope.security.interfaces import IUnauthorized
+from zope.publisher.interfaces import IReRaiseException
+from zope.dottedname.resolve import resolve
+
 def application_factory(global_conf, **local_conf):
     zope_conf = local_conf.get('zope_conf', global_conf.get(
             'zope_conf', os.path.join('parts', 'etc', 'zope.conf')))
     return zope.app.wsgi.getWSGIApplication(zope_conf)
 
+
+def debug_application_factory(global_conf, **local_conf):
+    # First create the application itself
+    app = application_factory(global_conf, **local_conf)
+    # Then register the IReRaiseException adaptation for
+    # various types of exceptions that are exempt from being
+    # raised by the publisher.
+    def do_not_reraise_exception(context):
+        return lambda : False
+    iface_names = local_conf.get('exempt-exceptions', '').split(',')
+    for name in iface_names:
+        name = name.strip()
+        if not name:
+            continue
+        iface = resolve(name)
+        provideAdapter(do_not_reraise_exception, (iface, ), IReRaiseException)
+    # Return the created application
+    return app
+    
+
 def interactive_debug_prompt(zope_conf=os.path.join('parts', 'etc',
                                                     'zope.conf')):
     db = zope.app.wsgi.config(zope_conf)



More information about the checkins mailing list