diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index a4b17866b..7a99b3f2e 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -680,11 +680,11 @@ def convert_document(self) -> None: 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 diff --git a/dangerzone/isolation_provider/base.py b/dangerzone/isolation_provider/base.py index ef2c740ad..9f55ca810 100644 --- a/dangerzone/isolation_provider/base.py +++ b/dangerzone/isolation_provider/base.py @@ -23,11 +23,12 @@ def convert( 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 @@ def _convert( self, document: Document, ocr_lang: Optional[str], - stdout_callback: Optional[Callable] = None, ) -> bool: pass @@ -61,6 +61,9 @@ def print_progress( 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 diff --git a/dangerzone/isolation_provider/container.py b/dangerzone/isolation_provider/container.py index ed5f38f22..2873ef3fb 100644 --- a/dangerzone/isolation_provider/container.py +++ b/dangerzone/isolation_provider/container.py @@ -133,27 +133,24 @@ def is_container_installed() -> bool: 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 @@ def exec( ) 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 @@ def exec_container( document: Document, command: List[str], extra_args: List[str] = [], - stdout_callback: Optional[Callable] = None, ) -> int: container_runtime = self.get_runtime() @@ -208,13 +202,12 @@ def exec_container( ) 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 @@ def _convert( pixel_dir=pixel_dir, safe_dir=safe_dir, ocr_lang=ocr_lang, - stdout_callback=stdout_callback, ) def _convert_with_tmpdirs( @@ -247,7 +239,6 @@ def _convert_with_tmpdirs( pixel_dir: pathlib.Path, safe_dir: pathlib.Path, ocr_lang: Optional[str], - stdout_callback: Optional[Callable] = None, ) -> bool: success = False @@ -273,7 +264,7 @@ def _convert_with_tmpdirs( "-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 @@ def _convert_with_tmpdirs( "-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: diff --git a/dangerzone/isolation_provider/dummy.py b/dangerzone/isolation_provider/dummy.py index 16b8967b1..3528656a0 100644 --- a/dangerzone/isolation_provider/dummy.py +++ b/dangerzone/isolation_provider/dummy.py @@ -34,7 +34,6 @@ def _convert( self, document: Document, ocr_lang: Optional[str], - stdout_callback: Optional[Callable] = None, ) -> bool: log.debug("Dummy converter started:") log.debug( @@ -58,8 +57,6 @@ def _convert( 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) diff --git a/dangerzone/isolation_provider/qubes.py b/dangerzone/isolation_provider/qubes.py index 10a976807..2d1ddaf2e 100644 --- a/dangerzone/isolation_provider/qubes.py +++ b/dangerzone/isolation_provider/qubes.py @@ -49,7 +49,6 @@ def _convert( self, document: Document, ocr_lang: Optional[str], - stdout_callback: Optional[Callable] = None, ) -> bool: success = False @@ -121,14 +120,10 @@ def _convert( 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 @@ def _convert( 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()