[debug/display_source_code] add a function to add links to source file in traceback draft
authorLaurent Peuch <cortex@worlddomination.be>
Fri, 27 Sep 2019 06:59:48 +0200
changeset 13435 d0639b416403
parent 13434 e6150cd580e5
child 13436 5b957bd567da
[debug/display_source_code] add a function to add links to source file in traceback
cubicweb/pyramid/debug_source_code.py
--- a/cubicweb/pyramid/debug_source_code.py	Fri Sep 27 06:20:39 2019 +0200
+++ b/cubicweb/pyramid/debug_source_code.py	Fri Sep 27 06:59:48 2019 +0200
@@ -25,6 +25,8 @@
 import logging
 import inspect
 
+from itertools import dropwhile
+
 from pyramid.response import Response
 
 from cubicweb.misc.source_highlight import highlight_html, generate_css, has_pygments
@@ -64,6 +66,65 @@
     )
 
 
+def source_code_url_in_stack(stack):
+    new_stack = []
+
+    # with synthax highlight
+    if '<span class="nb">' in stack:
+        for i in stack.split(" File "):
+            if not i.startswith('<span class="nb">'):
+                new_stack.append(i)
+                continue
+
+            tag, file_path, rest = i.split("&quot;", 2)
+
+            line_number = int("".join(dropwhile(lambda x: not x.isdigit(), rest)).split("<")[0])
+
+            result = tag
+            result += '<a href="../%s?file=%s&line=%s#line-%s" target="_blank">' % (
+                DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, line_number, max(0, line_number - 10)
+            )
+            result += "&quot;"
+            result += file_path
+            result += "&quot;"
+            result += "</a>"
+            result += rest
+
+            FILES_WHITE_LIST.add(file_path)
+
+            new_stack.append(result)
+
+        new_stack = " File ".join(new_stack)
+
+    # no synthax
+    else:
+        for i in stack.split("\n"):
+            if not i.startswith("  File "):
+                new_stack.append(i)
+                continue
+
+            beginning, file_path, rest = i.split('"', 2)
+            line_number = int("".join(dropwhile(lambda x: not x.isdigit(), rest)).split(",")[0])
+
+            result = beginning
+            result += '<a href="../%s?file=%s&line=%s#line-%s" target="_blank">' % (
+                DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, line_number, max(0, line_number - 10)
+            )
+            result += '"'
+            result += file_path
+            result += '"'
+            result += "</a>"
+            result += rest
+
+            FILES_WHITE_LIST.add(file_path)
+
+            new_stack.append(result)
+
+        new_stack = "\n".join(new_stack)
+
+    return new_stack
+
+
 def debug_display_source_code(request):
     """
     This view display a python source file content for making debugging easier.