import repository from arizona
[raven.git] / lib / ravenlib / tableformat.py
1 class TableFormatterBase:
2     def __init__(self, stream):
3         self.stream = stream
4
5     def start(self):
6         pass
7
8     def stop(self):
9         pass
10
11     def header_row_start(self):
12         self.row_start()
13
14     def header_row_stop(self):
15         self.row_stop()
16
17     def row_start(self):
18         pass
19
20     def row_stop(self):
21         pass
22
23     def column_separator(self):
24         pass
25
26     def column(self, name, data, maxwidth, just="r"):
27         pass
28
29     def header(self, name, maxwidth, just="c"):
30         self.column(name, name.upper(), maxwidth, just)
31
32 class TableFormatterAscii(TableFormatterBase):
33     def row_stop(self):
34         self.stream.write("\n")
35
36     def column(self, name, data, maxwidth, just):
37         if (just == "r"):
38             self.stream.write(str(data).rjust(maxwidth) + " ")
39         elif (just == "c"):
40             self.stream.write(str(data).center(maxwidth) + " ")
41         else:
42             self.stream.write(str(data).ljust(maxwidth) + " ")
43
44 class TableFormatterCSV(TableFormatterBase):
45     def row_stop(self):
46         self.stream.write("\n")
47
48     def column(self, name, data, maxwidth, just):
49         self.stream.write(str(data))
50
51     def column_separator(self):
52         self.stream.write(",")
53
54
55 class TableFormatterHtml(TableFormatterBase):
56     def start(self):
57         self.stream.write("<TABLE border=1>")
58
59     def stop(self):
60         self.stream.write("</TABLE>")
61
62     def row_start(self):
63         self.stream.write("<TR>")
64
65     def row_stop(self):
66         self.stream.write("</TR>")
67
68     def column(self, name, data, maxwidth, just="r"):
69         if (just=="r"):
70             self.stream.write("<TD align=right>" + str(data) + "</TD>")
71         else:
72             self.stream.write("<TD>" + str(data) + "</TD>")
73
74     def header(self, name, maxwidth, just="r"):
75         self.stream.write("<TH>" + str(name) + "</TH>")
76