Port txt report to new report engine.
[odoo/odoo.git] / bin / report / render / rml2txt / 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 import reportlab.lib.units
43 from lxml import etree
44
45 _regex = re.compile('\[\[(.+?)\]\]')
46
47 def _child_get(node, self=None, tagname=None):
48     for n in node:
49         if self and self.localcontext and n.get('rml_loop', False):
50             oldctx = self.localcontext
51             for ctx in eval(n.get('rml_loop'),{}, self.localcontext):
52                 self.localcontext.update(ctx)
53                 if (tagname is None) or (n.tag==tagname):
54                     if n.get('rml_except', False):
55                         try:
56                             eval(n.get('rml_except'), {}, self.localcontext)
57                         except:
58                             continue
59                     if n.get('rml_tag'):
60                         try:
61                             (tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
62                             n2 = copy.copy(n)
63                             n2.tag = tag
64                             n2.attrib.update(attr)
65                             yield n2
66                         except:
67                             yield n
68                     else:
69                         yield n
70             self.localcontext = oldctx
71             continue
72         if self and self.localcontext and n.get('rml_except', False):
73             try:
74                 eval(n.get('rml_except'), {}, self.localcontext)
75             except:
76                 continue
77         if (tagname is None) or (n.tag==tagname):
78             yield n
79
80 def _process_text(self, txt):
81         if not self.localcontext:
82             return txt
83         if not txt:
84             return ''
85         result = ''
86         sps = _regex.split(txt)
87         while sps:
88             # This is a simple text to translate
89             result += self.localcontext.get('translate', lambda x:x)(sps.pop(0))
90             if sps:
91                 try:
92                     txt2 = eval(sps.pop(0),self.localcontext)
93                 except:
94                     txt2 = ''
95                 if type(txt2) == type(0) or type(txt2) == type(0.0):
96                     txt2 = str(txt2)
97                 if type(txt2)==type('') or type(txt2)==type(u''):
98                     result += txt2
99         return result
100
101 def text_get(node):
102     rc = ''
103     for node in node.getchildren():
104             rc = rc + node.text
105     return rc
106
107 units = [
108     (re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
109     (re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),
110     (re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
111     (re.compile('^(-?[0-9\.]+)\s*$'), 1)
112 ]
113
114 def unit_get(size):
115     global units
116     if size:
117         for unit in units:
118             res = unit[0].search(size, 0)
119             if res:
120                 return unit[1]*float(res.group(1))
121     return False
122
123 def tuple_int_get(node, attr_name, default=None):
124     if not node.get(attr_name):
125         return default
126     res = [int(x) for x in node.get(attr_name).split(',')]
127     return res
128
129 def bool_get(value):
130     return (str(value)=="1") or (value.lower()=='yes')
131
132 def attr_get(node, attrs, dict={}):
133     res = {}
134     for name in attrs:
135         if node.get(name):
136             res[name] = unit_get(node.get(name))
137     for key in dict:
138         if node.get(key):
139             if dict[key]=='str':
140                 res[key] = str(node.get(key))
141             elif dict[key]=='bool':
142                 res[key] = bool_get(node.get(key))
143             elif dict[key]=='int':
144                 res[key] = int(node.get(key))
145             elif dict[key]=='unit':
146                 res[key] = unit_get(node.get(key))
147     return res
148
149 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: