Coverage for python/lsst/daf/butler/cli/progress.py: 67%
19 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-24 23:50 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-24 23:50 -0700
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/>.
23from __future__ import annotations
25__all__ = ("ClickProgressHandler",)
27from typing import Any, ContextManager, Iterable, Optional, TypeVar
29import click
31from ..core.progress import Progress, ProgressBar, ProgressHandler
33_T = TypeVar("_T")
36class ClickProgressHandler(ProgressHandler):
37 """A `ProgressHandler` implementation that delegates to
38 `click.progressbar`.
40 Parameters
41 ----------
42 **kwargs
43 Additional keyword arguments to pass to `click.progressbar`. May not
44 include ``iterable``, ``length``, or ``label``, as these are passed
45 directly from `get_progress_bar` arguments.
46 """
48 def __init__(self, **kwargs: Any):
49 self._kwargs = kwargs
51 @classmethod
52 def callback(cls, ctx, params, value):
53 """A `click` callback that installs this handler as the global handler
54 for progress bars.
56 Should usually be called only by the `option` method.
57 """
58 if value:
59 Progress.set_handler(cls())
60 else:
61 Progress.set_handler(None)
63 @classmethod
64 def option(cls, cmd: Any) -> Any:
65 """A `click` command decorator that adds a ``--progress`` option
66 that installs a default-constructed instance of this progress handler.
67 """
68 return click.option(
69 "--progress/--no-progress",
70 help="Show a progress bar for slow operations when possible.",
71 default=False,
72 is_flag=True,
73 callback=cls.callback,
74 )(cmd)
76 def get_progress_bar(
77 self, iterable: Optional[Iterable[_T]], desc: Optional[str], total: Optional[int], level: int
78 ) -> ContextManager[ProgressBar[_T]]:
79 # Docstring inherited.
80 return click.progressbar(iterable=iterable, length=total, label=desc, **self._kwargs)