[debug-toolbar/display_source_code] add a function to add links to source file in traceback

Closes #17256791

authorLaurent Peuch <cortex@worlddomination.be>
changeset38f22b8b4459
branchdefault
phasepublic
hiddenno
parent revision#e6bf15a69ea0 [debug-toolbar/display_source_code] add function to generate html link to source code
child revision#3cbcbaead477 [debug-toolbar] link to source code of all objects/classes
files modified by this revision
cubicweb/pyramid/debug_source_code.py
# HG changeset patch
# User Laurent Peuch <cortex@worlddomination.be>
# Date 1569560388 -7200
# Fri Sep 27 06:59:48 2019 +0200
# Node ID 38f22b8b445968dea3da29d28054779553c2583d
# Parent e6bf15a69ea01aab5ad85337fdd5c989db6ca143
[debug-toolbar/display_source_code] add a function to add links to source file in traceback

Closes #17256791

diff --git a/cubicweb/pyramid/debug_source_code.py b/cubicweb/pyramid/debug_source_code.py
@@ -23,10 +23,12 @@
1 
2  import os
3  import logging
4  import inspect
5 
6 +from itertools import dropwhile
7 +
8  from pyramid.response import Response
9  from mako.template import Template
10 
11  from cubicweb.misc.source_highlight import highlight_html, generate_css, has_pygments
12 
@@ -77,10 +79,69 @@
13          return _generate_link_to_source(file_path)
14 
15      return _generate_link_to_source(file_path, line, line + len(source_code))
16 
17 
18 +def source_code_url_in_stack(stack, highlighted=False):
19 +    new_stack = []
20 +
21 +    if highlighted:
22 +        for i in stack.split(" File "):
23 +            # expecting this format:
24 +            # '<span class="nb">&quot;/path/to/file.py&quot;</span>,
25 +            # line <span class="m">885</span>,...'
26 +            if not i.startswith('<span class="nb">'):
27 +                new_stack.append(i)
28 +                continue
29 +
30 +            # this will give:
31 +            # ['<span class="nb">', '/file/to/path.py', '</span>, ...']
32 +            tag, file_path, rest = i.split("&quot;", 2)
33 +
34 +            # "rest" is like that: '</span>, line <span class="m">885</span>, ...'
35 +            # we want to extrait "885" here
36 +            line_number = int("".join(dropwhile(lambda x: not x.isdigit(), rest)).split("<")[0])
37 +
38 +            new_stack.append("%s%s%s" % (
39 +                tag,
40 +                _generate_link_to_source(file_path, start=line_number,
41 +                                         tag_body="&quot;%s&quot;" % file_path),
42 +                rest,
43 +            ))
44 +
45 +            FILES_WHITE_LIST.add(file_path)
46 +
47 +        new_stack = " File ".join(new_stack)
48 +
49 +    # no syntax
50 +    else:
51 +        for i in stack.split("\n"):
52 +            # expecting this format:
53 +            # File "/path/to/file.py", line 885, in stuf\n  some_code\nFile "/stuff.py", line...
54 +            if not i.startswith("  File "):
55 +                new_stack.append(i)
56 +                continue
57 +
58 +            # this will give:
59 +            # ['File "', '/path/to/file.py', '", line 885, in stuf']
60 +            beginning, file_path, rest = i.split('"', 2)
61 +            line_number = int("".join(dropwhile(lambda x: not x.isdigit(), rest)).split(",")[0])
62 +
63 +            new_stack.append("%s%s%s" % (
64 +                beginning,
65 +                _generate_link_to_source(file_path, start=line_number,
66 +                                         tag_body='"%s"' % file_path),
67 +                rest,
68 +            ))
69 +
70 +            FILES_WHITE_LIST.add(file_path)
71 +
72 +        new_stack = "\n".join(new_stack)
73 +
74 +    return new_stack
75 +
76 +
77  def debug_display_source_code(request):
78      """
79      This view display a python source file content for making debugging easier.
80 
81      It is only activated on debug mode (-D) for pyramid during development.