[FIX] revert change 3509, as it introduces a regression
[odoo/odoo.git] / bin / report / render / rml2txt / utils.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import re
23 import reportlab
24 import reportlab.lib.units
25 from lxml import etree
26 from tools.safe_eval import safe_eval as eval
27
28 _regex = re.compile('\[\[(.+?)\]\]')
29
30 def _child_get(node, self=None, tagname=None):
31     for n in node:
32         if self and self.localcontext and n.get('rml_loop', False):
33             oldctx = self.localcontext
34             for ctx in eval(n.get('rml_loop'),{}, self.localcontext):
35                 self.localcontext.update(ctx)
36                 if (tagname is None) or (n.tag==tagname):
37                     if n.get('rml_except', False):
38                         try:
39                             eval(n.get('rml_except'), {}, self.localcontext)
40                         except:
41                             continue
42                     if n.get('rml_tag'):
43                         try:
44                             (tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
45                             n2 = copy.copy(n)
46                             n2.tag = tag
47                             n2.attrib.update(attr)
48                             yield n2
49                         except:
50                             yield n
51                     else:
52                         yield n
53             self.localcontext = oldctx
54             continue
55         if self and self.localcontext and n.get('rml_except', False):
56             try:
57                 eval(n.get('rml_except'), {}, self.localcontext)
58             except:
59                 continue
60         if (tagname is None) or (n.tag==tagname):
61             yield n
62
63 def _process_text(self, txt):
64         if not self.localcontext:
65             return txt
66         if not txt:
67             return ''
68         result = ''
69         sps = _regex.split(txt)
70         while sps:
71             # This is a simple text to translate
72             result += self.localcontext.get('translate', lambda x:x)(sps.pop(0))
73             if sps:
74                 try:
75                     txt2 = eval(sps.pop(0),self.localcontext)
76                 except:
77                     txt2 = ''
78                 if type(txt2) == type(0) or type(txt2) == type(0.0):
79                     txt2 = str(txt2)
80                 if type(txt2)==type('') or type(txt2)==type(u''):
81                     result += txt2
82         return result
83
84 def text_get(node):
85     rc = ''
86     for node in node.getchildren():
87             rc = rc + node.text
88     return rc
89
90 units = [
91     (re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
92     (re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),
93     (re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
94     (re.compile('^(-?[0-9\.]+)\s*$'), 1)
95 ]
96
97 def unit_get(size):
98     global units
99     if size:
100         for unit in units:
101             res = unit[0].search(size, 0)
102             if res:
103                 return unit[1]*float(res.group(1))
104     return False
105
106 def tuple_int_get(node, attr_name, default=None):
107     if not node.get(attr_name):
108         return default
109     res = [int(x) for x in node.get(attr_name).split(',')]
110     return res
111
112 def bool_get(value):
113     return (str(value)=="1") or (value.lower()=='yes')
114
115 def attr_get(node, attrs, dict={}):
116     res = {}
117     for name in attrs:
118         if node.get(name):
119             res[name] = unit_get(node.get(name))
120     for key in dict:
121         if node.get(key):
122             if dict[key]=='str':
123                 res[key] = str(node.get(key))
124             elif dict[key]=='bool':
125                 res[key] = bool_get(node.get(key))
126             elif dict[key]=='int':
127                 res[key] = int(node.get(key))
128             elif dict[key]=='unit':
129                 res[key] = unit_get(node.get(key))
130     return res
131
132 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: