cubicweb/pyramid/debug_source_code.py
changeset 12772 a2b8c201727f
parent 12771 70597b447fb7
child 12773 3a38f779bed5
equal deleted inserted replaced
12771:70597b447fb7 12772:a2b8c201727f
    20 """
    20 """
    21 Debug view for pyramid debug toolbar and others to help development
    21 Debug view for pyramid debug toolbar and others to help development
    22 """
    22 """
    23 
    23 
    24 import os
    24 import os
       
    25 import logging
       
    26 import inspect
    25 
    27 
    26 from pyramid.response import Response
    28 from pyramid.response import Response
    27 from mako.template import Template
    29 from mako.template import Template
    28 
    30 
    29 from cubicweb.misc.source_highlight import highlight_html, generate_css, has_pygments
    31 from cubicweb.misc.source_highlight import highlight_html, generate_css, has_pygments
    30 
    32 
    31 
    33 
    32 DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code'
    34 DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code'
       
    35 
       
    36 
       
    37 def source_code_url(object_or_class):
       
    38     if object_or_class is None:
       
    39         return ""
       
    40 
       
    41     if not inspect.isclass(object_or_class):
       
    42         object_or_class = object_or_class.__class__
       
    43 
       
    44     try:
       
    45         file_path = inspect.getsourcefile(object_or_class)
       
    46     except TypeError:
       
    47         logging.debug("Error while trying to source code of '%s'" % object_or_class)
       
    48         return ""
       
    49 
       
    50     try:
       
    51         source_code, line = inspect.getsourcelines(object_or_class)
       
    52     except OSError:  # when we couldn't read the source code/line
       
    53         return '<a href="../%s?file=%s" target="_blank">&lt;&gt;</a>' % (
       
    54             DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path
       
    55         )
       
    56 
       
    57     # step back a bit so we have a bit of top padding wen displaying the page
       
    58     # and the highlighted line isn't glued to top of the browser window
       
    59     line_anchor = max(0, line - 10)
       
    60 
       
    61     return '<a href="../%s?file=%s&line=%s&end=%s#line-%s" target="_blank">&lt;&gt;</a>' % (
       
    62         DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, line, line + len(source_code), line_anchor
       
    63     )
    33 
    64 
    34 
    65 
    35 def debug_display_source_code(request):
    66 def debug_display_source_code(request):
    36     """
    67     """
    37     This view display a python source file content for making debugging easier.
    68     This view display a python source file content for making debugging easier.