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 = """ %s %s """ 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 += "

%s

\n" % label def paragraph(self, text): self.content += "

%s

\n" % text def list(self, entries): self.content += "\n" def table(self, headers, data): self.content += "" for h in headers: self.content += "" % h self.content += "\n" for row in data: self.content += "" for i, val in enumerate(row): if i == 0: self.content += "" % val else: self.content += "" % val self.content += "\n" self.content += "
%s
%s%s
\n" def graph(self, label, graph_id, data): self.content += '
' % graph_id js_array = "var %s = %s;" % (graph_id, data) graph_command = JqueryTemplate % (graph_id, graph_id, label) self.content += '' % (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)