~openerp/openobject-server/web-dashboard

« back to all changes in this revision

Viewing changes to bin/report/render/simple.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#                    Fabien Pinckaers <fp@tiny.Be>
 
5
#
 
6
# WARNING: This program as such is intended to be used by professional
 
7
# programmers who take the whole responsability of assessing all potential
 
8
# consequences resulting from its eventual inadequacies and bugs
 
9
# End users who are looking for a ready-to-use solution with commercial
 
10
# garantees and support are strongly adviced to contract a Free Software
 
11
# Service Company
 
12
#
 
13
# This program is Free Software; you can redistribute it and/or
 
14
# modify it under the terms of the GNU General Public License
 
15
# as published by the Free Software Foundation; either version 2
 
16
# of the License, or (at your option) any later version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
26
#
 
27
##############################################################################
 
28
 
 
29
import render
 
30
 
 
31
from StringIO import StringIO
 
32
import xml.dom.minidom
 
33
 
 
34
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table
 
35
from reportlab.lib.units import mm
 
36
from reportlab.lib.pagesizes import A4
 
37
import reportlab.lib
 
38
 
 
39
import copy
 
40
 
 
41
class simple(render.render):
 
42
        def _render(self):
 
43
                self.result = StringIO()
 
44
                parser = xml.dom.minidom.parseString(self.xml)
 
45
 
 
46
                title = parser.documentElement.tagName
 
47
                doc = SimpleDocTemplate(self.result, pagesize=A4, title=title,
 
48
                  author='Tiny ERP, Fabien Pinckaers', leftmargin=10*mm, rightmargin=10*mm)
 
49
 
 
50
                styles = reportlab.lib.styles.getSampleStyleSheet()
 
51
                title_style = copy.deepcopy(styles["Heading1"])
 
52
                title_style.alignment = reportlab.lib.enums.TA_CENTER
 
53
                story = [ Paragraph(title, title_style) ]
 
54
                style_level = {}
 
55
                nodes = [ (parser.documentElement,0) ]
 
56
                while len(nodes):
 
57
                        node = nodes.pop(0)
 
58
                        value = ''
 
59
                        n=len(node[0].childNodes)-1
 
60
                        while n>=0:
 
61
                                if node[0].childNodes[n].nodeType==3:
 
62
                                        value += node[0].childNodes[n].nodeValue
 
63
                                else:
 
64
                                        nodes.insert( 0, (node[0].childNodes[n], node[1]+1) )
 
65
                                n-=1
 
66
                        if not node[1] in style_level:
 
67
                                style = copy.deepcopy(styles["Normal"])
 
68
                                style.leftIndent=node[1]*6*mm
 
69
                                style.firstLineIndent=-3*mm
 
70
                                style_level[node[1]] = style
 
71
                        story.append( Paragraph('<b>%s</b>: %s' % (node[0].tagName, value), style_level[node[1]]))
 
72
                doc.build(story)
 
73
                return self.result.getvalue()
 
74
 
 
75
if __name__=='__main__':
 
76
        import time
 
77
        s = simple('''<test>
 
78
                <author-list>
 
79
                        <author>
 
80
                                <name>Fabien Pinckaers</name>
 
81
                                <age>23</age>
 
82
                        </author>
 
83
                        <author>
 
84
                                <name>Michel Pinckaers</name>
 
85
                                <age>53</age>
 
86
                        </author>
 
87
                        No other
 
88
                </author-list>
 
89
        </test>''')
 
90
        print s.render()