[IMP] Use cStringIO
[odoo/odoo.git] / bin / report / render / simple.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import render
24
25 from cStringIO import StringIO
26 import xml.dom.minidom
27
28 from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table
29 from reportlab.lib.units import mm
30 from reportlab.lib.pagesizes import A4
31 import reportlab.lib
32
33 import copy
34
35 class simple(render.render):
36     def _render(self):
37         self.result = StringIO()
38         parser = xml.dom.minidom.parseString(self.xml)
39
40         title = parser.documentElement.tagName
41         doc = SimpleDocTemplate(self.result, pagesize=A4, title=title,
42           author='OpenERP, Fabien Pinckaers', leftmargin=10*mm, rightmargin=10*mm)
43
44         styles = reportlab.lib.styles.getSampleStyleSheet()
45         title_style = copy.deepcopy(styles["Heading1"])
46         title_style.alignment = reportlab.lib.enums.TA_CENTER
47         story = [ Paragraph(title, title_style) ]
48         style_level = {}
49         nodes = [ (parser.documentElement,0) ]
50         while len(nodes):
51             node = nodes.pop(0)
52             value = ''
53             n=len(node[0].childNodes)-1
54             while n>=0:
55                 if node[0].childNodes[n].nodeType==3:
56                     value += node[0].childNodes[n].nodeValue
57                 else:
58                     nodes.insert( 0, (node[0].childNodes[n], node[1]+1) )
59                 n-=1
60             if not node[1] in style_level:
61                 style = copy.deepcopy(styles["Normal"])
62                 style.leftIndent=node[1]*6*mm
63                 style.firstLineIndent=-3*mm
64                 style_level[node[1]] = style
65             story.append( Paragraph('<b>%s</b>: %s' % (node[0].tagName, value), style_level[node[1]]))
66         doc.build(story)
67         return self.result.getvalue()
68
69 if __name__=='__main__':
70     import time
71     s = simple('''<test>
72         <author-list>
73             <author>
74                 <name>Fabien Pinckaers</name>
75                 <age>23</age>
76             </author>
77             <author>
78                 <name>Michel Pinckaers</name>
79                 <age>53</age>
80             </author>
81             No other
82         </author-list>
83     </test>''')
84     print s.render()
85
86 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
87