|
1 # copyright 2019 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # |
|
3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
4 # |
|
5 # This file is part of CubicWeb. |
|
6 # |
|
7 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
8 # terms of the GNU Lesser General Public License as published by the Free |
|
9 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
10 # any later version. |
|
11 # |
|
12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
15 # details. |
|
16 # |
|
17 # You should have received a copy of the GNU Lesser General Public License along |
|
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
19 |
|
20 """ |
|
21 Debug view for pyramid debug toolbar and others to help development |
|
22 """ |
|
23 |
|
24 import os |
|
25 |
|
26 from pyramid.response import Response |
|
27 from mako.template import Template |
|
28 |
|
29 from cubicweb.misc.source_highlight import highlight_html, generate_css, has_pygments |
|
30 |
|
31 |
|
32 DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code' |
|
33 |
|
34 |
|
35 def debug_display_source_code(request): |
|
36 """ |
|
37 This view display a python source file content for making debugging easier. |
|
38 |
|
39 It is only activated on debug mode (-D) for pyramid during development. |
|
40 |
|
41 It will uses pygment if installed to colorize the source code. |
|
42 """ |
|
43 |
|
44 if "file" not in request.params: |
|
45 return Response('Error: you should have end up on this page following a link in the ' |
|
46 'pyramid debug toolbar with the correct parameters.') |
|
47 |
|
48 source_code_file = request.params["file"] |
|
49 |
|
50 if not os.path.exists(source_code_file): |
|
51 return Response("Error: file '%s' doesn't exist on the filesystem." % source_code_file) |
|
52 |
|
53 try: |
|
54 content = open(source_code_file, "r").read() |
|
55 except Exception as e: |
|
56 return Response("Error: while opening file '%s' got the error: %s" % (source_code_file, e)) |
|
57 |
|
58 lines = [] |
|
59 line_begin = request.params.get("line", None) |
|
60 if line_begin: |
|
61 line_end = request.params.get("end", line_begin) |
|
62 lines = list(range(int(line_begin), int(line_end) + 1)) |
|
63 |
|
64 this_file_directory = os.path.split(os.path.realpath(__file__))[0] |
|
65 template_filename = os.path.join(this_file_directory, |
|
66 "debug_toolbar_templates/debug_source_code.mako") |
|
67 |
|
68 html = Template(filename=template_filename).render( |
|
69 file_path=source_code_file, |
|
70 content=content, |
|
71 has_pygments=has_pygments, |
|
72 highlight_html=highlight_html, |
|
73 css=generate_css(), |
|
74 lines=lines, |
|
75 ) |
|
76 |
|
77 return Response(html) |