[PATCH cubicweb] [pyramid/ctl] add a new option to activate the debugtoolbar (-t)

Laurent Peuch cortex at worlddomination.be
Wed Oct 30 07:16:00 CET 2019


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1559154547 -7200
#      Wed May 29 20:29:07 2019 +0200
# Node ID ae18da4dd535cb7ae40f0c7e106b01946a117fce
# Parent  ff63319a1730d9970240d4684df91afc0dc56d06
# Available At https://hg.logilab.org/users/lpeuch/cubicweb
#              hg pull https://hg.logilab.org/users/lpeuch/cubicweb -r ae18da4dd535
[pyramid/ctl] add a new option to activate the debugtoolbar (-t)

While this toolbar isn't yet very integrated with CW, it integrates an inline
debugging shell like werkzeug for flask or django_extensions which greatly
helps debugging on exception.

It can already be manually activated by writting "pyramid.includes =
pyramid_debugtoolbar" but it's hidden somewhere in the documentation and not
very accessible and annoying to do.

Closes #17219765

diff --git a/cubicweb/pyramid/__init__.py b/cubicweb/pyramid/__init__.py
--- a/cubicweb/pyramid/__init__.py
+++ b/cubicweb/pyramid/__init__.py
@@ -33,7 +33,7 @@ from pyramid.exceptions import Configura
 from pyramid.settings import asbool, aslist
 
 
-def config_from_cwconfig(cwconfig, settings=None):
+def config_from_cwconfig(cwconfig, settings=None, debugtoolbar=False):
     """Return a Pyramid Configurator instance built from a CubicWeb config and
     Pyramid-specific configuration files (pyramid.ini).
 
@@ -41,14 +41,18 @@ def config_from_cwconfig(cwconfig, setti
     :returns: A Pyramid config object
     """
     settings = dict(settings) if settings else {}
-    settings.update(settings_from_cwconfig(cwconfig))
+    settings.update(settings_from_cwconfig(cwconfig, debugtoolbar=debugtoolbar))
     config = Configurator(settings=settings)
     config.registry['cubicweb.config'] = cwconfig
     config.include('cubicweb.pyramid')
+
+    if debugtoolbar:
+        config.include("pyramid_debugtoolbar")
+
     return config
 
 
-def settings_from_cwconfig(cwconfig):
+def settings_from_cwconfig(cwconfig, debugtoolbar=False):
     '''
     Extract settings from pyramid.ini and pyramid-debug.ini (if in debug)
 
@@ -82,7 +86,7 @@ def settings_from_cwconfig(cwconfig):
 
 def wsgi_application_from_cwconfig(
         cwconfig,
-        profile=False, profile_output=None, profile_dump_every=None):
+        profile=False, profile_output=None, profile_dump_every=None, debugtoolbar=False):
     """ Build a WSGI application from a cubicweb configuration
 
     :param cwconfig: A CubicWeb configuration
@@ -90,10 +94,12 @@ def wsgi_application_from_cwconfig(
     :param profile_output: Profiling output filename. See :ref:`profiling`.
     :param profile_dump_every: Profiling number of requests before dumping the
                                stats. See :ref:`profiling`.
+    :param debugtoolbar: Activate pyramid debugtoolbar when True.
 
     :returns: A fully operationnal WSGI application
     """
-    config = config_from_cwconfig(cwconfig)
+    config = config_from_cwconfig(cwconfig, debugtoolbar=debugtoolbar)
+
     profile = profile or asbool(config.registry.settings.get(
         'cubicweb.profile.enable', False))
     if profile:
diff --git a/cubicweb/pyramid/pyramidctl.py b/cubicweb/pyramid/pyramidctl.py
--- a/cubicweb/pyramid/pyramidctl.py
+++ b/cubicweb/pyramid/pyramidctl.py
@@ -99,6 +99,10 @@ class PyramidStartHandler(InstanceComman
         ('debug',
          {'short': 'D', 'action': 'store_true',
           'help': 'Equals to "--debug-mode --reload"'}),
+        ('toolbar',
+         {'short': 't', 'action': 'store_true',
+          'help': 'Activate the pyramid debug toolbar'
+                  '(the pypi "pyramid_debugtoolbar" package must be installed)'}),
         ('reload',
          {'action': 'store_true',
           'help': 'Restart the server if any source file is changed'}),
@@ -255,10 +259,23 @@ class PyramidStartHandler(InstanceComman
         if self['loglevel'] is None and self['debug']:
             init_cmdline_log_threshold(self.cwconfig, 'debug')
 
+        # if the debugtoolbar is activated, test if it's importable
+        if self['toolbar']:
+            try:
+                import pyramid_debugtoolbar  # noqa
+            except ImportError:
+                print("Error: you've tried to activate the pyramid debugtoolbar but it failed to "
+                      "import, make sure it's correctly installed by doing a "
+                      "'pip install pyramid_debugtoolbar'.\nYou can find more information on the "
+                      "official documentation: "
+                      "https://docs.pylonsproject.org/projects/pyramid_debugtoolbar/en/latest/")
+                sys.exit(1)
+
         app = wsgi_application_from_cwconfig(
             cwconfig, profile=self['profile'],
             profile_output=self['profile-output'],
-            profile_dump_every=self['profile-dump-every']
+            profile_dump_every=self['profile-dump-every'],
+            debugtoolbar=self['toolbar']
         )
 
         host = cwconfig['interface']
diff --git a/doc/changes/3.27.rst b/doc/changes/3.27.rst
--- a/doc/changes/3.27.rst
+++ b/doc/changes/3.27.rst
@@ -30,6 +30,8 @@ New features
 * add a new migration function `update_bfss_path(old_path, new_path)` to update
   the path in Bytes File-System Storage (bfss).
 
+* add a new '-t/--toolbar' option the pyramid command to activate the pyramid debugtoolbar
+
 Backwards incompatible changes
 ------------------------------
 
@@ -61,6 +63,9 @@ Backwards incompatible changes
 
 * DBG_MS flag has been removed since it is not used anymore
 
+* the -D option of `cubicweb-ctl pyramid` will now activate the pyramid
+  debugtoolbar if it's installed
+
 Deprecated code drops
 ---------------------
 



More information about the cubicweb-devel mailing list