1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """Universal report objects and some formatting drivers.
19
20 A way to create simple reports using python objects, primarily designed to be
21 formatted as text and html.
22 """
23 from __future__ import generators
24 __docformat__ = "restructuredtext en"
25
26 import sys
27 from cStringIO import StringIO
28 from StringIO import StringIO as UStringIO
29
30 from logilab.common.textutils import linesep
31
32
34 """return an iterator on all children node of the given klass"""
35 for child in node.children:
36 if isinstance(child, klass):
37 yield child
38
39 for grandchild in get_nodes(child, klass):
40 yield grandchild
41
43 """try to return the layout's title as string, return None if not found
44 """
45 for child in layout.children:
46 if isinstance(child, Title):
47 return ' '.join([node.data for node in get_nodes(child, Text)])
48
50 """make a summary for the report, including X level"""
51 assert level > 0
52 level -= 1
53 summary = List(klass='summary')
54 for child in layout.children:
55 if not isinstance(child, Section):
56 continue
57 label = layout_title(child)
58 if not label and not child.id:
59 continue
60 if not child.id:
61 child.id = label.replace(' ', '-')
62 node = Link('#'+child.id, label=label or child.id)
63
64
65
66
67 if level and [n for n in child.children if isinstance(n, Section)]:
68 node = Paragraph([node, build_summary(child, level)])
69 summary.append(node)
70
71
72
73 return summary
74
75
77 """base class for ureport writers"""
78
96
103
105 """write a line in the output buffer"""
106 self.write(string + linesep)
107
108 - def write(self, string):
109 """write a string in the output buffer"""
110 try:
111 self.out.write(string)
112 except UnicodeEncodeError:
113 self.out.write(string.encode(self.encoding))
114
118
121
122 - def get_table_content(self, table):
123 """trick to get table content without actually writing it
124
125 return an aligned list of lists containing table cells values as string
126 """
127 result = [[]]
128 cols = table.cols
129 for cell in self.compute_content(table):
130 if cols == 0:
131 result.append([])
132 cols = table.cols
133 cols -= 1
134 result[-1].append(cell)
135
136 while len(result[-1]) < cols:
137 result[-1].append('')
138 return result
139
140 - def compute_content(self, layout):
141 """trick to compute the formatting of children layout before actually
142 writing it
143
144 return an iterator on strings (one for each child element)
145 """
146
147 def write(data):
148 try:
149 stream.write(data)
150 except UnicodeEncodeError:
151 stream.write(data.encode(self.encoding))
152 def writeln(data=''):
153 try:
154 stream.write(data+linesep)
155 except UnicodeEncodeError:
156 stream.write(data.encode(self.encoding)+linesep)
157 self.write = write
158 self.writeln = writeln
159 self.__compute_funcs.append((write, writeln))
160 for child in layout.children:
161 stream = UStringIO()
162 child.accept(self)
163 yield stream.getvalue()
164 self.__compute_funcs.pop()
165 try:
166 self.write, self.writeln = self.__compute_funcs[-1]
167 except IndexError:
168 del self.write
169 del self.writeln
170
171
172 from logilab.common.ureports.nodes import *
173 from logilab.common.ureports.text_writer import TextWriter
174 from logilab.common.ureports.html_writer import HTMLWriter
175