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
|
class ConsoleWriter(object):
def section(self, label):
print "\n----- %s -----" % label
def paragraph(self, text):
print text
def list(self, entries):
for e in entries:
print "- %s" % e
def table(self, headers, data):
for row in data:
label = row[0]
if label:
print "\n%s:" % label
for h, d in zip(headers, row[1:]):
print " - %s: %s" % (h, d)
def graph(self, *args, **kwds):
pass
def close(self):
pass
HtmlTemplate = """
<html>
<head>
<title>%s</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://dump.fm/static/js/jquery.flot.js"></script>
<style>
table {
margin: 0;
cellpadding: 1px;
}
th { text-align: left; }
tr:hover { background-color: #AFF; }
</style>
</head>
<body>%s</body>
</html>
"""
JqueryTemplate = """
$(function () {
$.plot($("#%s"),
[ { data: %s, label: "%s" } ],
{ xaxis: { mode: 'time' },
yaxis: { min: 0 },
legend: { position: 'sw' } });
});
"""
class HtmlWriter(object):
def __init__(self, path, title):
self.path = path
self.title = title
self.content = ""
def section(self, label):
self.content += "<h2>%s</h2>\n" % label
def paragraph(self, text):
self.content += "<p>%s</p>\n" % text
def list(self, entries):
self.content += "<ul>\n"
for e in entries:
self.content += "<li>%s</li>\n" % e
self.content += "</ul>\n"
def table(self, headers, data):
self.content += "<table><tr><th></th>"
for h in headers:
self.content += "<th>%s</th>" % h
self.content += "</tr>\n"
for row in data:
self.content += "<tr>"
for i, val in enumerate(row):
if i == 0:
self.content += "<th>%s</th>" % val
else:
self.content += "<td>%s</td>" % val
self.content += "</tr>\n"
self.content += "</table>\n"
def graph(self, label, graph_id, data):
self.content += '<div id="%s" style="width: 1000px; height: 350px;"></div>' % graph_id
js_array = "var %s = %s;" % (graph_id, data)
graph_command = JqueryTemplate % (graph_id, graph_id, label)
self.content += '<script>%s\n%s</script>' % (js_array, graph_command)
def _assemble(self):
return HtmlTemplate % (self.title or 'Untitled', self.content)
def close(self):
print "writing report to %s" % self.path
text = self._assemble()
with open(self.path, 'w') as f:
f.write(text)
|