Coverage for python/lsst/ctrl/mpexec/cli/script/confirmable.py: 34%
41 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-11 09:04 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-11 09:04 +0000
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 collections.abc import Callable
26import click
29class ConfirmableResult(ABC):
30 """Interface for a results class that can be used by the `confirm`
31 function.
32 """
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 """Perform the action that was returned from `describe`.
52 This is Called just after the user has confirmed (if needed).
53 """
54 pass
56 @property
57 @abstractmethod
58 def failed(self) -> bool:
59 """Query if there was a failure preparing the ConfirmableResult,
60 before `on_confirmation` is called.
61 """
62 pass
64 @property
65 @abstractmethod
66 def describe_failure(self) -> str:
67 """Get a message describing the failure.
69 This is used as the message when raising a `~click.ClickException` to
70 stop with exit code 1.
71 """
72 pass
74 @property
75 @abstractmethod
76 def can_continue(self) -> bool:
77 """Query if the ConfirmableResult can continue. Returns `False` if
78 there is no work to be done.
79 """
80 pass
83def confirm(script_func: Callable[[], ConfirmableResult], confirm: bool) -> ConfirmableResult:
84 """Prompt user to continue."""
85 result = script_func()
86 if result.failed:
87 raise click.ClickException(result.describe_failure)
88 if not result.can_continue:
89 print(result.describe(will=True))
90 return result
91 if confirm:
92 print(result.describe(will=True))
93 if result.can_continue:
94 do_continue = click.confirm("Continue?", default=False)
95 else:
96 do_continue = True
97 if not do_continue:
98 print("Aborted.")
99 else:
100 result.on_confirmation()
101 if not confirm:
102 print(result.describe(will=False))
103 else:
104 print("Done.")
105 return result