[IMP]: Improve search view of Task hours report of project_timesheet module and file...
[odoo/odoo.git] / addons / document / std_index.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 content_index import indexer, cntIndex
23 from subprocess import Popen, PIPE
24 import StringIO
25 import odt2txt
26 import sys, zipfile, xml.dom.minidom
27
28 def _to_unicode(s):
29     try:
30         return s.decode('utf-8')
31     except UnicodeError:
32         try:
33             return s.decode('latin')
34         except UnicodeError:
35             try:
36                 return s.encode('ascii')
37             except UnicodeError:
38                 return s
39
40 def textToString(element) :
41     buffer = u""
42     for node in element.childNodes :
43         if node.nodeType == xml.dom.Node.TEXT_NODE :
44             buffer += node.nodeValue
45         elif node.nodeType == xml.dom.Node.ELEMENT_NODE :
46             buffer += textToString(node)
47     return buffer
48         
49 class TxtIndex(indexer):
50     def _getMimeTypes(self):
51         return ['text/plain','text/html','text/diff','text/xml', 'text/*', 
52             'application/xml']
53     
54     def _getExtensions(self):
55         return ['.txt', '.py']
56
57     def _doIndexContent(self,content):
58         return content
59
60 cntIndex.register(TxtIndex())
61
62 class PptxIndex(indexer):
63     def _getMimeTypes(self):
64         return [ 'application/vnd.openxmlformats-officedocument.presentationml.presentation']
65     
66     def _getExtensions(self):
67         return ['.pptx']
68
69     def _doIndexFile(self,fname):
70         def toString () :
71             """ Converts the document to a string. """
72             buffer = u""
73             for val in ["a:t"]:
74                 for paragraph in content.getElementsByTagName(val) :
75                     buffer += textToString(paragraph) + "\n"
76             return buffer
77
78         data = []
79         zip = zipfile.ZipFile(fname)
80         files = filter(lambda x: x.startswith('ppt/slides/slide'), zip.namelist())
81         for i in range(1, len(files) + 1):
82             content = xml.dom.minidom.parseString(zip.read('ppt/slides/slide%s.xml' % str(i)))
83             res = toString().encode('ascii','replace')
84             data.append(res)
85
86         return _to_unicode('\n'.join(data))
87
88 cntIndex.register(PptxIndex())
89
90 class DocIndex(indexer):
91     def _getMimeTypes(self):
92         return [ 'application/ms-word']
93     
94     def _getExtensions(self):
95         return ['.doc']
96
97     def _doIndexFile(self,fname):
98         fp = Popen(['antiword', fname], shell=False, stdout=PIPE).stdout
99         try:
100             file_data = _to_unicode(fp.read())
101         finally:
102             fp.close()
103             
104         return file_data
105     
106 cntIndex.register(DocIndex())
107
108 class DocxIndex(indexer):
109     def _getMimeTypes(self):
110         return [ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']
111
112     def _getExtensions(self):
113         return ['.docx']
114
115     def _doIndexFile(self,fname):
116         zip = zipfile.ZipFile(fname)
117         content = xml.dom.minidom.parseString(zip.read("word/document.xml"))
118         def toString () :
119             """ Converts the document to a string. """
120             buffer = u""
121             for val in ["w:p", "w:h", "text:list"]:
122                 for paragraph in content.getElementsByTagName(val) :
123                     buffer += textToString(paragraph) + "\n"
124             return buffer
125
126         res = toString().encode('ascii','replace')
127
128         return _to_unicode(res)
129
130 cntIndex.register(DocxIndex())
131
132
133 class XlsxIndex(indexer):
134     def _getMimeTypes(self):
135         return [ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
136
137     def _getExtensions(self):
138         return ['.xlsx']
139
140     def _doIndexFile(self,fname):
141         zip = zipfile.ZipFile(fname)
142         content = xml.dom.minidom.parseString(zip.read("xl/sharedStrings.xml"))
143         def toString () :
144             """ Converts the document to a string. """
145             buffer = u""
146             for val in ["t"]:
147                 for paragraph in content.getElementsByTagName(val) :
148                     buffer += textToString(paragraph) + "\n"
149             return buffer
150
151         res = toString().encode('ascii','replace')
152
153         return _to_unicode(res)
154
155 cntIndex.register(XlsxIndex())
156
157 class PdfIndex(indexer):
158     def _getMimeTypes(self):
159         return [ 'application/pdf']
160     
161     def _getExtensions(self):
162         return ['.pdf']
163
164     def _doIndexFile(self,fname):
165         fp = Popen(['pdftotext', '-enc', 'UTF-8', '-nopgbrk', fname, '-'], shell=False, stdout=PIPE).stdout
166         try:
167            file_data = _to_unicode( fp.read())
168         finally:
169             fp.close()
170                  
171         return file_data
172
173 cntIndex.register(PdfIndex())
174
175 class ImageNoIndex(indexer):
176     def _getMimeTypes(self):
177         return [ 'image/*']
178     
179     def _getExtensions(self):
180         #better return no extension, and let 'file' do its magic
181         return []
182         #return ['.png','.jpg','.gif','.jpeg','.bmp','.tiff']
183
184     def _doIndexContent(self,content):
185         return 'image'
186
187
188 cntIndex.register(ImageNoIndex())
189
190 # other opendocument formats:
191 # chart-template chart database
192 # formula-template formula graphics-template graphics
193 # image
194 # presentation-template presentation spreadsheet-template spreadsheet
195
196 class OpenDoc(indexer):
197     """ Index OpenDocument files.
198     
199         Q: is it really worth it to index spreadsheets, or do we only get a
200         meaningless list of numbers (cell contents) ?
201         """
202     def _getMimeTypes(self):
203         otypes = [ 'text', 'text-web', 'text-template', 'text-master' ]
204         return map(lambda a: 'application/vnd.oasis.opendocument.'+a, otypes)
205     
206     def _getExtensions(self):
207         return ['.odt', '.ott', ] # '.ods'
208
209     def _doIndexContent(self, content):
210         s = StringIO.StringIO(content)
211         o = odt2txt.OpenDocumentTextFile(s)
212         result = _to_unicode(o.toString())
213         s.close()
214         return result
215
216 cntIndex.register(OpenDoc())
217
218
219 #eof