1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
"""HTML reporting for coverage.py"""
import os
from coverage.report import Reporter
from coverage.templite import Templite
class HtmlReporter(Reporter):
"""HTML reporting.
"""
def __init__(self, coverage, ignore_errors=False):
super(HtmlReporter, self).__init__(coverage, ignore_errors)
self.directory = None
self.source_tmpl = Templite(SOURCE, globals())
def report(self, morfs, directory=None, omit_prefixes=None):
assert directory, "must provide a directory for html reporting"
self.report_files(self.html_file, morfs, directory, omit_prefixes)
def html_file(self, cu, statements, excluded, missing):
"""Generate an HTML file for one source file."""
lines = []
source = cu.source_file()
for lineno, line in enumerate(source.readlines()):
lineno += 1
css_class = ""
if lineno in statements:
css_class += " s"
if lineno not in missing and lineno not in excluded:
css_class += " r"
if lineno in excluded:
css_class += " x"
if lineno in missing:
css_class += " m"
lineinfo = {
'text': line,
'number': lineno,
'class': css_class.strip() or "p"
}
lines.append(lineinfo)
html_filename = os.path.join(self.directory, cu.flat_rootname()) + ".html"
fhtml = open(html_filename, 'w')
fhtml.write(self.source_tmpl.render(locals()))
fhtml.close()
# Helpers for templates
def escape(t):
"""HTML-escape the text in t."""
return (
t.replace("&", "&").replace("<", "<").replace(">", ">")
.replace("'", "'").replace('"', """)
.replace(" ", " ")
)
def not_empty(t):
"""Make sure HTML content is not completely empty."""
return t or " "
# Templates
SOURCE = """\
<html>
<head>
<title>Coverage of {{cu.filename|escape}}</title>
<style>
* {
font-size: 11pt;
line-height: 1.1em;
}
.linenos {
background: #eee;
}
.linenos p {
text-align: right;
margin: 0;
padding: 0 .5em 0 0;
font-family: verdana, sans-serif;
}
.source p {
margin: 0;
padding: 0 0 0 .5em;
font-family: "courier new", monospace;
}
.linenos p.m {
background: #fcc;
}
.linenos p.r {
background: #cfc;
}
.linenos p.x {
background: #ddd;
}
.source p.m {
background: #fee;
}
.source p.r {
background: #efe;
}
.source p.x {
background: #eee;
}
</style>
</head>
<body>
<table cellspacing='0' cellpadding='0'>
<tr>
<td class='linenos' valign='top'>
{% for line in lines %}
<p class='{{line.class}}'>{{line.number}}</p>
{% endfor %}
</td>
<td class='source' valign='top'>
{% for line in lines %}
<p class='{{line.class}}'>{{line.text.rstrip|escape|not_empty}}</p>
{% endfor %}
</td>
</tr>
</table>
</body>
</html>
"""
|