1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
24 from subprocess import Popen, PIPE
25 from content_index import indexer, cntIndex
26 from subprocess import Popen, PIPE
30 return s.decode('utf-8')
33 return s.decode('latin')
36 return s.encode('ascii')
40 class TxtIndex(indexer):
41 def _getMimeTypes(self):
42 return ['text/plain','text/html','text/diff','text/xml', 'text/*',
45 def _getExtensions(self):
46 return ['.txt', '.py']
48 def _doIndexContent(self,content):
51 cntIndex.register(TxtIndex())
53 class PptxIndex(indexer):
54 def _getMimeTypes(self):
55 return [ 'application/vnd.openxmlformats-officedocument.presentationml.presentation']
57 def _getExtensions(self):
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
65 file_obj = open(str(fname + ".txt"), "r")
66 data = file_obj.read()
67 return _to_unicode(data)
69 cntIndex.register(PptxIndex())
71 class DocIndex(indexer):
72 def _getMimeTypes(self):
73 return [ 'application/ms-word']
75 def _getExtensions(self):
78 def _doIndexFile(self,fname):
79 fp = Popen(['antiword', fname], shell=False, stdout=PIPE).stdout
80 return _to_unicode( fp.read())
82 cntIndex.register(DocIndex())
84 class DocxIndex(indexer):
85 def _getMimeTypes(self):
86 return [ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']
88 def _getExtensions(self):
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/
97 fp = Popen(['docx2txt.pl', fname], shell=False, stdout=PIPE).stdout
99 file_obj = open(str(fname + ".txt"), "r")
100 data = file_obj.read()
101 return _to_unicode(data)
103 cntIndex.register(DocxIndex())
105 class PdfIndex(indexer):
106 def _getMimeTypes(self):
107 return [ 'application/pdf']
109 def _getExtensions(self):
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())
116 cntIndex.register(PdfIndex())
118 class ImageNoIndex(indexer):
119 def _getMimeTypes(self):
122 def _getExtensions(self):
123 #better return no extension, and let 'file' do its magic
125 #return ['.png','.jpg','.gif','.jpeg','.bmp','.tiff']
127 def _doIndexContent(self,content):
131 cntIndex.register(ImageNoIndex())
134 #def _getDefMime(self,ext):
136 #def content_index(content, filename=None, content_type=None):
137 #fname,ext = os.path.splitext(filename)
139 #elif ext in ('.xls','.ods','.odt','.odp'):
140 #s = StringIO.StringIO(content)
141 #o = odt2txt.OpenDocumentTextFile(s)
142 #result = _to_unicode(o.toString())
144 #elif ext in ('.txt','.py','.patch','.html','.csv','.xml'):