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

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

19 statements  

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 

22 

23from __future__ import annotations 

24 

25__all__ = ("ClickProgressHandler",) 

26 

27import click 

28from typing import Any, ContextManager, Iterable, Optional, TypeVar 

29 

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

31 

32_T = TypeVar("_T") 

33 

34 

35class ClickProgressHandler(ProgressHandler): 

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

37 `click.progressbar`. 

38 

39 Parameters 

40 ---------- 

41 **kwargs 

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

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

44 directly from `get_progress_bar` arguments. 

45 """ 

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

47 self._kwargs = kwargs 

48 

49 @classmethod 

50 def callback(cls, ctx, params, value): 

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

52 for progress bars. 

53 

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

55 """ 

56 if value: 

57 Progress.set_handler(cls()) 

58 else: 

59 Progress.set_handler(None) 

60 

61 @classmethod 

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

63 """A `click` command decorator that adds a ``--progress`` option 

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

65 """ 

66 return click.option("--progress/--no-progress", 

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

68 default=False, 

69 is_flag=True, 

70 callback=cls.callback)(cmd) 

71 

72 def get_progress_bar(self, iterable: Optional[Iterable[_T]], desc: Optional[str], 

73 total: Optional[int], level: int) -> ContextManager[ProgressBar[_T]]: 

74 # Docstring inherited. 

75 return click.progressbar(iterable, length=total, label=desc, **self._kwargs)