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:
deeplow 2023-06-19 11:59:44 +01:00
parent 206c262554
commit bf38c24d99
No known key found for this signature in database
GPG key ID: 577982871529A52A
5 changed files with 17 additions and 33 deletions

View file

@ -680,11 +680,11 @@ class ConvertTask(QtCore.QObject):
self.dangerzone.isolation_provider.convert( self.dangerzone.isolation_provider.convert(
self.document, self.document,
self.ocr_lang, self.ocr_lang,
self.stdout_callback, self.progress_callback,
) )
self.finished.emit(self.error) 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: if error:
self.error = True self.error = True

View file

@ -23,11 +23,12 @@ class IsolationProvider(ABC):
self, self,
document: Document, document: Document,
ocr_lang: Optional[str], ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None, progress_callback: Optional[Callable] = None,
) -> None: ) -> None:
self.progress_callback = progress_callback
document.mark_as_converting() document.mark_as_converting()
try: try:
success = self._convert(document, ocr_lang, stdout_callback) success = self._convert(document, ocr_lang)
except Exception: except Exception:
success = False success = False
log.exception( log.exception(
@ -45,7 +46,6 @@ class IsolationProvider(ABC):
self, self,
document: Document, document: Document,
ocr_lang: Optional[str], ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool: ) -> bool:
pass pass
@ -61,6 +61,9 @@ class IsolationProvider(ABC):
s += Style.RESET_ALL + text s += Style.RESET_ALL + text
log.info(s) log.info(s)
if self.progress_callback:
self.progress_callback(error, text, percentage)
@abstractmethod @abstractmethod
def get_max_parallel_conversions(self) -> int: def get_max_parallel_conversions(self) -> int:
pass pass

View file

@ -133,27 +133,24 @@ class Container(IsolationProvider):
return installed 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. Parses a line returned by the container.
""" """
try: try:
status = json.loads(line) status = json.loads(line)
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( self.print_progress(
document, status["error"], status["text"], status["percentage"] document, status["error"], status["text"], status["percentage"]
) )
return (status["error"], status["text"], status["percentage"]) except:
error_message = f"Invalid JSON returned from container:\n\n\t {line}"
log.error(error_message)
self.print_progress(document, True, error_message, -1)
def exec( def exec(
self, self,
document: Document, document: Document,
args: List[str], args: List[str],
stdout_callback: Optional[Callable] = None,
) -> int: ) -> int:
args_str = " ".join(pipes.quote(s) for s in args) args_str = " ".join(pipes.quote(s) for s in args)
log.info("> " + args_str) log.info("> " + args_str)
@ -169,9 +166,7 @@ class Container(IsolationProvider):
) as p: ) as p:
if p.stdout is not None: if p.stdout is not None:
for line in p.stdout: for line in p.stdout:
(error, text, percentage) = self.parse_progress(document, line) self.parse_progress(document, line)
if stdout_callback:
stdout_callback(error, text, percentage)
p.communicate() p.communicate()
return p.returncode return p.returncode
@ -181,7 +176,6 @@ class Container(IsolationProvider):
document: Document, document: Document,
command: List[str], command: List[str],
extra_args: List[str] = [], extra_args: List[str] = [],
stdout_callback: Optional[Callable] = None,
) -> int: ) -> int:
container_runtime = self.get_runtime() container_runtime = self.get_runtime()
@ -208,13 +202,12 @@ class Container(IsolationProvider):
) )
args = [container_runtime] + args args = [container_runtime] + args
return self.exec(document, args, stdout_callback) return self.exec(document, args)
def _convert( def _convert(
self, self,
document: Document, document: Document,
ocr_lang: Optional[str], ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool: ) -> bool:
# Create a temporary directory inside the cache directory for this run. Then, # Create a temporary directory inside the cache directory for this run. Then,
# create some subdirectories for the various stages of the file conversion: # create some subdirectories for the various stages of the file conversion:
@ -237,7 +230,6 @@ class Container(IsolationProvider):
pixel_dir=pixel_dir, pixel_dir=pixel_dir,
safe_dir=safe_dir, safe_dir=safe_dir,
ocr_lang=ocr_lang, ocr_lang=ocr_lang,
stdout_callback=stdout_callback,
) )
def _convert_with_tmpdirs( def _convert_with_tmpdirs(
@ -247,7 +239,6 @@ class Container(IsolationProvider):
pixel_dir: pathlib.Path, pixel_dir: pathlib.Path,
safe_dir: pathlib.Path, safe_dir: pathlib.Path,
ocr_lang: Optional[str], ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool: ) -> bool:
success = False success = False
@ -273,7 +264,7 @@ class Container(IsolationProvider):
"-e", "-e",
f"ENABLE_TIMEOUTS={self.enable_timeouts}", 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: if ret != 0:
log.error("documents-to-pixels failed") log.error("documents-to-pixels failed")
else: else:
@ -297,7 +288,7 @@ class Container(IsolationProvider):
"-e", "-e",
f"ENABLE_TIMEOUTS={self.enable_timeouts}", 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: if ret != 0:
log.error("pixels-to-pdf failed") log.error("pixels-to-pdf failed")
else: else:

View file

@ -34,7 +34,6 @@ class Dummy(IsolationProvider):
self, self,
document: Document, document: Document,
ocr_lang: Optional[str], ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool: ) -> bool:
log.debug("Dummy converter started:") log.debug("Dummy converter started:")
log.debug( log.debug(
@ -58,8 +57,6 @@ class Dummy(IsolationProvider):
for error, text, percentage in progress: for error, text, percentage in progress:
self.print_progress(document, error, text, percentage) # type: ignore [arg-type] self.print_progress(document, error, text, percentage) # type: ignore [arg-type]
if stdout_callback:
stdout_callback(error, text, percentage)
if error: if error:
success = False success = False
time.sleep(0.2) time.sleep(0.2)

View file

@ -49,7 +49,6 @@ class Qubes(IsolationProvider):
self, self,
document: Document, document: Document,
ocr_lang: Optional[str], ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool: ) -> bool:
success = False success = False
@ -121,14 +120,10 @@ class Qubes(IsolationProvider):
text = f"Converting page {page}/{n_pages} to pixels" text = f"Converting page {page}/{n_pages} to pixels"
self.print_progress(document, False, text, percentage) self.print_progress(document, False, text, percentage)
if stdout_callback:
stdout_callback(False, text, percentage)
# TODO handle leftover code input # TODO handle leftover code input
text = "Converted document to pixels" text = "Converted document to pixels"
self.print_progress(document, False, text, percentage) self.print_progress(document, False, text, percentage)
if stdout_callback:
stdout_callback(False, text, percentage)
# FIXME pass OCR stuff properly (see #455) # FIXME pass OCR stuff properly (see #455)
old_environ = dict(os.environ) old_environ = dict(os.environ)
@ -143,8 +138,6 @@ class Qubes(IsolationProvider):
percentage = 100.0 percentage = 100.0
text = "Safe PDF created" text = "Safe PDF created"
self.print_progress(document, False, text, percentage) 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 # FIXME remove once the OCR args are no longer passed with env vars
os.environ.clear() os.environ.clear()