forked from camelot-dev/camelot
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
567520b
commit a5be455
Showing
4 changed files
with
53 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class ConversionBackend: | ||
|
||
def installed(self) -> bool: | ||
raise NotImplementedError | ||
|
||
def convert(self, pdf_path: str, png_path: str, resolution: int = 300) -> None: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,49 @@ | ||
from typing import Dict | ||
from typing import List | ||
from typing import Type | ||
|
||
from .base import ConversionBackend | ||
from .ghostscript_backend import GhostscriptBackend | ||
from .poppler_backend import PopplerBackend | ||
|
||
|
||
BACKENDS = {"poppler": PopplerBackend, "ghostscript": GhostscriptBackend} | ||
BACKENDS: Dict[str, Type[ConversionBackend]] = { | ||
"poppler": PopplerBackend, | ||
"ghostscript": GhostscriptBackend, | ||
} | ||
|
||
|
||
class ImageConversionError(Exception): | ||
pass | ||
|
||
|
||
class ImageConversionBackend: | ||
def __init__(self, backend="poppler", use_fallback=True): | ||
def __init__(self, backend: str = "poppler", use_fallback: bool = True) -> None: | ||
if backend not in BACKENDS.keys(): | ||
raise ValueError(f"Image conversion backend '{backend}' not supported") | ||
msg = f"Image conversion backend '{backend!r}' not supported" | ||
raise ValueError(msg) | ||
|
||
self.backend = backend | ||
self.use_fallback = use_fallback | ||
self.fallbacks = list(filter(lambda x: x != backend, BACKENDS.keys())) | ||
self.backend: str = backend | ||
self.use_fallback: bool = use_fallback | ||
self.fallbacks: List[str] = list( | ||
filter(lambda x: x != backend, BACKENDS.keys()) | ||
) | ||
|
||
def convert(self, pdf_path, png_path): | ||
def convert(self, pdf_path: str, png_path: str) -> None: | ||
try: | ||
converter = BACKENDS[self.backend]() | ||
converter.convert(pdf_path, png_path) | ||
except Exception as e: | ||
import sys | ||
|
||
except Exception as f: | ||
if self.use_fallback: | ||
for fallback in self.fallbacks: | ||
try: | ||
converter = BACKENDS[fallback]() | ||
converter.convert(pdf_path, png_path) | ||
except Exception as e: | ||
raise type(e)( | ||
str(e) + f" with image conversion backend '{fallback}'" | ||
).with_traceback(sys.exc_info()[2]) | ||
continue | ||
msg = f" with image conversion backend '{fallback!r}'" | ||
raise ImageConversionError(msg) from e | ||
else: | ||
break | ||
else: | ||
raise type(e)( | ||
str(e) + f" with image conversion backend '{self.backend}'" | ||
).with_traceback(sys.exc_info()[2]) | ||
|
||
msg = f" with image conversion backend '{self.backend!r}'" | ||
raise ImageConversionError(msg) from f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters