[FIX] Pagecount probelm while all objects print on same page and printing special...
[odoo/odoo.git] / bin / report / render / rml2pdf / utils.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 # trml2pdf - An RML to PDF converter
24 # Copyright (C) 2003, Fabien Pinckaers, UCL, FSA
25 #
26 # This library is free software; you can redistribute it and/or
27 # modify it under the terms of the GNU Lesser General Public
28 # License as published by the Free Software Foundation; either
29 # version 2.1 of the License, or (at your option) any later version.
30 #
31 # This library is distributed in the hope that it will be useful,
32 # but WITHOUT ANY WARRANTY; without even the implied warranty of
33 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
34 # Lesser General Public License for more details.
35 #
36 # You should have received a copy of the GNU Lesser General Public
37 # License along with this library; if not, write to the Free Software
38 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39
40 import re
41 import reportlab
42 from lxml import etree
43 import copy
44 import tools
45
46 _regex = re.compile('\[\[(.+?)\]\]')
47
48 def str2xml(s):
49     return s.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
50
51 def xml2str(s):
52     return s.replace('&amp;','&').replace('&lt;','<').replace('&gt;','>')
53
54 def _child_get(node, self=None, tagname=None):
55     for n in node:
56         if self and self.localcontext and n.get('rml_loop', False):
57             oldctx = self.localcontext
58
59             for ctx in eval(n.get('rml_loop'),{}, self.localcontext):
60                 self.localcontext.update(ctx)
61                 if (tagname is None) or (n.tag==tagname):
62                     if n.get('rml_except', False):
63                         try:
64                             eval(n.get('rml_except'), {}, self.localcontext)
65                         except:
66                             continue
67                     if n.get('rml_tag'):
68                         try:
69                             (tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
70                             n2 = copy.deepcopy(n)
71                             n2.tag = tag
72                             n2.attrib.update(attr)
73                             yield n2
74                         except:
75                             yield n
76                     else:
77                         yield n
78             self.localcontext = oldctx
79             continue
80         if self and self.localcontext and n.get('rml_except', False):
81             try:
82                 eval(n.get('rml_except'), {}, self.localcontext)
83             except:
84                 continue
85         if self and self.localcontext and n.get('rml_tag', False):
86             try:
87                 (tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
88                 n2 = copy.deepcopy(n)
89                 n2.tag = tag
90                 n2.attrib.update(attr or {})
91                 yield n2
92                 tagname = ''
93             except:
94                 pass
95         if (tagname is None) or (n.tag==tagname):
96             yield n
97
98 def _process_text(self, txt):
99         if not self.localcontext:
100             return str2xml(txt)
101         if not txt:
102             return ''
103         result = ''
104         sps = _regex.split(txt)
105         while sps:
106             # This is a simple text to translate
107             result += self.localcontext.get('translate', lambda x:x)(sps.pop(0))
108             if sps:
109                 try:
110                     txt = eval(sps.pop(0),self.localcontext)
111                 except:
112                     pass
113                 if type(txt)==type('') or type(txt)==type(u''):
114                     txt2 = str2xml(txt)
115                     result += tools.ustr(txt2)
116                 elif (txt is not None) and (txt is not False):
117                     result += str(txt)
118         return result
119
120 def text_get(node):
121     rc = ''
122     for node in node.getchildren():
123             rc = rc + tools.ustr(node.text)
124     return rc
125
126 units = [
127     (re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
128     (re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),
129     (re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
130     (re.compile('^(-?[0-9\.]+)\s*$'), 1)
131 ]
132
133 def unit_get(size):
134     global units
135     if size:
136         for unit in units:
137             res = unit[0].search(size, 0)
138             if res:
139                 return unit[1]*float(res.group(1))
140     return False
141
142 def tuple_int_get(node, attr_name, default=None):
143     if not node.get(attr_name):
144         return default
145     res = [int(x) for x in node.get(attr_name).split(',')]
146     return res
147
148 def bool_get(value):
149     return (str(value)=="1") or (value.lower()=='yes')
150
151 def attr_get(node, attrs, dict={}):
152     res = {}
153     for name in attrs:
154         if node.get(name):
155             res[name] = unit_get(node.get(name))
156     for key in dict:
157         if node.get(key):
158             if dict[key]=='str':
159                 res[key] = str(node.get(key))
160             elif dict[key]=='bool':
161                 res[key] = bool_get(node.get(key))
162             elif dict[key]=='int':
163                 res[key] = int(node.get(key))
164             elif dict[key]=='unit':
165                 res[key] = unit_get(node.get(key))
166             elif dict[key] == 'float' : 
167                 res[key] = float(node.get(key))
168     return res
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: