Skip to content

Commit

Permalink
Merge stdout_callback with print_progress
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
deeplow committed Jun 19, 2023
1 parent a7403ee commit 3483542
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
4 changes: 2 additions & 2 deletions dangerzone/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions dangerzone/isolation_provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -45,7 +46,6 @@ def _convert(
self,
document: Document,
ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool:
pass

Expand All @@ -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
Expand Down
18 changes: 5 additions & 13 deletions dangerzone/isolation_provider/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ 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.
"""
Expand All @@ -147,13 +147,11 @@ def parse_progress(self, document: Document, line: str) -> Tuple[bool, str, int]
self.print_progress(
document, status["error"], status["text"], status["percentage"]
)
return (status["error"], status["text"], status["percentage"])

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)
Expand All @@ -169,9 +167,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
Expand All @@ -181,7 +177,6 @@ def exec_container(
document: Document,
command: List[str],
extra_args: List[str] = [],
stdout_callback: Optional[Callable] = None,
) -> int:
container_runtime = self.get_runtime()

Expand All @@ -208,13 +203,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:
Expand All @@ -237,7 +231,6 @@ def _convert(
pixel_dir=pixel_dir,
safe_dir=safe_dir,
ocr_lang=ocr_lang,
stdout_callback=stdout_callback,
)

def _convert_with_tmpdirs(
Expand All @@ -247,7 +240,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

Expand All @@ -272,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:
Expand All @@ -295,7 +287,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:
Expand Down
3 changes: 0 additions & 3 deletions dangerzone/isolation_provider/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions dangerzone/isolation_provider/qubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def _convert(
self,
document: Document,
ocr_lang: Optional[str],
stdout_callback: Optional[Callable] = None,
) -> bool:
success = False

Expand Down Expand Up @@ -137,14 +136,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
old_environ = dict(os.environ)
Expand All @@ -159,8 +154,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()
Expand Down

0 comments on commit 3483542

Please sign in to comment.