[pyramid/debugtoolbar] add a pyramid view that display syntax highlighted python source file draft
authorLaurent Peuch <cortex@worlddomination.be>
Tue, 08 Oct 2019 22:14:06 +0200
changeset 13431 5a334735a4eb
parent 13430 e3e39e090243
child 13432 20c33beec0e8
[pyramid/debugtoolbar] add a pyramid view that display syntax highlighted python source file This will be used for tool building for easier debugging.
cubicweb/pyramid/__init__.py
cubicweb/pyramid/debug_source_code.py
--- a/cubicweb/pyramid/__init__.py	Wed Sep 25 05:42:47 2019 +0200
+++ b/cubicweb/pyramid/__init__.py	Tue Oct 08 22:14:06 2019 +0200
@@ -28,6 +28,8 @@
 import wsgicors
 
 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
+from cubicweb.pyramid.debug_source_code import debug_display_source_code, DEBUG_DISPLAY_SOURCE_CODE_PATH
+
 from pyramid.config import Configurator
 from pyramid.exceptions import ConfigurationError
 from pyramid.settings import asbool, aslist
@@ -109,6 +111,11 @@
         config.add_route('profile_ping', '_profile/ping')
         config.add_route('profile_cnx', '_profile/cnx')
         config.scan('cubicweb.pyramid.profile')
+
+    if debugtoolbar:
+        config.add_route('debug_display_source_code', DEBUG_DISPLAY_SOURCE_CODE_PATH)
+        config.add_view(debug_display_source_code, route_name='debug_display_source_code')
+
     app = config.make_wsgi_app()
     # This replaces completely web/cors.py, which is not used by
     # cubicweb.pyramid anymore
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cubicweb/pyramid/debug_source_code.py	Tue Oct 08 22:14:06 2019 +0200
@@ -0,0 +1,133 @@
+# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+#
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of CubicWeb.
+#
+# CubicWeb is free software: you can redistribute it and/or modify it under the
+# terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option)
+# any later version.
+#
+# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
+
+"""
+Debug view for pyramid debug toolbar and others to help development
+"""
+
+import os
+
+from pyramid.response import Response
+
+from cubicweb.misc.source_highlight import highlight_html, generate_css, has_pygments
+
+
+DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code'
+
+
+def debug_display_source_code(request):
+    """
+    This view display a python source file content for making debugging easier.
+
+    It is only activated on debug mode (-D) for pyramid during development.
+
+    It will uses pygment if installed to colorize the source code.
+    """
+
+    if "file" not in request.params:
+        return Response('Error: you should have end up on this page following a link in the pyramid debug toolbar with the correct parameters.')
+
+    source_code_file = request.params["file"]
+
+    if not os.path.exists(source_code_file):
+        return Response("Error: file '%s' doesn't exist on the filesystem." % source_code_file)
+
+    try:
+        content = open(source_code_file, "r").read()
+    except Exception as e:
+        return Response("Error: while opening file '%s' got the error: %s" % (source_code_file, e))
+
+    if "line" in request.params:
+        if "end" in request.params:
+            line = list(range(int(request.params["line"]), int(request.params["end"]) + 1))
+        else:
+            line = [int(request.params["line"])]
+    else:
+        line = []
+
+    css = generate_css()
+
+    if not has_pygments:
+        html_content = '<table class="rawtable">\n'
+        for line_number, source_line in enumerate(content.split("\n"), start=1):
+            html_content += """
+            <tr>
+            <td class="line_number"><pre>%(line_number)s</pre></td>
+            <td><a class="line-%(line_number)s" name="line-%(line_number)s"><pre>%(line)s</pre></a></td>
+            </tr>
+            """ % {"line_number": line_number, "line": source_line.rstrip()}
+        html_content += "</table>\n"
+        content = html_content
+
+        if line:
+            css += """
+            .line-%s > pre {
+                background-color: #ffffcc;
+            }""" % line[0]
+    else:
+        content = highlight_html(content, "python", linenos=True, hl_lines=line, lineanchors="line")
+
+    html = """
+    <html>
+    <head><title>%(file_path)s</title></head>
+    <body>
+    <h2>%(file_path)s</h2>
+    %(content)s
+    <style>
+    h2 {
+        text-align: center;
+        width: 100%%;
+        color: #fefefe;
+        background-color: #333333;
+        padding: 10px;
+        font-family: sans;
+        margin: 0;
+    }
+    body {
+        margin: 0;
+    }
+    .highlighttable, .rawtable {
+        margin: auto;
+        font-size: larger;
+        border: 2px solid black;
+        border-top: 0;
+        border-bottom: 0;
+    }
+    .rawtable {
+        padding: 10px;
+    }
+    pre {
+        margin: 0;
+    }
+    .line_number {
+        text-align: right;
+    }
+    .rawtable td {
+        padding: 0;
+    }
+    .hll {
+        display: block;
+    }
+    %(css)s
+    </style>
+    </body>
+    </html>
+    """ % {"file_path": source_code_file, "content": content, "css": css}
+
+    return Response(html)