[IMP] Added missing vim mode lines
[odoo/odoo.git] / openerp / report / preprocess.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 from lxml import etree
23 import re
24 rml_parents = ['tr','story','section']
25 html_parents = ['tr','body','div']
26 sxw_parents = ['{http://openoffice.org/2000/table}table-row','{http://openoffice.org/2000/office}body','{http://openoffice.org/2000/text}section']
27 odt_parents = ['{urn:oasis:names:tc:opendocument:xmlns:office:1.0}body','{urn:oasis:names:tc:opendocument:xmlns:table:1.0}table-row','{urn:oasis:names:tc:opendocument:xmlns:text:1.0}section']
28
29 class report(object):
30     def preprocess_rml(self, root_node,type='pdf'):
31         _regex1 = re.compile("\[\[(.*?)(repeatIn\(.*?\s*,\s*[\'\"].*?[\'\"]\s*(?:,\s*(.*?)\s*)?\s*\))(.*?)\]\]")
32         _regex11= re.compile("\[\[(.*?)(repeatIn\(.*?\s*\(.*?\s*[\'\"].*?[\'\"]\s*\),[\'\"].*?[\'\"](?:,\s*(.*?)\s*)?\s*\))(.*?)\]\]")
33         _regex2 = re.compile("\[\[(.*?)(removeParentNode\(\s*(?:['\"](.*?)['\"])\s*\))(.*?)\]\]")
34         _regex3 = re.compile("\[\[\s*(.*?setTag\(\s*['\"](.*?)['\"]\s*,\s*['\"].*?['\"]\s*(?:,.*?)?\).*?)\s*\]\]")
35         for node in root_node:
36             if node.tag == etree.Comment:
37                 continue
38             if node.text or node.tail:
39                 def _sub3(txt):
40                     n = node
41                     while n.tag != txt.group(2):
42                         n = n.getparent()
43                     n.set('rml_tag', txt.group(1))
44                     return "[[ '' ]]"
45                 def _sub2(txt):
46                     if txt.group(3):
47                         n = node
48                         try:
49                             while n.tag != txt.group(3):
50                                 n = n.getparent()
51                         except:
52                             n = node
53                     else:
54                         n = node.getparent()
55                     n.set('rml_except', txt.group(0)[2:-2])
56                     return txt.group(0)
57                 def _sub1(txt):
58                     if len(txt.group(4)) > 1:
59                         return " "
60                     match = rml_parents
61                     if type == 'odt':
62                         match = odt_parents
63                     if type == 'sxw':
64                         match = sxw_parents
65                     if type =='html2html':
66                         match = html_parents
67                     if txt.group(3):
68                         group_3 = txt.group(3)
69                         if group_3.startswith("'") or group_3.startswith('"'):
70                             group_3 = group_3[1:-1]
71                         match = [group_3]
72                     n = node
73                     while n.tag not in match:
74                         n = n.getparent()
75                     n.set('rml_loop', txt.group(2))
76                     return '[['+txt.group(1)+"''"+txt.group(4)+']]'
77                 t = _regex1.sub(_sub1, node.text or node.tail)
78                 if t == " ":
79                     t = _regex11.sub(_sub1, node.text  or node.tail)
80                 t = _regex3.sub(_sub3, t)
81                 node.text = _regex2.sub(_sub2, t)
82             self.preprocess_rml(node,type)
83         return root_node
84
85 if __name__=='__main__':
86     node = etree.XML('''<story>
87     <para>This is a test[[ setTag('para','xpre') ]]</para>
88     <blockTable>
89     <tr>
90         <td><para>Row 1 [[ setTag('tr','tr',{'style':'TrLevel'+str(a['level']), 'paraStyle':('Level'+str(a['level']))}) ]] </para></td>
91         <td>Row 2 [[ True and removeParentNode('td') ]] </td>
92     </tr><tr>
93         <td>Row 1 [[repeatIn(o.order_line,'o')]] </td>
94         <td>Row 2</td>
95     </tr>
96     </blockTable>
97     <p>This isa test</p>
98 </story>''')
99     a = report()
100     result = a.preprocess_rml(node)
101     print etree.tostring(result)
102
103
104 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: