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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
"""HTML reporting for coverage.py"""
import os
from coverage import __version__ # pylint: disable-msg=W0611
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 += " stm"
if lineno not in missing and lineno not in excluded:
css_class += " run"
if lineno in excluded:
css_class += " exc"
if lineno in missing:
css_class += " mis"
lineinfo = {
'text': line,
'number': lineno,
'class': css_class.strip() or "pln"
}
lines.append(lineinfo)
html_filename = os.path.join(self.directory, cu.flat_rootname())
html_filename += ".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
# For making line numbers and text different fonts:
# http://24ways.org/2006/compose-to-a-vertical-rhythm
SOURCE = """\
<!doctype html PUBLIC "-//W3C//DTD html 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Coverage for {{cu.name|escape}}</title>
<style>
html, body {
margin: 0;
padding: 0;
font-size: 85%;
}
p {
margin: 0;
padding: 0;
}
#header {
background: #ffd472;
width: 100%;
font-family: verdana, sans-serif;
}
#source {
padding: 1em;
font-family: "courier new", monospace;
}
#footer {
background: #ffe9b8;
font-size: 85%;
font-family: verdana, sans-serif;
color: #666666;
font-style: italic;
}
.content {
padding: 1em;
}
a.nav {
text-decoration: none;
color: inherit;
}
a.nav:hover {
text-decoration: underline;
color: inherit;
}
.linenos {
background: #eeeeee;
}
.linenos p {
text-align: right;
margin: 0;
padding: 0 .5em 0 .5em;
color: #999999;
}
.text p {
margin: 0;
padding: 0 0 0 .5em;
white-space: nowrap;
}
.linenos p.mis {
background: #ffcccc;
}
.linenos p.run {
background: #ccffcc;
}
.linenos p.exc {
background: #e2e2e2;
}
.text p.mis {
background: #ffdddd;
}
.text p.run {
background: #ddffdd;
}
.text p.exc {
background: #eeeeee;
}
</style>
</head>
<body>
<div id='header'>
<div class='content'>
<p>Coverage for <b>{{cu.filename|escape}}</b></p>
</div>
</div>
<div id='source'>
<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='text' valign='top'>
{% for line in lines %}
<p class='{{line.class}}'>{{line.text.rstrip|escape|not_empty}}</p>
{% endfor %}
</td>
</tr>
</table>
</div>
<div id='footer'>
<div class='content'>
<p>
<a class='nav' href='http://bitbucket.org/ned/coveragepy/'>coverage.py v{{__version__}}</a>
</p>
</div>
</div>
</body>
</html>
"""
|