[IMP] document :- change comment
[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 from subprocess import Popen, PIPE
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 PptxIndex(indexer):
54     def _getMimeTypes(self):
55         return [ 'application/vnd.openxmlformats-officedocument.presentationml.presentation']
56     
57     def _getExtensions(self):
58         return ['.pptx']
59
60     def _doIndexFile(self,fname):
61         # Download pptx2txt package then used  http://sourceforge.net/projects/pptx2txt/" link.
62         # To install this tool, just copy pptx2txt.pl to appropriate place (e.g. /usr/bin directory)
63         fp = Popen(['pptx2txt.pl', fname], shell=False, stdout=PIPE).stdout
64         fp.read()
65         file_obj = open(str(fname + ".txt"), "r")
66         data = file_obj.read()
67         return _to_unicode(data)
68
69 cntIndex.register(PptxIndex())
70
71 class DocIndex(indexer):
72     def _getMimeTypes(self):
73         return [ 'application/ms-word']
74     
75     def _getExtensions(self):
76         return ['.doc']
77
78     def _doIndexFile(self,fname):
79         fp = Popen(['antiword', fname], shell=False, stdout=PIPE).stdout
80         return _to_unicode( fp.read())
81
82 cntIndex.register(DocIndex())
83
84 class DocxIndex(indexer):
85     def _getMimeTypes(self):
86         return [ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']
87     
88     def _getExtensions(self):
89         return ['.docx']
90
91     def _doIndexFile(self,fname):
92         # If you want to Download docx2txt package then used  "http://sourceforge.net/projects/docx2txt/" link.   
93         # In case, you don't want to use Makefile for installation, you can follow these steps for manual installation.
94         # Copy docx2txt.pl, docx2txt.sh and docx2txt.config to appropriate place (e.g. /usr/bin directory) . used following command.
95         # --> cp docx2txt.pl docx2txt.sh docx2txt.config /usr/bin/
96
97         fp = Popen(['docx2txt.pl', fname], shell=False, stdout=PIPE).stdout
98         fp.read()
99         file_obj = open(str(fname + ".txt"), "r")
100         data = file_obj.read()
101         return _to_unicode(data)
102
103 cntIndex.register(DocxIndex())
104
105 class PdfIndex(indexer):
106     def _getMimeTypes(self):
107         return [ 'application/pdf']
108     
109     def _getExtensions(self):
110         return ['.pdf']
111
112     def _doIndexFile(self,fname):
113         fp = Popen(['pdftotext', '-enc', 'UTF-8', '-nopgbrk', fname, '-'], shell=False, stdout=PIPE).stdout
114         return _to_unicode( fp.read())
115
116 cntIndex.register(PdfIndex())
117
118 class ImageNoIndex(indexer):
119     def _getMimeTypes(self):
120         return [ 'image/*']
121     
122     def _getExtensions(self):
123         #better return no extension, and let 'file' do its magic
124         return []
125         #return ['.png','.jpg','.gif','.jpeg','.bmp','.tiff']
126
127     def _doIndexContent(self,content):
128         return 'image'
129
130
131 cntIndex.register(ImageNoIndex())
132
133 #class Doc(indexer):
134     #def _getDefMime(self,ext):
135
136 #def content_index(content, filename=None, content_type=None):
137     #fname,ext = os.path.splitext(filename)
138     #result = ''
139     #elif ext in ('.xls','.ods','.odt','.odp'):
140         #s = StringIO.StringIO(content)
141         #o = odt2txt.OpenDocumentTextFile(s)
142         #result = _to_unicode(o.toString())
143         #s.close()
144     #elif ext in ('.txt','.py','.patch','.html','.csv','.xml'):
145         #result = content
146     #else:
147         #result = content
148     #return result
149
150 #eof