diff --git a/dangerzone/isolation_provider/base.py b/dangerzone/isolation_provider/base.py index 06c552e..d845d70 100644 --- a/dangerzone/isolation_provider/base.py +++ b/dangerzone/isolation_provider/base.py @@ -3,6 +3,8 @@ import subprocess from abc import ABC, abstractmethod from typing import Callable, Optional +from colorama import Fore, Style + from ..document import Document log = logging.getLogger(__name__) @@ -41,6 +43,18 @@ class IsolationProvider(ABC): ) -> bool: pass + def print_progress( + self, document: Document, error: bool, text: str, percentage: float + ) -> None: + s = Style.BRIGHT + Fore.YELLOW + f"[doc {document.id}] " + s += Fore.CYAN + f"{percentage}% " + if error: + s += Style.RESET_ALL + Fore.RED + text + log.error(s) + else: + s += Style.RESET_ALL + text + log.info(s) + @abstractmethod def get_max_parallel_conversions(self) -> int: pass diff --git a/dangerzone/isolation_provider/container.py b/dangerzone/isolation_provider/container.py index 46db661..5d84b4c 100644 --- a/dangerzone/isolation_provider/container.py +++ b/dangerzone/isolation_provider/container.py @@ -10,7 +10,6 @@ import tempfile from typing import Callable, List, Optional, Tuple import appdirs -from colorama import Fore, Style from ..document import Document from ..util import get_resource_path, get_subprocess_startupinfo @@ -142,15 +141,9 @@ class Container(IsolationProvider): log.error(error_message) return (True, error_message, -1) - s = Style.BRIGHT + Fore.YELLOW + f"[doc {document.id}] " - s += Fore.CYAN + f"{status['percentage']}% " - if status["error"]: - s += Style.RESET_ALL + Fore.RED + status["text"] - log.error(s) - else: - s += Style.RESET_ALL + status["text"] - log.info(s) - + self.print_progress( + document, status["error"], status["text"], status["percentage"] + ) return (status["error"], status["text"], status["percentage"]) def exec( diff --git a/dangerzone/isolation_provider/dummy.py b/dangerzone/isolation_provider/dummy.py index 03529fe..c2c6d66 100644 --- a/dangerzone/isolation_provider/dummy.py +++ b/dangerzone/isolation_provider/dummy.py @@ -5,8 +5,6 @@ import sys import time from typing import Callable, Optional -from colorama import Fore, Style - from ..document import Document from ..util import get_resource_path from .base import IsolationProvider @@ -59,7 +57,7 @@ class Dummy(IsolationProvider): ] 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: @@ -73,17 +71,5 @@ class Dummy(IsolationProvider): return success - def _print_progress( - self, document: Document, error: bool, text: str, percentage: float - ) -> None: - s = Style.BRIGHT + Fore.YELLOW + f"[doc {document.id}] " - s += Fore.CYAN + f"{percentage}% " - if error: - s += Style.RESET_ALL + Fore.RED + text - log.error(s) - else: - s += Style.RESET_ALL + text - log.info(s) - def get_max_parallel_conversions(self) -> int: return 1