From ba6d59fd6de6aabeb022876285d1e5fe0498e181 Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Sun, 12 May 2024 15:24:15 -0400 Subject: [PATCH] add null_progress function --- docs/api.rst | 2 ++ progress_api/api.py | 12 +++++++++++- progress_api/backends/enlighten.py | 1 + progress_api/backends/mock.py | 1 + progress_api/backends/null.py | 5 +---- 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index ec09d32..1699d0e 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -11,6 +11,8 @@ Creating Progress Bars .. autofunction:: make_progress +.. autofunction:: null_progress + Progress Bar Interface ---------------------- diff --git a/progress_api/api.py b/progress_api/api.py index 2cb4b2f..6df9a76 100644 --- a/progress_api/api.py +++ b/progress_api/api.py @@ -25,7 +25,6 @@ class Progress(ABC): """ name: str - spec: backends.ProgressBarSpec @abstractmethod def set_label(self, label: Optional[str]) -> None: @@ -147,3 +146,14 @@ def make_progress( spec = backends.ProgressBarSpec(logger, label, total, unit, sl, leave) return config.get_backend().create_bar(spec) + + +def null_progress() -> Progress: + """ + Create a null progress bar, regardless of the configured backend. This is + useful to allow progress reporting to be optional without littering code + with conditionals. + """ + from progress_api.backends.null import NullProgress + + return NullProgress() diff --git a/progress_api/backends/enlighten.py b/progress_api/backends/enlighten.py index 8890900..3599826 100644 --- a/progress_api/backends/enlighten.py +++ b/progress_api/backends/enlighten.py @@ -51,6 +51,7 @@ class EnlightenProgressBackend(ProgressBackend): Progress bar backend that doesn't emit any progress. """ + spec: ProgressBarSpec manager: Manager state_colors: dict[str, str] diff --git a/progress_api/backends/mock.py b/progress_api/backends/mock.py index d586802..a8f5377 100644 --- a/progress_api/backends/mock.py +++ b/progress_api/backends/mock.py @@ -32,6 +32,7 @@ def create_bar(self, spec: ProgressBarSpec) -> api.Progress: class MockProgress(api.Progress): + spec: ProgressBarSpec backend: MockProgressBackend def __init__(self, backend: MockProgressBackend, spec: ProgressBarSpec): diff --git a/progress_api/backends/null.py b/progress_api/backends/null.py index 8b447e2..c542db2 100644 --- a/progress_api/backends/null.py +++ b/progress_api/backends/null.py @@ -21,13 +21,10 @@ class NullProgressBackend(ProgressBackend): """ def create_bar(self, spec: ProgressBarSpec) -> api.Progress: - return NullProgress(spec) + return NullProgress() class NullProgress(api.Progress): - def __init__(self, spec: ProgressBarSpec): - self.spec = spec - def set_label(self, label: Optional[str]): pass