[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

authorLaurent Peuch <cortex@worlddomination.be>
changeset30b205676182
branchdefault
phasepublic
hiddenno
parent revision#74b473f288d5 [changelog] add missing information to the 3.27 changelog
child revision#3cf37ec616ad [migrations/pdb] add to every failing migration operation a "p(db)" option
files modified by this revision
cubicweb/pyramid/__init__.py
cubicweb/pyramid/pyramidctl.py
doc/changes/3.27.rst
# HG changeset patch
# User Laurent Peuch <cortex@worlddomination.be>
# Date 1559154547 -7200
# Wed May 29 20:29:07 2019 +0200
# Node ID 30b205676182b2d1fdedd60c1098ad2fe2711bf7
# Parent 74b473f288d536a57dccbfbb7a2873bbd3bde3bb
[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
@@ -31,26 +31,30 @@
1  from pyramid.config import Configurator
2  from pyramid.exceptions import ConfigurationError
3  from pyramid.settings import asbool, aslist
4 
5 
6 -def config_from_cwconfig(cwconfig, settings=None):
7 +def config_from_cwconfig(cwconfig, settings=None, debugtoolbar=False):
8      """Return a Pyramid Configurator instance built from a CubicWeb config and
9      Pyramid-specific configuration files (pyramid.ini).
10 
11      :param cwconfig: A CubicWeb configuration
12      :returns: A Pyramid config object
13      """
14      settings = dict(settings) if settings else {}
15 -    settings.update(settings_from_cwconfig(cwconfig))
16 +    settings.update(settings_from_cwconfig(cwconfig, debugtoolbar=debugtoolbar))
17      config = Configurator(settings=settings)
18      config.registry['cubicweb.config'] = cwconfig
19      config.include('cubicweb.pyramid')
20 +
21 +    if debugtoolbar:
22 +        config.include("pyramid_debugtoolbar")
23 +
24      return config
25 
26 
27 -def settings_from_cwconfig(cwconfig):
28 +def settings_from_cwconfig(cwconfig, debugtoolbar=False):
29      '''
30      Extract settings from pyramid.ini and pyramid-debug.ini (if in debug)
31 
32      Can be used to configure middleware WSGI with settings from pyramid.ini files
33 
@@ -80,22 +84,24 @@
34      return settings
35 
36 
37  def wsgi_application_from_cwconfig(
38          cwconfig,
39 -        profile=False, profile_output=None, profile_dump_every=None):
40 +        profile=False, profile_output=None, profile_dump_every=None, debugtoolbar=False):
41      """ Build a WSGI application from a cubicweb configuration
42 
43      :param cwconfig: A CubicWeb configuration
44      :param profile: Enable profiling. See :ref:`profiling`.
45      :param profile_output: Profiling output filename. See :ref:`profiling`.
46      :param profile_dump_every: Profiling number of requests before dumping the
47                                 stats. See :ref:`profiling`.
48 +    :param debugtoolbar: Activate pyramid debugtoolbar when True.
49 
50      :returns: A fully operationnal WSGI application
51      """
52 -    config = config_from_cwconfig(cwconfig)
53 +    config = config_from_cwconfig(cwconfig, debugtoolbar=debugtoolbar)
54 +
55      profile = profile or asbool(config.registry.settings.get(
56          'cubicweb.profile.enable', False))
57      if profile:
58          config.add_route('profile_ping', '_profile/ping')
59          config.add_route('profile_cnx', '_profile/cnx')
diff --git a/cubicweb/pyramid/pyramidctl.py b/cubicweb/pyramid/pyramidctl.py
@@ -97,10 +97,14 @@
60            'help': 'Activate the repository debug mode ('
61                    'logs in the console and the debug toolbar).'}),
62          ('debug',
63           {'short': 'D', 'action': 'store_true',
64            'help': 'Equals to "--debug-mode --reload"'}),
65 +        ('toolbar',
66 +         {'short': 't', 'action': 'store_true',
67 +          'help': 'Activate the pyramid debug toolbar'
68 +                  '(the pypi "pyramid_debugtoolbar" package must be installed)'}),
69          ('reload',
70           {'action': 'store_true',
71            'help': 'Restart the server if any source file is changed'}),
72          ('reload-interval',
73           {'type': 'int', 'default': 1,
@@ -253,14 +257,27 @@
74 
75          # if no loglevel is specified and --debug is here, set log level at debug
76          if self['loglevel'] is None and self['debug']:
77              init_cmdline_log_threshold(self.cwconfig, 'debug')
78 
79 +        # if the debugtoolbar is activated, test if it's importable
80 +        if self['toolbar']:
81 +            try:
82 +                import pyramid_debugtoolbar  # noqa
83 +            except ImportError:
84 +                print("Error: you've tried to activate the pyramid debugtoolbar but it failed to "
85 +                      "import, make sure it's correctly installed by doing a "
86 +                      "'pip install pyramid_debugtoolbar'.\nYou can find more information on the "
87 +                      "official documentation: "
88 +                      "https://docs.pylonsproject.org/projects/pyramid_debugtoolbar/en/latest/")
89 +                sys.exit(1)
90 +
91          app = wsgi_application_from_cwconfig(
92              cwconfig, profile=self['profile'],
93              profile_output=self['profile-output'],
94 -            profile_dump_every=self['profile-dump-every']
95 +            profile_dump_every=self['profile-dump-every'],
96 +            debugtoolbar=self['toolbar']
97          )
98 
99          host = cwconfig['interface']
100          port = cwconfig['port'] or 8080
101          url_scheme = ('https' if cwconfig['base-url'].startswith('https')
diff --git a/doc/changes/3.27.rst b/doc/changes/3.27.rst
@@ -45,10 +45,12 @@
102      which otherwise is lost
103 
104  * on DBG_SQL and/or DBG_RQL, if pygments is installed, syntax highlight sql/rql
105    debug output
106 
107 +* add a new '-t/--toolbar' option the pyramid command to activate the pyramid debugtoolbar
108 +
109  Backwards incompatible changes
110  ------------------------------
111 
112  * Standardization on the way to launch a cubicweb instance, from now on the
113    only way to do that will be the used the ``pyramid`` command. Therefore: