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 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

View file

@ -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(

View file

@ -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