Skip to content

Commit

Permalink
[IMP] Add conversion exception message to feedback
Browse files Browse the repository at this point in the history
[IMP] Ghostscriptbackend add exception message passtrough

[IMP] Error tests and feedback message
  • Loading branch information
bosd committed Oct 14, 2024
1 parent aa61d3a commit 4b86f60
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion camelot/backends/ghostscript_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def convert(self, pdf_path: str, png_path: str, resolution: int = 300) -> None:
"""
try:
import ghostscript # type: ignore[import-untyped]
except RuntimeError:
except ModuleNotFoundError as ex:
raise OSError(
"Ghostscript is not installed. You can install it using the instructions"
" here: https://pypdf-table-extraction.readthedocs.io/en/latest/user/install-deps.html"
Expand Down
4 changes: 2 additions & 2 deletions camelot/backends/image_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def convert(self, pdf_path: str, png_path: str) -> None:
converter = BACKENDS[fallback]()
converter.convert(pdf_path, png_path)
except Exception as e:
msg = f"Image conversion failed with image conversion backend {fallback!r}"
msg = f"Image conversion failed with image conversion backend {fallback!r}\n error: {e}"
raise ImageConversionError(msg) from e
else:
break
else:
msg = f"Image conversion failed with image conversion backend {self.backend!r}"
msg = f"Image conversion failed with image conversion backend {self.backend!r}\n error: {f}"
raise ImageConversionError(msg) from f
16 changes: 10 additions & 6 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ def test_url_ghostscript(testdir):
@skip_pdftopng
def test_pages_poppler():
url = "https://pypdf-table-extraction.readthedocs.io/en/latest/_static/pdf/foo.pdf"
tables = camelot.read_pdf(url, backend="poppler")
tables = camelot.read_pdf(url, backend="poppler", use_fallback=False)
assert repr(tables) == "<TableList n=1>"
assert repr(tables[0]) == "<Table shape=(7, 7)>"
assert repr(tables[0].cells[0][0]) == "<Cell x1=120 y1=219 x2=165 y2=234>"

tables = camelot.read_pdf(url, pages="1-end", backend="poppler")
tables = camelot.read_pdf(url, pages="1-end", backend="poppler", use_fallback=False)
assert repr(tables) == "<TableList n=1>"
assert repr(tables[0]) == "<Table shape=(7, 7)>"
assert repr(tables[0].cells[0][0]) == "<Cell x1=120 y1=219 x2=165 y2=234>"

tables = camelot.read_pdf(url, pages="all", backend="poppler")
tables = camelot.read_pdf(url, pages="all", backend="poppler", use_fallback=False)
assert repr(tables) == "<TableList n=1>"
assert repr(tables[0]) == "<Table shape=(7, 7)>"
assert repr(tables[0].cells[0][0]) == "<Cell x1=120 y1=219 x2=165 y2=234>"
Expand All @@ -92,17 +92,21 @@ def test_pages_poppler():
@skip_on_windows
def test_pages_ghostscript():
url = "https://pypdf-table-extraction.readthedocs.io/en/latest/_static/pdf/foo.pdf"
tables = camelot.read_pdf(url, backend="ghostscript")
tables = camelot.read_pdf(url, backend="ghostscript", use_fallback=False)
assert repr(tables) == "<TableList n=1>"
assert repr(tables[0]) == "<Table shape=(7, 7)>"
assert repr(tables[0].cells[0][0]) == "<Cell x1=120 y1=218 x2=165 y2=234>"

tables = camelot.read_pdf(url, pages="1-end", backend="ghostscript")
tables = camelot.read_pdf(
url, pages="1-end", backend="ghostscript", use_fallback=False
)
assert repr(tables) == "<TableList n=1>"
assert repr(tables[0]) == "<Table shape=(7, 7)>"
assert repr(tables[0].cells[0][0]) == "<Cell x1=120 y1=218 x2=165 y2=234>"

tables = camelot.read_pdf(url, pages="all", backend="ghostscript")
tables = camelot.read_pdf(
url, pages="all", backend="ghostscript", use_fallback=False
)
assert repr(tables) == "<TableList n=1>"
assert repr(tables[0]) == "<Table shape=(7, 7)>"
assert repr(tables[0].cells[0][0]) == "<Cell x1=120 y1=218 x2=165 y2=234>"
Expand Down
25 changes: 23 additions & 2 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import sys
import warnings
from unittest import mock

import pytest

import camelot
from camelot.backends.image_conversion import ImageConversionError
from camelot.utils import is_url
from tests.conftest import skip_on_windows

Expand Down Expand Up @@ -122,7 +125,9 @@ def test_lattice_no_tables_on_page(testdir):
def test_lattice_unknown_backend(foo_pdf):
message = "Unknown backend 'mupdf' specified. Please use either 'poppler' or 'ghostscript'."
with pytest.raises(NotImplementedError, match=message):
tables = camelot.read_pdf(foo_pdf, backend="mupdf")
tables = camelot.read_pdf(
foo_pdf, flavor="lattice", backend="mupdf", use_fallback=False
)


def test_lattice_no_convert_method(foo_pdf):
Expand All @@ -131,7 +136,9 @@ class ConversionBackend:

message = "must implement a 'convert' method"
with pytest.raises(NotImplementedError, match=message):
camelot.read_pdf(foo_pdf, backend=ConversionBackend())
camelot.read_pdf(
foo_pdf, flavor="lattice", backend=ConversionBackend(), use_fallback=False
)


def test_invalid_url():
Expand All @@ -140,3 +147,17 @@ def test_invalid_url():
with pytest.raises(Exception, match=message):
url = camelot.read_pdf(url)
assert is_url(url) == False


def test_ghostscript_backend_import_error(testdir):
filename = os.path.join(testdir, "table_region.pdf")
with mock.patch.dict(sys.modules, {"ghostscript": None}):
message = "Ghostscript is not installed"
with pytest.raises(ImageConversionError) as e:
tables = camelot.read_pdf(
filename,
flavor="lattice",
backend="ghostscript",
use_fallback=False,
)
assert message in str(e.value)
2 changes: 1 addition & 1 deletion tests/test_image_conversion_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ def test_ghostscript_backend_error_when_use_fallback(monkeypatch):
)
backend = ImageConversionBackend(backend="ghostscript")

message = "Image conversion failed with image conversion backend 'ghostscript'"
message = "Image conversion failed with image conversion backend 'poppler'\n error: Image conversion failed"
with pytest.raises(ValueError, match=message):
backend.convert("foo", "bar")

0 comments on commit 4b86f60

Please sign in to comment.