7186cc13fff59f874b88c07ef5f770ab65b25137
[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-2008 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 import re
24 import reportlab
25 import reportlab.lib.units
26
27 def text_get(node):
28     rc = ''
29     for node in node.childNodes:
30         if node.nodeType == node.TEXT_NODE:
31             rc = rc + node.data
32     return rc
33
34 units = [
35     (re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
36     (re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),  
37     (re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
38     (re.compile('^(-?[0-9\.]+)\s*px$'), 0.7),
39     (re.compile('^(-?[0-9\.]+)\s*$'), 1)
40 ]
41
42 def unit_get(size):
43     global units
44     for unit in units:
45         res = unit[0].search(size, 0)
46         if res:
47             return int(unit[1]*float(res.group(1))*1.3)
48     return False
49
50 def tuple_int_get(node, attr_name, default=None):
51     if not node.hasAttribute(attr_name):
52         return default
53     res = [int(x) for x in node.getAttribute(attr_name).split(',')]
54     return res
55
56 def bool_get(value):
57     return (str(value)=="1") or (value.lower()=='yes')
58
59 def attr_get(node, attrs, dict={}):
60     res = {}
61     for name in attrs:
62         if node.hasAttribute(name):
63             res[name] =  unit_get(node.getAttribute(name))
64     for key in dict:
65         if node.hasAttribute(key):
66             if dict[key]=='str':
67                 res[key] = str(node.getAttribute(key))
68             elif dict[key]=='bool':
69                 res[key] = bool_get(node.getAttribute(key))
70             elif dict[key]=='int':
71                 res[key] = int(node.getAttribute(key))
72     return res
73
74 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
75