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

Source Code for Module logilab.common.ureports.nodes

  1  """Micro reports objects. 
  2   
  3  A micro report is a tree of layout and content objects. 
  4   
  5  :copyright: 
  6    2004-2008 `LOGILAB S.A. <http://www.logilab.fr>`_ (Paris, FRANCE), 
  7    all rights reserved. 
  8   
  9  :contact: 
 10    http://www.logilab.org/project/logilab-common -- 
 11    mailto:python-projects@logilab.org 
 12   
 13  :license: 
 14    `General Public License version 2 
 15    <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>`_ 
 16  """ 
 17  __docformat__ = "restructuredtext en" 
 18   
 19  from logilab.common.tree import VNode 
 20   
21 -class BaseComponent(VNode):
22 """base report component 23 24 attributes 25 * id : the component's optional id 26 * klass : the component's optional klass 27 """
28 - def __init__(self, id=None, klass=None):
29 VNode.__init__(self, id) 30 self.klass = klass
31
32 -class BaseLayout(BaseComponent):
33 """base container node 34 35 attributes 36 * BaseComponent attributes 37 * children : components in this table (i.e. the table's cells) 38 """
39 - def __init__(self, children=(), **kwargs):
40 super(BaseLayout, self).__init__(**kwargs) 41 for child in children: 42 if isinstance(child, BaseComponent): 43 self.append(child) 44 else: 45 self.add_text(child)
46
47 - def append(self, child):
48 """overridden to detect problems easily""" 49 assert child not in self.parents() 50 VNode.append(self, child)
51
52 - def parents(self):
53 """return the ancestor nodes""" 54 assert self.parent is not self 55 if self.parent is None: 56 return [] 57 return [self.parent] + self.parent.parents()
58
59 - def add_text(self, text):
60 """shortcut to add text data""" 61 self.children.append(Text(text))
62 63 64 # non container nodes ######################################################### 65
66 -class Text(BaseComponent):
67 """a text portion 68 69 attributes : 70 * BaseComponent attributes 71 * data : the text value as an encoded or unicode string 72 """
73 - def __init__(self, data, escaped=True, **kwargs):
74 super(Text, self).__init__(**kwargs) 75 #if isinstance(data, unicode): 76 # data = data.encode('ascii') 77 assert isinstance(data, (str, unicode)), data.__class__ 78 self.escaped = escaped 79 self.data = data
80
81 -class VerbatimText(Text):
82 """a verbatim text, display the raw data 83 84 attributes : 85 * BaseComponent attributes 86 * data : the text value as an encoded or unicode string 87 """
88 102 103
104 -class Image(BaseComponent):
105 """an embedded or a single image 106 107 attributes : 108 * BaseComponent attributes 109 * filename : the image's filename (REQUIRED) 110 * stream : the stream object containing the image data (REQUIRED) 111 * title : the image's optional title 112 """
113 - def __init__(self, filename, stream, title=None, **kwargs):
114 super(Link, self).__init__(**kwargs) 115 assert filename 116 assert stream 117 self.filename = filename 118 self.stream = stream 119 self.title = title
120 121 122 # container nodes ############################################################# 123
124 -class Section(BaseLayout):
125 """a section 126 127 attributes : 128 * BaseLayout attributes 129 130 a title may also be given to the constructor, it'll be added 131 as a first element 132 a description may also be given to the constructor, it'll be added 133 as a first paragraph 134 """
135 - def __init__(self, title=None, description=None, **kwargs):
136 super(Section, self).__init__(**kwargs) 137 if description: 138 self.insert(0, Paragraph([Text(description)])) 139 if title: 140 self.insert(0, Title(children=(title,)))
141
142 -class Title(BaseLayout):
143 """a title 144 145 attributes : 146 * BaseLayout attributes 147 148 A title must not contains a section nor a paragraph! 149 """
150
151 -class Span(BaseLayout):
152 """a title 153 154 attributes : 155 * BaseLayout attributes 156 157 A span should only contains Text and Link nodes (in-line elements) 158 """
159
160 -class Paragraph(BaseLayout):
161 """a simple text paragraph 162 163 attributes : 164 * BaseLayout attributes 165 166 A paragraph must not contains a section ! 167 """
168
169 -class Table(BaseLayout):
170 """some tabular data 171 172 attributes : 173 * BaseLayout attributes 174 * cols : the number of columns of the table (REQUIRED) 175 * rheaders : the first row's elements are table's header 176 * cheaders : the first col's elements are table's header 177 * title : the table's optional title 178 """
179 - def __init__(self, cols, title=None, 180 rheaders=0, cheaders=0, rrheaders=0, rcheaders=0, 181 **kwargs):
182 super(Table, self).__init__(**kwargs) 183 assert isinstance(cols, int) 184 self.cols = cols 185 self.title = title 186 self.rheaders = rheaders 187 self.cheaders = cheaders 188 self.rrheaders = rrheaders 189 self.rcheaders = rcheaders
190
191 -class List(BaseLayout):
192 """some list data 193 194 attributes : 195 * BaseLayout attributes 196 """
197