Coverage for python/lsst/ctrl/mpexec/cli/script/confirmable.py: 34%
41 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-07 02:42 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-07 02:42 -0800
1# This file is part of ctrl_mpexec.
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 abc import ABC, abstractmethod
24from typing import Callable
26import click
29class ConfirmableResult(ABC):
31 """Interface for a results class that can be used by the `confirm`
32 function."""
34 @abstractmethod
35 def describe(self, will: bool) -> str:
36 """Get a message describing what will be or was done. This is printed
37 just before "Continue?" on the CLI, if confirming, or just before
38 "Done." if confirmation is being skipped.
40 Parameters
41 ----------
42 will : bool
43 True if confirmation is being requested, False if --no-confirm was
44 used, and the action is completed.
45 """
46 pass
48 @abstractmethod
49 def on_confirmation(self) -> None:
50 """Performs the action that was returned from `describe`. This is
51 Called just after the user has confirmed (if needed)."""
52 pass
54 @property
55 @abstractmethod
56 def failed(self) -> bool:
57 """Query if there was a failure preparing the ConfirmableResult,
58 before `on_confirmation` is called."""
59 pass
61 @property
62 @abstractmethod
63 def describe_failure(self) -> str:
64 """Get a message describing the failure. This is used as the message
65 when raising a `ClickException` to stop with exit code 1."""
66 pass
68 @property
69 @abstractmethod
70 def can_continue(self) -> bool:
71 """Query if the ConfirmableResult can continue. Returns `False` if
72 there is no work to be done."""
73 pass
76def confirm(script_func: Callable[[], ConfirmableResult], confirm: bool) -> ConfirmableResult:
77 result = script_func()
78 if result.failed:
79 raise click.ClickException(result.describe_failure)
80 if not result.can_continue:
81 print(result.describe(will=True))
82 return result
83 if confirm:
84 print(result.describe(will=True))
85 if result.can_continue:
86 do_continue = click.confirm("Continue?", default=False)
87 else:
88 do_continue = True
89 if not do_continue:
90 print("Aborted.")
91 else:
92 result.on_confirmation()
93 if not confirm:
94 print(result.describe(will=False))
95 else:
96 print("Done.")
97 return result