cubicweb/pyramid/debug_source_code.py
author Laurent Peuch <cortex@worlddomination.be>
Thu, 05 Dec 2019 02:08:01 +0100
changeset 12794 9c6dc1b071e3
parent 12775 38f22b8b4459
permissions -rw-r--r--
[debug-toolbar/display_source_code] change link display from "<>" to "[source]" It is way more explicit and understandable and doesn't take that much place.
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
12775
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    28
from itertools import dropwhile
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    29
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
    30
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
    31
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
    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
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
    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
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
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
    37
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
    38
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
    39
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
    40
12794
9c6dc1b071e3 [debug-toolbar/display_source_code] change link display from "<>" to "[source]"
Laurent Peuch <cortex@worlddomination.be>
parents: 12775
diff changeset
    41
def _generate_link_to_source(file_path, start=None, end=None, tag_body="[source]"):
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
    42
    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
    43
        # 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
    44
        # 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
    45
        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
    46
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
        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
    48
            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
    49
                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
    50
            )
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
        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
    52
            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
    53
                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
    54
            )
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
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
    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
    57
        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
    58
    )
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    59
e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
Laurent Peuch <cortex@worlddomination.be>
parents: 12773
diff changeset
    60
12772
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    61
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
    62
    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
    63
        return ""
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    64
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    65
    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
    66
        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
    67
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    68
    try:
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    69
        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
    70
    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
    71
        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
    72
        return ""
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    73
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
    74
    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
    75
12772
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    76
    try:
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    77
        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
    78
    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
    79
        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
    80
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
    81
    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
    82
a2b8c201727f [debug-toolbar/display_source_code] add helper to render link to source file
Laurent Peuch <cortex@worlddomination.be>
parents: 12771
diff changeset
    83
12775
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    84
def source_code_url_in_stack(stack, highlighted=False):
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    85
    new_stack = []
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    86
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    87
    if highlighted:
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    88
        for i in stack.split(" File "):
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    89
            # expecting this format:
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    90
            # '<span class="nb">&quot;/path/to/file.py&quot;</span>,
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    91
            # line <span class="m">885</span>,...'
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    92
            if not i.startswith('<span class="nb">'):
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    93
                new_stack.append(i)
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    94
                continue
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    95
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    96
            # this will give:
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    97
            # ['<span class="nb">', '/file/to/path.py', '</span>, ...']
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    98
            tag, file_path, rest = i.split("&quot;", 2)
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
    99
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   100
            # "rest" is like that: '</span>, line <span class="m">885</span>, ...'
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   101
            # we want to extrait "885" here
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   102
            line_number = int("".join(dropwhile(lambda x: not x.isdigit(), rest)).split("<")[0])
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   103
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   104
            new_stack.append("%s%s%s" % (
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   105
                tag,
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   106
                _generate_link_to_source(file_path, start=line_number,
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   107
                                         tag_body="&quot;%s&quot;" % file_path),
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   108
                rest,
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   109
            ))
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   110
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   111
            FILES_WHITE_LIST.add(file_path)
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   112
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   113
        new_stack = " File ".join(new_stack)
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   114
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   115
    # no syntax
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   116
    else:
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   117
        for i in stack.split("\n"):
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   118
            # expecting this format:
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   119
            # File "/path/to/file.py", line 885, in stuf\n  some_code\nFile "/stuff.py", line...
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   120
            if not i.startswith("  File "):
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   121
                new_stack.append(i)
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   122
                continue
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   123
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   124
            # this will give:
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   125
            # ['File "', '/path/to/file.py', '", line 885, in stuf']
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   126
            beginning, file_path, rest = i.split('"', 2)
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   127
            line_number = int("".join(dropwhile(lambda x: not x.isdigit(), rest)).split(",")[0])
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   128
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   129
            new_stack.append("%s%s%s" % (
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   130
                beginning,
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   131
                _generate_link_to_source(file_path, start=line_number,
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   132
                                         tag_body='"%s"' % file_path),
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   133
                rest,
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   134
            ))
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   135
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   136
            FILES_WHITE_LIST.add(file_path)
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   137
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   138
        new_stack = "\n".join(new_stack)
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   139
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   140
    return new_stack
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   141
38f22b8b4459 [debug-toolbar/display_source_code] add a function to add links to source file in traceback
Laurent Peuch <cortex@worlddomination.be>
parents: 12774
diff changeset
   142
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
   143
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
   144
    """
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
   145
    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
   146
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
   147
    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
   148
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
   149
    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
   150
    """
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
   151
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
   152
    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
   153
        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
   154
                        '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
   155
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
   156
    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
   157
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
   158
    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
   159
        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
   160
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
   161
    # 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
   162
    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
   163
        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
   164
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
   165
    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
   166
        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
   167
    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
   168
        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
   169
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
   170
    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
   171
    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
   172
    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
   173
        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
   174
        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
   175
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
   176
    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
   177
    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
   178
                                     "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
   179
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
   180
    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
   181
        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
   182
        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
   183
        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
   184
        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
   185
        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
   186
        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
   187
    )
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
   188
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
   189
    return Response(html)