Package logilab :: Package common :: Package ureports
[frames] | no frames]

Source Code for Package logilab.common.ureports

  1  # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  2  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  3  # 
  4  # This file is part of logilab-common. 
  5  # 
  6  # logilab-common is free software: you can redistribute it and/or modify it under 
  7  # the terms of the GNU Lesser General Public License as published by the Free 
  8  # Software Foundation, either version 2.1 of the License, or (at your option) any 
  9  # later version. 
 10  # 
 11  # logilab-common is distributed in the hope that it will be useful, but WITHOUT 
 12  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 13  # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
 14  # details. 
 15  # 
 16  # You should have received a copy of the GNU Lesser General Public License along 
 17  # with logilab-common.  If not, see <http://www.gnu.org/licenses/>. 
 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   
33 -def get_nodes(node, klass):
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 # recurse (FIXME: recursion controled by an option) 39 for grandchild in get_nodes(child, klass): 40 yield grandchild
41
42 -def layout_title(layout):
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
49 -def build_summary(layout, level=1):
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 # FIXME: Three following lines produce not very compliant 64 # docbook: there are some useless <para><para>. They might be 65 # replaced by the three commented lines but this then produces 66 # a bug in html display... 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 # summary.append(node) 71 # if level and [n for n in child.children if isinstance(n, Section)]: 72 # summary.append(build_summary(child, level)) 73 return summary
74 75
76 -class BaseWriter(object):
77 """base class for ureport writers""" 78
79 - def format(self, layout, stream=None, encoding=None):
80 """format and write the given layout into the stream object 81 82 unicode policy: unicode strings may be found in the layout; 83 try to call stream.write with it, but give it back encoded using 84 the given encoding if it fails 85 """ 86 if stream is None: 87 stream = sys.stdout 88 if not encoding: 89 encoding = getattr(stream, 'encoding', 'UTF-8') 90 self.encoding = encoding or 'UTF-8' 91 self.__compute_funcs = [] 92 self.out = stream 93 self.begin_format(layout) 94 layout.accept(self) 95 self.end_format(layout)
96
97 - def format_children(self, layout):
98 """recurse on the layout children and call their accept method 99 (see the Visitor pattern) 100 """ 101 for child in getattr(layout, 'children', ()): 102 child.accept(self)
103
104 - def writeln(self, string=''):
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
115 - def begin_format(self, layout):
116 """begin to format a layout""" 117 self.section = 0
118
119 - def end_format(self, layout):
120 """finished to format a layout"""
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 # fill missing cells 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 # use cells ! 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