nodes.py 5.7 KB

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