1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # Copyright (C) 2003, Fabien Pinckaers, UCL, FSA
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library 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 GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-13017 USA
21 ##############################################################################
30 import openerp.tools as tools
31 from openerp.tools.safe_eval import safe_eval as eval
32 from openerp.tools.misc import ustr
34 _logger = logging.getLogger(__name__)
36 _regex = re.compile('\[\[(.+?)\]\]')
39 return (s or '').replace('&', '&').replace('<', '<').replace('>', '>')
42 return (s or '').replace('&','&').replace('<','<').replace('>','>')
44 def _child_get(node, self=None, tagname=None):
46 if self and self.localcontext and n.get('rml_loop'):
48 for ctx in eval(n.get('rml_loop'),{}, self.localcontext):
49 self.localcontext.update(ctx)
50 if (tagname is None) or (n.tag==tagname):
51 if n.get('rml_except', False):
53 eval(n.get('rml_except'), {}, self.localcontext)
57 _logger.warning('rml_except: "%s"', n.get('rml_except',''), exc_info=True)
61 (tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
64 n2.attrib.update(attr)
69 _logger.warning('rml_tag: "%s"', n.get('rml_tag',''), exc_info=True)
74 if self and self.localcontext and n.get('rml_except'):
76 eval(n.get('rml_except'), {}, self.localcontext)
80 _logger.warning('rml_except: "%s"', n.get('rml_except',''), exc_info=True)
82 if self and self.localcontext and n.get('rml_tag'):
84 (tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
87 n2.attrib.update(attr or {})
93 _logger.warning('rml_tag: "%s"', n.get('rml_tag',''), exc_info=True)
95 if (tagname is None) or (n.tag==tagname):
98 def _process_text(self, txt):
99 """Translate ``txt`` according to the language in the local context,
100 replace dynamic ``[[expr]]`` with their real value, then escape
103 :param str txt: original text to translate (must NOT be XML-escaped)
104 :return: translated text, with dynamic expressions evaluated and
105 with special XML characters escaped (``&,<,>``).
107 if not self.localcontext:
112 sps = _regex.split(txt)
114 # This is a simple text to translate
115 to_translate = tools.ustr(sps.pop(0))
116 result += tools.ustr(self.localcontext.get('translate', lambda x:x)(to_translate))
121 txt = eval(expr, self.localcontext)
122 if txt and isinstance(txt, basestring):
123 txt = tools.ustr(txt)
126 if isinstance(txt, basestring):
128 elif txt and (txt is not None) and (txt is not False):
130 return str2xml(result)
133 return ''.join([ustr(n.text) for n in node])
136 (re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
137 (re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),
138 (re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
139 (re.compile('^(-?[0-9\.]+)\s*$'), 1)
145 if size.find('.') == -1:
148 decimal_point = locale.nl_langinfo(locale.RADIXCHAR)
150 decimal_point = locale.localeconv()['decimal_point']
152 size = size.replace(decimal_point, '.')
155 res = unit[0].search(size, 0)
157 return unit[1]*float(res.group(1))
160 def tuple_int_get(node, attr_name, default=None):
161 if not node.get(attr_name):
163 return map(int, node.get(attr_name).split(','))
166 return (str(value)=="1") or (value.lower()=='yes')
168 def attr_get(node, attrs, dict=None):
174 res[name] = unit_get(node.get(name))
178 res[key] = tools.ustr(node.get(key))
179 elif dict[key]=='bool':
180 res[key] = bool_get(node.get(key))
181 elif dict[key]=='int':
182 res[key] = int(node.get(key))
183 elif dict[key]=='unit':
184 res[key] = unit_get(node.get(key))
185 elif dict[key] == 'float' :
186 res[key] = float(node.get(key))
189 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: