cubicweb/pyramid/debug_source_code.py
author Laurent Peuch <cortex@worlddomination.be>
Tue, 08 Oct 2019 22:14:06 +0200
changeset 12771 70597b447fb7
child 12772 a2b8c201727f
permissions -rw-r--r--
[debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file This will be used for tool building for easier debugging. Closes #17256791
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12771
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     1
# copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     2
#
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     3
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     4
#
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     5
# This file is part of CubicWeb.
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     6
#
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     7
# CubicWeb is free software: you can redistribute it and/or modify it under the
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     8
# terms of the GNU Lesser General Public License as published by the Free
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
     9
# Software Foundation, either version 2.1 of the License, or (at your option)
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    10
# any later version.
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    11
#
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    12
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    13
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    14
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    15
# details.
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    16
#
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    17
# You should have received a copy of the GNU Lesser General Public License along
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    18
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    19
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    20
"""
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    21
Debug view for pyramid debug toolbar and others to help development
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    22
"""
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    23
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    24
import os
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    25
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    26
from pyramid.response import Response
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    27
from mako.template import Template
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    28
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    29
from cubicweb.misc.source_highlight import highlight_html, generate_css, has_pygments
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    30
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    31
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    32
DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code'
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    33
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    34
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    35
def debug_display_source_code(request):
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    36
    """
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    37
    This view display a python source file content for making debugging easier.
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    38
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    39
    It is only activated on debug mode (-D) for pyramid during development.
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    40
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    41
    It will uses pygment if installed to colorize the source code.
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    42
    """
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    43
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    44
    if "file" not in request.params:
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    45
        return Response('Error: you should have end up on this page following a link in the '
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    46
                        'pyramid debug toolbar with the correct parameters.')
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    47
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    48
    source_code_file = request.params["file"]
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    49
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    50
    if not os.path.exists(source_code_file):
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    51
        return Response("Error: file '%s' doesn't exist on the filesystem." % source_code_file)
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    52
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    53
    try:
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    54
        content = open(source_code_file, "r").read()
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    55
    except Exception as e:
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    56
        return Response("Error: while opening file '%s' got the error: %s" % (source_code_file, e))
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    57
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    58
    lines = []
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    59
    line_begin = request.params.get("line", None)
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    60
    if line_begin:
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    61
        line_end = request.params.get("end", line_begin)
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    62
        lines = list(range(int(line_begin), int(line_end) + 1))
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    63
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    64
    this_file_directory = os.path.split(os.path.realpath(__file__))[0]
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    65
    template_filename = os.path.join(this_file_directory,
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    66
                                     "debug_toolbar_templates/debug_source_code.mako")
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    67
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    68
    html = Template(filename=template_filename).render(
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    69
        file_path=source_code_file,
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    70
        content=content,
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    71
        has_pygments=has_pygments,
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    72
        highlight_html=highlight_html,
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    73
        css=generate_css(),
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    74
        lines=lines,
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    75
    )
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    76
70597b447fb7 [debug-toolbar/display_source_code] add a pyramid view that display syntax highlighted python source file
Laurent Peuch <cortex@worlddomination.be>
parents:
diff changeset
    77
    return Response(html)