diff options
| author | Scott Ostler <scottbot9000@gmail.com> | 2010-07-08 19:37:38 -0400 |
|---|---|---|
| committer | Scott Ostler <scottbot9000@gmail.com> | 2010-07-08 19:37:38 -0400 |
| commit | 88fb87e7c2d72db2c5f7b3e7a1ee41f021e5c40d (patch) | |
| tree | 0c7680d662c4138cd9ef84d6d6e6fe8ce19cc1df /scripts/writer.py | |
| parent | ae77b19680b6b83d93d5951108029c4889c5868a (diff) | |
logreport.py rewritten to use generators
Diffstat (limited to 'scripts/writer.py')
| -rw-r--r-- | scripts/writer.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/scripts/writer.py b/scripts/writer.py new file mode 100644 index 0000000..df36b50 --- /dev/null +++ b/scripts/writer.py @@ -0,0 +1,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) |
