diff --git a/pyproject.toml b/pyproject.toml index aae4911a..269d7ad3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,6 @@ dependencies = [ "pluggy>=0.13.0", "reportlab>=3.5.66", "rich>=13", - "tqdm>=4", "typing-extensions>=4;python_version<'3.10'", ] authors = [{ name = "James R. Barlow", email = "james@purplerock.ca" }] diff --git a/src/ocrmypdf/_logging.py b/src/ocrmypdf/_logging.py index 7882a1b0..6a449dd8 100644 --- a/src/ocrmypdf/_logging.py +++ b/src/ocrmypdf/_logging.py @@ -6,7 +6,6 @@ from __future__ import annotations import logging -from contextlib import suppress from rich.console import Console from rich.logging import RichHandler @@ -19,7 +18,6 @@ from rich.progress import ( TimeRemainingColumn, ) from rich.table import Column -from tqdm import tqdm class PageNumberFilter(logging.Filter): @@ -34,26 +32,6 @@ class PageNumberFilter(logging.Filter): return True -class TqdmConsole: - """Wrapper to log messages in a way that is compatible with tqdm progress bar. - - This routes log messages through tqdm so that it can print them above the - progress bar, and then refresh the progress bar, rather than overwriting - it which looks messy. - """ - - def __init__(self, file): - self.file = file - - def write(self, msg): - # When no progress bar is active, tqdm.write() routes to print() - tqdm.write(msg.rstrip(), end='\n', file=self.file) - - def flush(self): - with suppress(AttributeError): - self.file.flush() - - class RichLoggingHandler(RichHandler): def __init__(self, console: Console, **kwargs): super().__init__( diff --git a/src/ocrmypdf/api.py b/src/ocrmypdf/api.py index b0f8c455..4be442d1 100644 --- a/src/ocrmypdf/api.py +++ b/src/ocrmypdf/api.py @@ -15,7 +15,7 @@ from pathlib import Path from typing import AnyStr, BinaryIO, Iterable, Union from warnings import warn -from ocrmypdf._logging import PageNumberFilter, TqdmConsole +from ocrmypdf._logging import PageNumberFilter, RichTqdmProgressAdapter from ocrmypdf._plugin_manager import get_plugin_manager from ocrmypdf._sync import run_pipeline from ocrmypdf._validation import check_options @@ -339,7 +339,6 @@ def ocr( # noqa: ruff: disable=D417 __all__ = [ 'PageNumberFilter', - 'TqdmConsole', 'Verbosity', 'check_options', 'configure_logging', diff --git a/tests/test_api.py b/tests/test_api.py index ee2bd48a..5a6ba124 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -7,48 +7,10 @@ import logging from io import BytesIO, StringIO import pytest -from tqdm import tqdm import ocrmypdf -def test_raw_console(): - bio = StringIO() - tqconsole = ocrmypdf.api.TqdmConsole(file=bio) - tqconsole.write("Test") - tqconsole.flush() - assert "Test" in bio.getvalue() - - -def test_tqdm_console(): - log = logging.getLogger() - log.setLevel(logging.INFO) - - formatter = logging.Formatter('%(message)s') - - bio = StringIO() - console = logging.StreamHandler(ocrmypdf.api.TqdmConsole(file=bio)) - console.setFormatter(formatter) - - log.addHandler(console) - - def before_pbar(message): - # Ensure that log messages appear before the progress bar, even when - # printed after the progress bar updates. - v = bio.getvalue() - pbar_start_marker = '|#' - return v.index(message) < v.index(pbar_start_marker) - - with tqdm(total=2, file=bio, disable=False) as pbar: - pbar.update() - msg = "1/2 above progress bar" - log.info(msg) - assert before_pbar(msg) - - log.info("done") - assert not before_pbar("done") - - def test_language_list(): with pytest.raises( (ocrmypdf.exceptions.InputFileError, ocrmypdf.exceptions.MissingDependencyError)