~toolpart/openobject-server/toolpart

« back to all changes in this revision

Viewing changes to bin/report/render/rml2pdf/utils.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
# trml2pdf - An RML to PDF converter
 
30
# Copyright (C) 2003, Fabien Pinckaers, UCL, FSA
 
31
#
 
32
# This library is free software; you can redistribute it and/or
 
33
# modify it under the terms of the GNU Lesser General Public
 
34
# License as published by the Free Software Foundation; either
 
35
# version 2.1 of the License, or (at your option) any later version.
 
36
#
 
37
# This library is distributed in the hope that it will be useful,
 
38
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
39
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
40
# Lesser General Public License for more details.
 
41
#
 
42
# You should have received a copy of the GNU Lesser General Public
 
43
# License along with this library; if not, write to the Free Software
 
44
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
45
 
 
46
import re
 
47
import reportlab
 
48
 
 
49
def text_get(node):
 
50
        rc = ''
 
51
        for node in node.childNodes:
 
52
                if node.nodeType == node.TEXT_NODE:
 
53
                        rc = rc + node.data
 
54
        return rc
 
55
 
 
56
units = [
 
57
        (re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
 
58
        (re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),  
 
59
        (re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
 
60
        (re.compile('^(-?[0-9\.]+)\s*$'), 1)
 
61
]
 
62
 
 
63
def unit_get(size):
 
64
        global units
 
65
        for unit in units:
 
66
                res = unit[0].search(size, 0)
 
67
                if res:
 
68
                        return unit[1]*float(res.group(1))
 
69
        return False
 
70
 
 
71
def tuple_int_get(node, attr_name, default=None):
 
72
        if not node.hasAttribute(attr_name):
 
73
                return default
 
74
        res = [int(x) for x in node.getAttribute(attr_name).split(',')]
 
75
        return res
 
76
 
 
77
def bool_get(value):
 
78
        return (str(value)=="1") or (value.lower()=='yes')
 
79
 
 
80
def attr_get(node, attrs, dict={}):
 
81
        res = {}
 
82
        for name in attrs:
 
83
                if node.hasAttribute(name):
 
84
                        res[name] =  unit_get(node.getAttribute(name))
 
85
        for key in dict:
 
86
                if node.hasAttribute(key):
 
87
                        if dict[key]=='str':
 
88
                                res[key] = str(node.getAttribute(key))
 
89
                        elif dict[key]=='bool':
 
90
                                res[key] = bool_get(node.getAttribute(key))
 
91
                        elif dict[key]=='int':
 
92
                                res[key] = int(node.getAttribute(key))
 
93
        return res