cubicweb/pyramid/debug_source_code.py
changeset 12774 e6bf15a69ea0
parent 12773 3a38f779bed5
child 12775 38f22b8b4459
equal deleted inserted replaced
12773:3a38f779bed5 12774:e6bf15a69ea0
    34 DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code'
    34 DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code'
    35 
    35 
    36 FILES_WHITE_LIST = set()
    36 FILES_WHITE_LIST = set()
    37 
    37 
    38 
    38 
       
    39 def _generate_link_to_source(file_path, start=None, end=None, tag_body="<>"):
       
    40     if start:
       
    41         # step back a bit so we have a bit of top padding wen displaying the page
       
    42         # and the highlighted line isn't glued to top of the browser window
       
    43         line_anchor = max(0, start - 10)
       
    44 
       
    45         if end:
       
    46             return '<a href="../%s?file=%s&line=%s&end=%s#line-%s" target="_blank">%s</a>' % (
       
    47                 DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, start, end, line_anchor, tag_body
       
    48             )
       
    49         else:
       
    50             return '<a href="../%s?file=%s&line=%s#line-%s" target="_blank">%s</a>' % (
       
    51                 DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, start, line_anchor, tag_body
       
    52             )
       
    53 
       
    54     return '<a href="../%s?file=%s" target="_blank">%s</a>' % (
       
    55         DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, tag_body
       
    56     )
       
    57 
       
    58 
    39 def source_code_url(object_or_class):
    59 def source_code_url(object_or_class):
    40     if object_or_class is None:
    60     if object_or_class is None:
    41         return ""
    61         return ""
    42 
    62 
    43     if not inspect.isclass(object_or_class):
    63     if not inspect.isclass(object_or_class):
    52     FILES_WHITE_LIST.add(file_path)
    72     FILES_WHITE_LIST.add(file_path)
    53 
    73 
    54     try:
    74     try:
    55         source_code, line = inspect.getsourcelines(object_or_class)
    75         source_code, line = inspect.getsourcelines(object_or_class)
    56     except OSError:  # when we couldn't read the source code/line
    76     except OSError:  # when we couldn't read the source code/line
    57         return '<a href="../%s?file=%s" target="_blank">&lt;&gt;</a>' % (
    77         return _generate_link_to_source(file_path)
    58             DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path
       
    59         )
       
    60 
    78 
    61     # step back a bit so we have a bit of top padding wen displaying the page
    79     return _generate_link_to_source(file_path, line, line + len(source_code))
    62     # and the highlighted line isn't glued to top of the browser window
       
    63     line_anchor = max(0, line - 10)
       
    64 
       
    65     return '<a href="../%s?file=%s&line=%s&end=%s#line-%s" target="_blank">&lt;&gt;</a>' % (
       
    66         DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, line, line + len(source_code), line_anchor
       
    67     )
       
    68 
    80 
    69 
    81 
    70 def debug_display_source_code(request):
    82 def debug_display_source_code(request):
    71     """
    83     """
    72     This view display a python source file content for making debugging easier.
    84     This view display a python source file content for making debugging easier.