# HG changeset patch # User Laurent Peuch # Date 1569560388 -7200 # Node ID 38f22b8b445968dea3da29d28054779553c2583d # Parent e6bf15a69ea01aab5ad85337fdd5c989db6ca143 [debug-toolbar/display_source_code] add a function to add links to source file in traceback Closes #17256791 diff -r e6bf15a69ea0 -r 38f22b8b4459 cubicweb/pyramid/debug_source_code.py --- a/cubicweb/pyramid/debug_source_code.py Tue Nov 26 16:03:06 2019 +0100 +++ 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 mako.template import Template @@ -79,6 +81,65 @@ return _generate_link_to_source(file_path, line, line + len(source_code)) +def source_code_url_in_stack(stack, highlighted=False): + new_stack = [] + + if highlighted: + for i in stack.split(" File "): + # expecting this format: + # '"/path/to/file.py", + # line 885,...' + if not i.startswith(''): + new_stack.append(i) + continue + + # this will give: + # ['', '/file/to/path.py', ', ...'] + tag, file_path, rest = i.split(""", 2) + + # "rest" is like that: ', line 885, ...' + # we want to extrait "885" here + line_number = int("".join(dropwhile(lambda x: not x.isdigit(), rest)).split("<")[0]) + + new_stack.append("%s%s%s" % ( + tag, + _generate_link_to_source(file_path, start=line_number, + tag_body=""%s"" % file_path), + rest, + )) + + FILES_WHITE_LIST.add(file_path) + + new_stack = " File ".join(new_stack) + + # no syntax + else: + for i in stack.split("\n"): + # expecting this format: + # File "/path/to/file.py", line 885, in stuf\n some_code\nFile "/stuff.py", line... + if not i.startswith(" File "): + new_stack.append(i) + continue + + # this will give: + # ['File "', '/path/to/file.py', '", line 885, in stuf'] + beginning, file_path, rest = i.split('"', 2) + line_number = int("".join(dropwhile(lambda x: not x.isdigit(), rest)).split(",")[0]) + + new_stack.append("%s%s%s" % ( + beginning, + _generate_link_to_source(file_path, start=line_number, + tag_body='"%s"' % file_path), + rest, + )) + + FILES_WHITE_LIST.add(file_path) + + 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.