[IMP] document :- support indexation of .docx to text and PPTX to html
[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 import StringIO
23 import odt2txt
24 from subprocess import Popen, PIPE
25 from content_index import indexer, cntIndex
26
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 class TxtIndex(indexer):
41     def _getMimeTypes(self):
42         return ['text/plain','text/html','text/diff','text/xml', 'text/*', 
43         'application/xml']
44     
45     def _getExtensions(self):
46         return ['.txt', '.py']
47
48     def _doIndexContent(self,content):
49         return content
50         
51 cntIndex.register(TxtIndex())
52
53 class PptIndex(indexer):
54     def _getMimeTypes(self):
55         return [ 'application/ms-word']
56     
57     def _getExtensions(self):
58         return ['.ppt','.pptx']
59
60     def _doIndexFile(self,fname):
61         fp = Popen(['ppthtml', fname], shell=False, stdout=PIPE).stdout
62         return _to_unicode( fp.read())
63
64 cntIndex.register(PptIndex())
65
66 class DocIndex(indexer):
67     def _getMimeTypes(self):
68         return [ 'application/ms-word']
69     
70     def _getExtensions(self):
71         return ['.doc','.docx']
72
73     def _doIndexFile(self,fname):
74         fp = Popen(['antiword', fname], shell=False, stdout=PIPE).stdout
75         return _to_unicode( fp.read())
76
77 cntIndex.register(DocIndex())
78
79 class PdfIndex(indexer):
80     def _getMimeTypes(self):
81         return [ 'application/pdf']
82     
83     def _getExtensions(self):
84         return ['.pdf']
85
86     def _doIndexFile(self,fname):
87         fp = Popen(['pdftotext', '-enc', 'UTF-8', '-nopgbrk', fname, '-'], shell=False, stdout=PIPE).stdout
88         return _to_unicode( fp.read())
89
90 cntIndex.register(PdfIndex())
91
92 class ImageNoIndex(indexer):
93     def _getMimeTypes(self):
94         return [ 'image/*']
95     
96     def _getExtensions(self):
97         #better return no extension, and let 'file' do its magic
98         return []
99         #return ['.png','.jpg','.gif','.jpeg','.bmp','.tiff']
100
101     def _doIndexContent(self,content):
102         return 'image'
103
104
105 cntIndex.register(ImageNoIndex())
106
107 #class Doc(indexer):
108     #def _getDefMime(self,ext):
109
110 #def content_index(content, filename=None, content_type=None):
111     #fname,ext = os.path.splitext(filename)
112     #result = ''
113     #elif ext in ('.xls','.ods','.odt','.odp'):
114         #s = StringIO.StringIO(content)
115         #o = odt2txt.OpenDocumentTextFile(s)
116         #result = _to_unicode(o.toString())
117         #s.close()
118     #elif ext in ('.txt','.py','.patch','.html','.csv','.xml'):
119         #result = content
120     #else:
121         #result = content
122     #return result
123
124 #eof