mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
Isolation-provider-specific methods in _convert()
All isolation providers will some similar steps when convert() is called. For this reason, all the common parts are captured in convert() and then each isolation provider implements its own specific conversion process in _convert() (which is called from the convert() method).
This commit is contained in:
parent
a4f27afdc6
commit
7ed1fd6b59
2 changed files with 18 additions and 11 deletions
|
@ -40,12 +40,27 @@ class AbstractIsolationProvider(ABC):
|
|||
def install(self) -> bool:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def convert(
|
||||
self,
|
||||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> None:
|
||||
document.mark_as_converting()
|
||||
success = self._convert(document, ocr_lang, stdout_callback)
|
||||
if success:
|
||||
document.mark_as_safe()
|
||||
if document.archive_after_conversion:
|
||||
document.archive()
|
||||
else:
|
||||
document.mark_as_failed()
|
||||
|
||||
@abstractmethod
|
||||
def _convert(
|
||||
self,
|
||||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
pass
|
||||
|
||||
|
@ -196,10 +211,6 @@ class Container(AbstractIsolationProvider):
|
|||
if p.stdout is not None:
|
||||
for line in p.stdout:
|
||||
(error, text, percentage) = self.parse_progress(document, line)
|
||||
if error:
|
||||
document.mark_as_failed()
|
||||
if percentage == 100.0:
|
||||
document.mark_as_safe()
|
||||
if stdout_callback:
|
||||
stdout_callback(error, text, percentage)
|
||||
|
||||
|
@ -243,14 +254,13 @@ class Container(AbstractIsolationProvider):
|
|||
args = [container_runtime] + args
|
||||
return self.exec(document, args, stdout_callback)
|
||||
|
||||
def convert(
|
||||
def _convert(
|
||||
self,
|
||||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
success = False
|
||||
document.mark_as_converting()
|
||||
|
||||
if ocr_lang:
|
||||
ocr = "1"
|
||||
|
@ -313,9 +323,6 @@ class Container(AbstractIsolationProvider):
|
|||
)
|
||||
shutil.move(container_output_filename, document.output_filename)
|
||||
|
||||
if document.archive_after_conversion:
|
||||
document.archive()
|
||||
|
||||
# We did it
|
||||
success = True
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ class DangerzoneCore(object):
|
|||
self, ocr_lang: Optional[str], stdout_callback: Optional[Callable] = None
|
||||
) -> None:
|
||||
def convert_doc(document: Document) -> None:
|
||||
success = self.isolation_provider.convert(
|
||||
self.isolation_provider.convert(
|
||||
document,
|
||||
ocr_lang,
|
||||
stdout_callback,
|
||||
|
|
Loading…
Reference in a new issue