cubicweb/pyramid/debug_source_code.py
author Laurent Peuch <cortex@worlddomination.be>
Tue, 26 Nov 2019 16:03:06 +0100
changeset 12774 e6bf15a69ea0
parent 12773 3a38f779bed5
child 12775 38f22b8b4459
permissions -rw-r--r--
[debug-toolbar/display_source_code] add function to generate html link to source code
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
12772
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    25
import logging
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    26
import inspect
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
    27
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
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
    29
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
    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
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
    32
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
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
    35
12773
3a38f779bed5 [debug-toolbar/display_source_code/security] add security mechanism to only read whitelisted files
Laurent Peuch <cortex@worlddomination.be>
parents: 12772
diff changeset
    36
FILES_WHITE_LIST = set()
3a38f779bed5 [debug-toolbar/display_source_code/security] add security mechanism to only read whitelisted files
Laurent Peuch <cortex@worlddomination.be>
parents: 12772
diff changeset
    37
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
    38
12774
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    39
def _generate_link_to_source(file_path, start=None, end=None, tag_body="&lt;&gt;"):
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    40
    if start:
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    41
        # step back a bit so we have a bit of top padding wen displaying the page
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    42
        # and the highlighted line isn't glued to top of the browser window
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    43
        line_anchor = max(0, start - 10)
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    44
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    45
        if end:
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    46
            return '<a href="../%s?file=%s&line=%s&end=%s#line-%s" target="_blank">%s</a>' % (
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    47
                DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, start, end, line_anchor, tag_body
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    48
            )
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    49
        else:
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    50
            return '<a href="../%s?file=%s&line=%s#line-%s" target="_blank">%s</a>' % (
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    51
                DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, start, line_anchor, tag_body
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    52
            )
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    53
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    54
    return '<a href="../%s?file=%s" target="_blank">%s</a>' % (
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    55
        DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, tag_body
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    56
    )
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    57
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    58
12772
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    59
def source_code_url(object_or_class):
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    60
    if object_or_class is None:
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    61
        return ""
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    62
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    63
    if not inspect.isclass(object_or_class):
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    64
        object_or_class = object_or_class.__class__
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    65
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    66
    try:
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    67
        file_path = inspect.getsourcefile(object_or_class)
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    68
    except TypeError:
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    69
        logging.debug("Error while trying to source code of '%s'" % object_or_class)
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    70
        return ""
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    71
12773
3a38f779bed5 [debug-toolbar/display_source_code/security] add security mechanism to only read whitelisted files
Laurent Peuch <cortex@worlddomination.be>
parents: 12772
diff changeset
    72
    FILES_WHITE_LIST.add(file_path)
3a38f779bed5 [debug-toolbar/display_source_code/security] add security mechanism to only read whitelisted files
Laurent Peuch <cortex@worlddomination.be>
parents: 12772
diff changeset
    73
12772
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    74
    try:
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    75
        source_code, line = inspect.getsourcelines(object_or_class)
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    76
    except OSError:  # when we couldn't read the source code/line
12774
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    77
        return _generate_link_to_source(file_path)
12772
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    78
12774
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    79
    return _generate_link_to_source(file_path, line, line + len(source_code))
12772
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    80
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    81
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
    82
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
    83
    """
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
    84
    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
    85
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
    86
    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
    87
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
    88
    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
    89
    """
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
    90
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
    91
    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
    92
        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
    93
                        '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
    94
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
    95
    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
    96
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
    97
    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
    98
        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
    99
12773
3a38f779bed5 [debug-toolbar/display_source_code/security] add security mechanism to only read whitelisted files
Laurent Peuch <cortex@worlddomination.be>
parents: 12772
diff changeset
   100
    # security
3a38f779bed5 [debug-toolbar/display_source_code/security] add security mechanism to only read whitelisted files
Laurent Peuch <cortex@worlddomination.be>
parents: 12772
diff changeset
   101
    if source_code_file not in FILES_WHITE_LIST:
3a38f779bed5 [debug-toolbar/display_source_code/security] add security mechanism to only read whitelisted files
Laurent Peuch <cortex@worlddomination.be>
parents: 12772
diff changeset
   102
        return Response("Error: access to file is not authorized")
3a38f779bed5 [debug-toolbar/display_source_code/security] add security mechanism to only read whitelisted files
Laurent Peuch <cortex@worlddomination.be>
parents: 12772
diff changeset
   103
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
   104
    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
   105
        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
   106
    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
   107
        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
   108
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
   109
    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
   110
    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
   111
    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
   112
        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
   113
        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
   114
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
   115
    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
   116
    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
   117
                                     "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
   118
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
   119
    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
   120
        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
   121
        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
   122
        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
   123
        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
   124
        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
   125
        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
   126
    )
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
   127
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
   128
    return Response(html)