1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
24 import reportlab.lib.units
25 from lxml import etree
26 from tools.safe_eval import safe_eval as eval
28 _regex = re.compile('\[\[(.+?)\]\]')
30 def _child_get(node, self=None, tagname=None):
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):
39 eval(n.get('rml_except'), {}, self.localcontext)
44 (tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
47 n2.attrib.update(attr)
53 self.localcontext = oldctx
55 if self and self.localcontext and n.get('rml_except', False):
57 eval(n.get('rml_except'), {}, self.localcontext)
60 if (tagname is None) or (n.tag==tagname):
63 def _process_text(self, txt):
64 if not self.localcontext:
69 sps = _regex.split(txt)
71 # This is a simple text to translate
72 result += self.localcontext.get('translate', lambda x:x)(sps.pop(0))
75 txt2 = eval(sps.pop(0),self.localcontext)
78 if type(txt2) == type(0) or type(txt2) == type(0.0):
80 if type(txt2)==type('') or type(txt2)==type(u''):
86 for node in node.getchildren():
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)
101 res = unit[0].search(size, 0)
103 return unit[1]*float(res.group(1))
106 def tuple_int_get(node, attr_name, default=None):
107 if not node.get(attr_name):
109 res = [int(x) for x in node.get(attr_name).split(',')]
113 return (str(value)=="1") or (value.lower()=='yes')
115 def attr_get(node, attrs, dict={}):
119 res[name] = unit_get(node.get(name))
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))
132 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: