Coverage for python/lsst/ctrl/mpexec/cli/script/confirmable.py: 53%
41 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-13 09:53 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-13 09:53 +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 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/>.
29from abc import ABC, abstractmethod
30from collections.abc import Callable
32import click
35class ConfirmableResult(ABC):
36 """Interface for a results class that can be used by the `confirm`
37 function.
38 """
40 @abstractmethod
41 def describe(self, will: bool) -> str:
42 """Get a message describing what will be or was done. This is printed
43 just before "Continue?" on the CLI, if confirming, or just before
44 "Done." if confirmation is being skipped.
46 Parameters
47 ----------
48 will : bool
49 True if confirmation is being requested, False if --no-confirm was
50 used, and the action is completed.
51 """
52 pass
54 @abstractmethod
55 def on_confirmation(self) -> None:
56 """Perform the action that was returned from `describe`.
58 This is Called just after the user has confirmed (if needed).
59 """
60 pass
62 @property
63 @abstractmethod
64 def failed(self) -> bool:
65 """Query if there was a failure preparing the ConfirmableResult,
66 before `on_confirmation` is called.
67 """
68 pass
70 @property
71 @abstractmethod
72 def describe_failure(self) -> str:
73 """Get a message describing the failure.
75 This is used as the message when raising a `~click.ClickException` to
76 stop with exit code 1.
77 """
78 pass
80 @property
81 @abstractmethod
82 def can_continue(self) -> bool:
83 """Query if the ConfirmableResult can continue. Returns `False` if
84 there is no work to be done.
85 """
86 pass
89def confirm(script_func: Callable[[], ConfirmableResult], confirm: bool) -> ConfirmableResult:
90 """Prompt user to continue."""
91 result = script_func()
92 if result.failed:
93 raise click.ClickException(result.describe_failure)
94 if not result.can_continue:
95 print(result.describe(will=True))
96 return result
97 if confirm:
98 print(result.describe(will=True))
99 if result.can_continue:
100 do_continue = click.confirm("Continue?", default=False)
101 else:
102 do_continue = True
103 if not do_continue:
104 print("Aborted.")
105 else:
106 result.on_confirmation()
107 if not confirm:
108 print(result.describe(will=False))
109 else:
110 print("Done.")
111 return result