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

Source Code for Module logilab.common.ureports.nodes

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