diff options
| author | Harry (Open ERP) <hmo@tinyerp.com> | 2009-12-02 11:40:42 +0530 |
|---|---|---|
| committer | Harry (Open ERP) <hmo@tinyerp.com> | 2009-12-02 11:40:42 +0530 |
| commit | 349fdf9bfa6e37dab80ee1ae84667b4485584166 (patch) | |
| tree | ddf2933a9065712494a1d20c98f312447c9be390 /addons/document/std_index.py | |
| parent | bcddba076258a31df689eb63b8a8f35acfeffe89 (diff) | |
| parent | 36c6824e5c14c3766f194ecc4eed6f1324313cfc (diff) | |
[MERGE]
bzr revid: hmo@tinyerp.com-20091202061042-104crbh2v0b4yzux
Diffstat (limited to 'addons/document/std_index.py')
| -rw-r--r-- | addons/document/std_index.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/addons/document/std_index.py b/addons/document/std_index.py new file mode 100644 index 00000000000..730e5082a1b --- /dev/null +++ b/addons/document/std_index.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +############################################################################## + +import StringIO +import odt2txt + +from content_index import indexer, cntIndex + + +def _to_unicode(s): + try: + return s.decode('utf-8') + except UnicodeError: + try: + return s.decode('latin') + except UnicodeError: + try: + return s.encode('ascii') + except UnicodeError: + return s + +class TxtIndex(indexer): + def _getMimeTypes(self): + return ['text/plain','text/html','text/diff','text/xml', 'text/*', + 'application/xml'] + + def _getExtensions(self): + return ['.txt', '.py'] + + def _doIndexContent(self,content): + return content + +cntIndex.register(TxtIndex()) + +class DocIndex(indexer): + def _getMimeTypes(self): + return [ 'application/ms-word'] + + def _getExtensions(self): + return ['.doc'] + + def _doIndexFile(self,fname): + fp = Popen(['antiword',fname], shell=False, stdout=PIPE).stdout + return _to_unicode( fp.read()) + +cntIndex.register(DocIndex()) + +class PdfIndex(indexer): + def _getMimeTypes(self): + return [ 'application/pdf'] + + def _getExtensions(self): + return ['.pdf'] + + def _doIndexFile(self,fname): + fp = Popen(['pdftotext', '-enc', 'UTF-8', '-nopgbrk', fname, '-'], shell=False, stdout=PIPE).stdout + return _to_unicode( fp.read()) + +cntIndex.register(PdfIndex()) + +class ImageNoIndex(indexer): + def _getMimeTypes(self): + return [ 'image/*'] + + def _getExtensions(self): + #better return no extension, and let 'file' do its magic + return [] + #return ['.png','.jpg','.gif','.jpeg','.bmp','.tiff'] + + def _doIndexContent(self,content): + return 'image' + + +cntIndex.register(ImageNoIndex()) + +#class Doc(indexer): + #def _getDefMime(self,ext): + +#def content_index(content, filename=None, content_type=None): + #fname,ext = os.path.splitext(filename) + #result = '' + #elif ext in ('.xls','.ods','.odt','.odp'): + #s = StringIO.StringIO(content) + #o = odt2txt.OpenDocumentTextFile(s) + #result = _to_unicode(o.toString()) + #s.close() + #elif ext in ('.txt','.py','.patch','.html','.csv','.xml'): + #result = content + #else: + #result = content + #return result + +#eof |
