|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from datetime import datetime |
9 | | -import linecache |
10 | 9 | import os |
11 | 10 | import shlex |
12 | | -import re |
13 | 11 | import sys |
14 | | -from xml.sax.saxutils import escape |
15 | 12 | from urllib.parse import quote_plus as url_quote |
16 | | -import html |
17 | 13 | from . import __version__ |
18 | 14 |
|
19 | | -LINE_REFERENCE = re.compile(r"\<title\>TB@@([^:]+):(\d+)@@TB") |
20 | | - |
21 | 15 | DEBUGGING_INFO = url_quote( |
22 | 16 | f"""\ |
23 | 17 | ## Version information |
|
27 | 21 | ) |
28 | 22 |
|
29 | 23 |
|
30 | | -def replace_code_references(string: str) -> str: |
31 | | - """ |
32 | | - Replace occurrences of TB@@file.py:123@@TB with the line of code at that |
33 | | - location, XML quoted and slightly indented. |
34 | | - """ |
35 | | - |
36 | | - def replace_with_code(match): |
37 | | - filename, line = match.group(1, 2) |
38 | | - filename = html.unescape(filename) |
39 | | - line = int(line) |
40 | | - return "<title>    " + escape( |
41 | | - linecache.getline(filename, line).strip() |
42 | | - ) |
43 | | - |
44 | | - return re.sub(LINE_REFERENCE, replace_with_code, string) |
45 | | - |
46 | | - |
47 | | -def update_svg(svg_path: str): |
48 | | - """Fix up the SVGs. |
49 | | -
|
50 | | - 1. Add an appropriate subtitle. |
51 | | - 2. Add source code lines. |
52 | | - """ |
53 | | - with open(svg_path) as f: |
54 | | - data = f.read().replace( |
55 | | - "SUBTITLE-HERE", |
56 | | - """Made with the Fil memory profiler. <a href="https://pythonspeed.com/products/filmemoryprofiler/" style="text-decoration: underline;" target="_parent">Try it on your code!</a>""", |
57 | | - ) |
58 | | - data = replace_code_references(data) |
59 | | - with open(svg_path, "w") as f: |
60 | | - f.write(data) |
61 | | - |
62 | | - |
63 | 24 | def render_report(output_path: str, now: datetime) -> str: |
64 | 25 | """Write out the HTML index and improve the SVGs.""" |
65 | | - for svg_path in [ |
66 | | - os.path.join(output_path, "peak-memory.svg"), |
67 | | - os.path.join(output_path, "peak-memory-reversed.svg"), |
68 | | - ]: |
69 | | - update_svg(svg_path) |
70 | | - |
71 | 26 | index_path = os.path.join(output_path, "index.html") |
72 | 27 | with open(index_path, "w") as index: |
73 | 28 | index.write( |
|
0 commit comments