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
30 return s.decode('utf-8')
33 return s.decode('latin')
36 return s.encode('ascii')
40 def textToString(element) :
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)
49 class TxtIndex(indexer):
50 def _getMimeTypes(self):
51 return ['text/plain','text/html','text/diff','text/xml', 'text/*',
54 def _getExtensions(self):
55 return ['.txt', '.py']
57 def _doIndexContent(self,content):
60 cntIndex.register(TxtIndex())
62 class PptxIndex(indexer):
63 def _getMimeTypes(self):
64 return [ 'application/vnd.openxmlformats-officedocument.presentationml.presentation']
66 def _getExtensions(self):
69 def _doIndexFile(self,fname):
71 """ Converts the document to a string. """
74 for paragraph in content.getElementsByTagName(val) :
75 buffer += textToString(paragraph) + "\n"
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')
86 return _to_unicode('\n'.join(data))
88 cntIndex.register(PptxIndex())
90 class DocIndex(indexer):
91 def _getMimeTypes(self):
92 return [ 'application/ms-word']
94 def _getExtensions(self):
97 def _doIndexFile(self,fname):
98 pop = Popen(['antiword', fname], shell=False, stdout=PIPE)
99 (data, _) = pop.communicate()
100 return _to_unicode(data)
102 cntIndex.register(DocIndex())
104 class DocxIndex(indexer):
105 def _getMimeTypes(self):
106 return [ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']
108 def _getExtensions(self):
111 def _doIndexFile(self,fname):
112 zip = zipfile.ZipFile(fname)
113 content = xml.dom.minidom.parseString(zip.read("word/document.xml"))
115 """ Converts the document to a string. """
117 for val in ["w:p", "w:h", "text:list"]:
118 for paragraph in content.getElementsByTagName(val) :
119 buffer += textToString(paragraph) + "\n"
122 res = toString().encode('ascii','replace')
124 return _to_unicode(res)
126 cntIndex.register(DocxIndex())
129 class XlsxIndex(indexer):
130 def _getMimeTypes(self):
131 return [ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']
133 def _getExtensions(self):
136 def _doIndexFile(self,fname):
137 zip = zipfile.ZipFile(fname)
138 content = xml.dom.minidom.parseString(zip.read("xl/sharedStrings.xml"))
140 """ Converts the document to a string. """
143 for paragraph in content.getElementsByTagName(val) :
144 buffer += textToString(paragraph) + "\n"
147 res = toString().encode('ascii','replace')
149 return _to_unicode(res)
151 cntIndex.register(XlsxIndex())
153 class PdfIndex(indexer):
154 def _getMimeTypes(self):
155 return [ 'application/pdf']
157 def _getExtensions(self):
160 def _doIndexFile(self,fname):
161 pop = Popen(['pdftotext', '-enc', 'UTF-8', '-nopgbrk', fname, '-'], shell=False, stdout=PIPE)
162 (data, _) = pop.communicate()
163 return _to_unicode(data)
165 cntIndex.register(PdfIndex())
167 class ImageNoIndex(indexer):
168 def _getMimeTypes(self):
171 def _getExtensions(self):
172 #better return no extension, and let 'file' do its magic
174 #return ['.png','.jpg','.gif','.jpeg','.bmp','.tiff']
176 def _doIndexContent(self,content):
180 cntIndex.register(ImageNoIndex())
182 # other opendocument formats:
183 # chart-template chart database
184 # formula-template formula graphics-template graphics
186 # presentation-template presentation spreadsheet-template spreadsheet
188 class OpenDoc(indexer):
189 """ Index OpenDocument files.
191 Q: is it really worth it to index spreadsheets, or do we only get a
192 meaningless list of numbers (cell contents) ?
194 def _getMimeTypes(self):
195 otypes = [ 'text', 'text-web', 'text-template', 'text-master' ]
196 return map(lambda a: 'application/vnd.oasis.opendocument.'+a, otypes)
198 def _getExtensions(self):
199 return ['.odt', '.ott', ] # '.ods'
201 def _doIndexContent(self, content):
202 s = StringIO.StringIO(content)
203 o = odt2txt.OpenDocumentTextFile(s)
204 result = _to_unicode(o.toString())
208 cntIndex.register(OpenDoc())