De-duplicate print_progress() logic

This commit is contained in:
deeplow 2023-01-16 12:58:03 +00:00
parent a339eff648
commit f5c4847af2
No known key found for this signature in database
GPG key ID: 577982871529A52A
3 changed files with 18 additions and 25 deletions

View file

@ -3,6 +3,8 @@ import subprocess
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Callable, Optional from typing import Callable, Optional
from colorama import Fore, Style
from ..document import Document from ..document import Document
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -41,6 +43,18 @@ class IsolationProvider(ABC):
) -> bool: ) -> bool:
pass 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 @abstractmethod
def get_max_parallel_conversions(self) -> int: def get_max_parallel_conversions(self) -> int:
pass pass

View file

@ -10,7 +10,6 @@ import tempfile
from typing import Callable, List, Optional, Tuple from typing import Callable, List, Optional, Tuple
import appdirs import appdirs
from colorama import Fore, Style
from ..document import Document from ..document import Document
from ..util import get_resource_path, get_subprocess_startupinfo from ..util import get_resource_path, get_subprocess_startupinfo
@ -142,15 +141,9 @@ class Container(IsolationProvider):
log.error(error_message) log.error(error_message)
return (True, error_message, -1) return (True, error_message, -1)
s = Style.BRIGHT + Fore.YELLOW + f"[doc {document.id}] " self.print_progress(
s += Fore.CYAN + f"{status['percentage']}% " document, status["error"], status["text"], 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)
return (status["error"], status["text"], status["percentage"]) return (status["error"], status["text"], status["percentage"])
def exec( def exec(

View file

@ -5,8 +5,6 @@ import sys
import time import time
from typing import Callable, Optional from typing import Callable, Optional
from colorama import Fore, Style
from ..document import Document from ..document import Document
from ..util import get_resource_path from ..util import get_resource_path
from .base import IsolationProvider from .base import IsolationProvider
@ -59,7 +57,7 @@ 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: if stdout_callback:
stdout_callback(error, text, percentage) stdout_callback(error, text, percentage)
if error: if error:
@ -73,17 +71,5 @@ class Dummy(IsolationProvider):
return success 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: def get_max_parallel_conversions(self) -> int:
return 1 return 1