mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 10:12:38 +02:00
Merge stdout_callback with print_progress
stdout_callback is used to flow progress information from the conversion to some front-end. It was always used in tandem with printing to the terminal (which is kind of a front-end). So it made sense to put them always together.
This commit is contained in:
parent
206c262554
commit
bf38c24d99
5 changed files with 17 additions and 33 deletions
|
@ -680,11 +680,11 @@ class ConvertTask(QtCore.QObject):
|
|||
self.dangerzone.isolation_provider.convert(
|
||||
self.document,
|
||||
self.ocr_lang,
|
||||
self.stdout_callback,
|
||||
self.progress_callback,
|
||||
)
|
||||
self.finished.emit(self.error)
|
||||
|
||||
def stdout_callback(self, error: bool, text: str, percentage: int) -> None:
|
||||
def progress_callback(self, error: bool, text: str, percentage: int) -> None:
|
||||
if error:
|
||||
self.error = True
|
||||
|
||||
|
|
|
@ -23,11 +23,12 @@ class IsolationProvider(ABC):
|
|||
self,
|
||||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
progress_callback: Optional[Callable] = None,
|
||||
) -> None:
|
||||
self.progress_callback = progress_callback
|
||||
document.mark_as_converting()
|
||||
try:
|
||||
success = self._convert(document, ocr_lang, stdout_callback)
|
||||
success = self._convert(document, ocr_lang)
|
||||
except Exception:
|
||||
success = False
|
||||
log.exception(
|
||||
|
@ -45,7 +46,6 @@ class IsolationProvider(ABC):
|
|||
self,
|
||||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
pass
|
||||
|
||||
|
@ -61,6 +61,9 @@ class IsolationProvider(ABC):
|
|||
s += Style.RESET_ALL + text
|
||||
log.info(s)
|
||||
|
||||
if self.progress_callback:
|
||||
self.progress_callback(error, text, percentage)
|
||||
|
||||
@abstractmethod
|
||||
def get_max_parallel_conversions(self) -> int:
|
||||
pass
|
||||
|
|
|
@ -133,27 +133,24 @@ class Container(IsolationProvider):
|
|||
|
||||
return installed
|
||||
|
||||
def parse_progress(self, document: Document, line: str) -> Tuple[bool, str, int]:
|
||||
def parse_progress(self, document: Document, line: str) -> None:
|
||||
"""
|
||||
Parses a line returned by the container.
|
||||
"""
|
||||
try:
|
||||
status = json.loads(line)
|
||||
self.print_progress(
|
||||
document, status["error"], status["text"], status["percentage"]
|
||||
)
|
||||
except:
|
||||
error_message = f"Invalid JSON returned from container:\n\n\t {line}"
|
||||
log.error(error_message)
|
||||
return (True, error_message, -1)
|
||||
|
||||
self.print_progress(
|
||||
document, status["error"], status["text"], status["percentage"]
|
||||
)
|
||||
return (status["error"], status["text"], status["percentage"])
|
||||
self.print_progress(document, True, error_message, -1)
|
||||
|
||||
def exec(
|
||||
self,
|
||||
document: Document,
|
||||
args: List[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> int:
|
||||
args_str = " ".join(pipes.quote(s) for s in args)
|
||||
log.info("> " + args_str)
|
||||
|
@ -169,9 +166,7 @@ class Container(IsolationProvider):
|
|||
) as p:
|
||||
if p.stdout is not None:
|
||||
for line in p.stdout:
|
||||
(error, text, percentage) = self.parse_progress(document, line)
|
||||
if stdout_callback:
|
||||
stdout_callback(error, text, percentage)
|
||||
self.parse_progress(document, line)
|
||||
|
||||
p.communicate()
|
||||
return p.returncode
|
||||
|
@ -181,7 +176,6 @@ class Container(IsolationProvider):
|
|||
document: Document,
|
||||
command: List[str],
|
||||
extra_args: List[str] = [],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> int:
|
||||
container_runtime = self.get_runtime()
|
||||
|
||||
|
@ -208,13 +202,12 @@ class Container(IsolationProvider):
|
|||
)
|
||||
|
||||
args = [container_runtime] + args
|
||||
return self.exec(document, args, stdout_callback)
|
||||
return self.exec(document, args)
|
||||
|
||||
def _convert(
|
||||
self,
|
||||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
# Create a temporary directory inside the cache directory for this run. Then,
|
||||
# create some subdirectories for the various stages of the file conversion:
|
||||
|
@ -237,7 +230,6 @@ class Container(IsolationProvider):
|
|||
pixel_dir=pixel_dir,
|
||||
safe_dir=safe_dir,
|
||||
ocr_lang=ocr_lang,
|
||||
stdout_callback=stdout_callback,
|
||||
)
|
||||
|
||||
def _convert_with_tmpdirs(
|
||||
|
@ -247,7 +239,6 @@ class Container(IsolationProvider):
|
|||
pixel_dir: pathlib.Path,
|
||||
safe_dir: pathlib.Path,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
success = False
|
||||
|
||||
|
@ -273,7 +264,7 @@ class Container(IsolationProvider):
|
|||
"-e",
|
||||
f"ENABLE_TIMEOUTS={self.enable_timeouts}",
|
||||
]
|
||||
ret = self.exec_container(document, command, extra_args, stdout_callback)
|
||||
ret = self.exec_container(document, command, extra_args)
|
||||
if ret != 0:
|
||||
log.error("documents-to-pixels failed")
|
||||
else:
|
||||
|
@ -297,7 +288,7 @@ class Container(IsolationProvider):
|
|||
"-e",
|
||||
f"ENABLE_TIMEOUTS={self.enable_timeouts}",
|
||||
]
|
||||
ret = self.exec_container(document, command, extra_args, stdout_callback)
|
||||
ret = self.exec_container(document, command, extra_args)
|
||||
if ret != 0:
|
||||
log.error("pixels-to-pdf failed")
|
||||
else:
|
||||
|
|
|
@ -34,7 +34,6 @@ class Dummy(IsolationProvider):
|
|||
self,
|
||||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
log.debug("Dummy converter started:")
|
||||
log.debug(
|
||||
|
@ -58,8 +57,6 @@ class Dummy(IsolationProvider):
|
|||
|
||||
for error, text, percentage in progress:
|
||||
self.print_progress(document, error, text, percentage) # type: ignore [arg-type]
|
||||
if stdout_callback:
|
||||
stdout_callback(error, text, percentage)
|
||||
if error:
|
||||
success = False
|
||||
time.sleep(0.2)
|
||||
|
|
|
@ -49,7 +49,6 @@ class Qubes(IsolationProvider):
|
|||
self,
|
||||
document: Document,
|
||||
ocr_lang: Optional[str],
|
||||
stdout_callback: Optional[Callable] = None,
|
||||
) -> bool:
|
||||
success = False
|
||||
|
||||
|
@ -121,14 +120,10 @@ class Qubes(IsolationProvider):
|
|||
|
||||
text = f"Converting page {page}/{n_pages} to pixels"
|
||||
self.print_progress(document, False, text, percentage)
|
||||
if stdout_callback:
|
||||
stdout_callback(False, text, percentage)
|
||||
|
||||
# TODO handle leftover code input
|
||||
text = "Converted document to pixels"
|
||||
self.print_progress(document, False, text, percentage)
|
||||
if stdout_callback:
|
||||
stdout_callback(False, text, percentage)
|
||||
|
||||
# FIXME pass OCR stuff properly (see #455)
|
||||
old_environ = dict(os.environ)
|
||||
|
@ -143,8 +138,6 @@ class Qubes(IsolationProvider):
|
|||
percentage = 100.0
|
||||
text = "Safe PDF created"
|
||||
self.print_progress(document, False, text, percentage)
|
||||
if stdout_callback:
|
||||
stdout_callback(False, text, percentage)
|
||||
|
||||
# FIXME remove once the OCR args are no longer passed with env vars
|
||||
os.environ.clear()
|
||||
|
|
Loading…
Reference in a new issue