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

21 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-13 09:58 +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 software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28from __future__ import annotations 

29 

30__all__ = ("ClickProgressHandler",) 

31 

32from collections.abc import Iterable 

33from contextlib import AbstractContextManager 

34from typing import Any, TypeVar 

35 

36import click 

37 

38from ..progress import Progress, ProgressBar, ProgressHandler 

39 

40_T = TypeVar("_T") 

41 

42 

43class ClickProgressHandler(ProgressHandler): 

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

45 `click.progressbar`. 

46 

47 Parameters 

48 ---------- 

49 **kwargs 

50 Additional keyword arguments to pass to `click.progressbar`. May not 

51 include ``iterable``, ``length``, or ``label``, as these are passed 

52 directly from `get_progress_bar` arguments. 

53 """ 

54 

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

56 self._kwargs = kwargs 

57 

58 @classmethod 

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

60 """`click` callback that installs this handler as the global handler 

61 for progress bars. 

62 

63 Should usually be called only by the `option` method. 

64 

65 Parameters 

66 ---------- 

67 ctx : `click.Context` 

68 Context provided by Click. 

69 params : `click.Parameter` 

70 Parameters provided by Click. 

71 value : `typing.Any` 

72 Value to control whether a handler class is registered. 

73 """ 

74 if value: 

75 Progress.set_handler(cls()) 

76 else: 

77 Progress.set_handler(None) 

78 

79 @classmethod 

80 def option(cls, cmd: Any) -> Any: 

81 """`click` command decorator that adds a ``--progress`` option 

82 that installs a default-constructed instance of this progress handler. 

83 

84 Parameters 

85 ---------- 

86 cmd : `typing.Any` 

87 Command to be modified. 

88 """ 

89 return click.option( 

90 "--progress/--no-progress", 

91 help="Show a progress bar for slow operations when possible.", 

92 default=False, 

93 is_flag=True, 

94 callback=cls.callback, 

95 )(cmd) 

96 

97 def get_progress_bar( 

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

99 ) -> AbstractContextManager[ProgressBar[_T]]: 

100 # Docstring inherited. 

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