From 6fa4dbf2047f5e531257876c14a419da67df3f79 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Thu, 30 Jan 2020 14:50:10 +0000 Subject: [PATCH] [FIX] attachement_indexation: make pdfminer optional As pdfminer does not have a Debian package in Ubuntu Bionic, it cannot be declared as a strong requirement. With this commit, a warning is logged if the library is not installed. It does not prevent to index other types of documents. closes odoo/odoo#44327 Signed-off-by: Christophe Monniez (moc) --- addons/attachment_indexation/__manifest__.py | 4 +++- .../models/ir_attachment.py | 16 +++++++++++++--- .../tests/test_indexation.py | 7 +++++++ requirements.txt | 1 - 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/addons/attachment_indexation/__manifest__.py b/addons/attachment_indexation/__manifest__.py index 325590158e7..ff75a99a083 100644 --- a/addons/attachment_indexation/__manifest__.py +++ b/addons/attachment_indexation/__manifest__.py @@ -8,7 +8,9 @@ Attachments list and document indexation ======================================== * Show attachment on the top of the forms -* Document Indexation: odt +* Document Indexation: odt, pdf, xlsx, docx + +The `pdfminer.six` Python library has to be installed in order to index PDF files """, 'depends': ['web'], 'installable': True, diff --git a/addons/attachment_indexation/models/ir_attachment.py b/addons/attachment_indexation/models/ir_attachment.py index 11152b6c2ea..05394e12698 100644 --- a/addons/attachment_indexation/models/ir_attachment.py +++ b/addons/attachment_indexation/models/ir_attachment.py @@ -4,15 +4,23 @@ import io import logging import xml.dom.minidom import zipfile -from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter -from pdfminer.converter import TextConverter -from pdfminer.pdfpage import PDFPage from odoo import api, models _logger = logging.getLogger(__name__) + +try: + from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter + from pdfminer.converter import TextConverter + from pdfminer.pdfpage import PDFPage +except ImportError: + PDFResourceManager = PDFPageInterpreter = TextConverter = PDFPage = None + _logger.warning("Attachment indexation of PDF documents is unavailable because the 'pdfminer' Python library cannot be found on the system. " + "You may install it from https://pypi.org/project/pdfminer.six/ (e.g. `pip3 install pdfminer.six`)") + FTYPES = ['docx', 'pptx', 'xlsx', 'opendoc', 'pdf'] + def textToString(element): buff = u"" for node in element.childNodes: @@ -93,6 +101,8 @@ class IrAttachment(models.Model): def _index_pdf(self, bin_data): '''Index PDF documents''' + if PDFResourceManager is None: + return buf = u"" if bin_data.startswith(b'%PDF-'): f = io.BytesIO(bin_data) diff --git a/addons/attachment_indexation/tests/test_indexation.py b/addons/attachment_indexation/tests/test_indexation.py index 9f5fa0eca15..36c558cdcc1 100644 --- a/addons/attachment_indexation/tests/test_indexation.py +++ b/addons/attachment_indexation/tests/test_indexation.py @@ -1,14 +1,21 @@ # -*- coding: utf-8 -*- from odoo.tests.common import TransactionCase, tagged +from unittest import skipIf import os directory = os.path.dirname(__file__) +try: + from pdfminer.pdfinterp import PDFResourceManager +except ImportError: + PDFResourceManager = None + @tagged('post_install', '-at_install') class TestCaseIndexation(TransactionCase): + @skipIf(PDFResourceManager is None, "pdfminer not installed") def test_attachment_pdf_indexation(self): with open(os.path.join(directory, 'files', 'test_content.pdf'), 'rb') as file: pdf = file.read() diff --git a/requirements.txt b/requirements.txt index 2507a3d10fa..f6057683875 100644 --- a/requirements.txt +++ b/requirements.txt @@ -21,7 +21,6 @@ mock==2.0.0 num2words==0.5.6 ofxparse==0.19 passlib==1.7.1 -pdfminer.six==20181108 Pillow==5.4.1 ; python_version < '3.7' or sys_platform != 'win32' Pillow==6.1.0 ; sys_platform == 'win32' and python_version >= '3.7' polib==1.1.0