Coverage for python/lsst/daf/butler/cli/progress.py: 68%

20 statements  

« prev     ^ index     » next       coverage.py v7.2.5, created at 2023-05-18 09:13 +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/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ("ClickProgressHandler",) 

25 

26from collections.abc import Iterable 

27from typing import Any, ContextManager, TypeVar 

28 

29import click 

30 

31from ..core.progress import Progress, ProgressBar, ProgressHandler 

32 

33_T = TypeVar("_T") 

34 

35 

36class ClickProgressHandler(ProgressHandler): 

37 """A `ProgressHandler` implementation that delegates to 

38 `click.progressbar`. 

39 

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

47 

48 def __init__(self, **kwargs: Any): 

49 self._kwargs = kwargs 

50 

51 @classmethod 

52 def callback(cls, ctx: click.Context, params: click.Parameter, value: Any) -> None: 

53 """A `click` callback that installs this handler as the global handler 

54 for progress bars. 

55 

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) 

62 

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) 

75 

76 def get_progress_bar( 

77 self, iterable: Iterable[_T] | None, desc: str | None, total: int | None, level: int 

78 ) -> ContextManager[ProgressBar[_T]]: 

79 # Docstring inherited. 

80 return click.progressbar(iterable=iterable, length=total, label=desc, **self._kwargs) # type: ignore