Coverage for python/lsst/daf/butler/cli/progress.py: 74%
21 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-25 15:14 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-25 15:14 +0000
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from __future__ import annotations
24__all__ = ("ClickProgressHandler",)
26from collections.abc import Iterable
27from contextlib import AbstractContextManager
28from typing import Any, TypeVar
30import click
32from ..core.progress import Progress, ProgressBar, ProgressHandler
34_T = TypeVar("_T")
37class ClickProgressHandler(ProgressHandler):
38 """A `ProgressHandler` implementation that delegates to
39 `click.progressbar`.
41 Parameters
42 ----------
43 **kwargs
44 Additional keyword arguments to pass to `click.progressbar`. May not
45 include ``iterable``, ``length``, or ``label``, as these are passed
46 directly from `get_progress_bar` arguments.
47 """
49 def __init__(self, **kwargs: Any):
50 self._kwargs = kwargs
52 @classmethod
53 def callback(cls, ctx: click.Context, params: click.Parameter, value: Any) -> None:
54 """`click` callback that installs this handler as the global handler
55 for progress bars.
57 Should usually be called only by the `option` method.
58 """
59 if value:
60 Progress.set_handler(cls())
61 else:
62 Progress.set_handler(None)
64 @classmethod
65 def option(cls, cmd: Any) -> Any:
66 """`click` command decorator that adds a ``--progress`` option
67 that installs a default-constructed instance of this progress handler.
68 """
69 return click.option(
70 "--progress/--no-progress",
71 help="Show a progress bar for slow operations when possible.",
72 default=False,
73 is_flag=True,
74 callback=cls.callback,
75 )(cmd)
77 def get_progress_bar(
78 self, iterable: Iterable[_T] | None, desc: str | None, total: int | None, level: int
79 ) -> AbstractContextManager[ProgressBar[_T]]:
80 # Docstring inherited.
81 return click.progressbar(iterable=iterable, length=total, label=desc, **self._kwargs) # type: ignore