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 ##############################################################################
22 from content_index import indexer, cntIndex
23 from subprocess import Popen, PIPE
26 import sys, zipfile, xml.dom.minidom
31 return s.decode('utf-8')
34 return s.decode('latin')
37 return s.encode('ascii')
41 def textToString(element) :
43 for node in element.childNodes :
44 if node.nodeType == xml.dom.Node.TEXT_NODE :
45 buffer += node.nodeValue
46 elif node.nodeType == xml.dom.Node.ELEMENT_NODE :
47 buffer += textToString(node)
50 class TxtIndex(indexer):
51 def _getMimeTypes(self):
52 return ['text/plain','text/html','text/diff','text/xml', 'text/*',
55 def _getExtensions(self):
56 return ['.txt', '.py']
58 def _doIndexContent(self,content):
61 cntIndex.register(TxtIndex())
63 class PptxIndex(indexer):
64 def _getMimeTypes(self):
65 return [ 'application/vnd.openxmlformats-officedocument.presentationml.presentation']
67 def _getExtensions(self):
70 def _doIndexFile(self,fname):
72 """ Converts the document to a string. """
75 for paragraph in content.getElementsByTagName(val) :
76 buffer += textToString(paragraph) + "\n"
80 zip = zipfile.ZipFile(fname)
81 files = filter(lambda x: x.startswith('ppt/slides/slide'), zip.namelist())
82 for i in range(1, len(files) + 1):
83 content = xml.dom.minidom.parseString(zip.read('ppt/slides/slide%s.xml' % str(i)))
84 res = toString().encode('ascii','replace')
87 return _to_unicode('\n'.join(data))
89 cntIndex.register(PptxIndex())
91 class DocIndex(indexer):
92 def _getMimeTypes(self):
93 return [ 'application/ms-word']
95 def _getExtensions(self):
98 def _doIndexFile(self,fname):
100 pop = Popen(['antiword', fname], shell=False, stdout=PIPE)
101 (data, _) = pop.communicate()
102 return _to_unicode(data)
104 logger = logging.getLogger('document.DocIndex')
105 logger.warn("Failed attempt to execute antiword (MS Word reader). Antiword is necessary to index the file %s of MIME type %s. Detailed error available at DEBUG level.", fname, self._getMimeTypes()[0])
106 logger.debug("Trace of the failed file indexing attempt: ", exc_info=True)
109 cntIndex.register(DocIndex())
111 class DocxIndex(indexer):
112 def _getMimeTypes(self):
113 return [ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']
115 def _getExtensions(self):
118 def _doIndexFile(self,fname):
119 zip = zipfile.ZipFile(fname)
120 content = xml.dom.minidom.parseString(zip.read("word/document.xml"))
122 """ Converts the document to a string. """
124 for val in ["w:p", "w:h", "text:list"]:
125 for paragraph in content.getElementsByTagName(val) :
126 buffer += textToString(paragraph) + "\n"
129 res = toString().encode('ascii','replace')
131 return _to_unicode(res)
133 cntIndex.register(DocxIndex())
136 class XlsxIndex(indexer):
137 def _getMimeTypes(self):
138 return [ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
140 def _getExtensions(self):
143 def _doIndexFile(self,fname):
144 zip = zipfile.ZipFile(fname)
145 content = xml.dom.minidom.parseString(zip.read("xl/sharedStrings.xml"))
147 """ Converts the document to a string. """
150 for paragraph in content.getElementsByTagName(val) :
151 buffer += textToString(paragraph) + "\n"
154 res = toString().encode('ascii','replace')
156 return _to_unicode(res)
158 cntIndex.register(XlsxIndex())
160 class PdfIndex(indexer):
161 def _getMimeTypes(self):
162 return [ 'application/pdf']
164 def _getExtensions(self):
167 def _doIndexFile(self,fname):
168 pop = Popen(['pdftotext', '-enc', 'UTF-8', '-nopgbrk', fname, '-'], shell=False, stdout=PIPE)
169 (data, _) = pop.communicate()
170 return _to_unicode(data)
172 cntIndex.register(PdfIndex())
174 class ImageNoIndex(indexer):
175 def _getMimeTypes(self):
178 def _getExtensions(self):
179 #better return no extension, and let 'file' do its magic
181 #return ['.png','.jpg','.gif','.jpeg','.bmp','.tiff']
183 def _doIndexContent(self,content):
187 cntIndex.register(ImageNoIndex())
189 # other opendocument formats:
190 # chart-template chart database
191 # formula-template formula graphics-template graphics
193 # presentation-template presentation spreadsheet-template spreadsheet
195 class OpenDoc(indexer):
196 """ Index OpenDocument files.
198 Q: is it really worth it to index spreadsheets, or do we only get a
199 meaningless list of numbers (cell contents) ?
201 def _getMimeTypes(self):
202 otypes = [ 'text', 'text-web', 'text-template', 'text-master' ]
203 return map(lambda a: 'application/vnd.oasis.opendocument.'+a, otypes)
205 def _getExtensions(self):
206 return ['.odt', '.ott', ] # '.ods'
208 def _doIndexContent(self, content):
209 s = StringIO.StringIO(content)
210 o = odt2txt.OpenDocumentTextFile(s)
211 result = _to_unicode(o.toString())
215 cntIndex.register(OpenDoc())
220 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: