Coverage for python/lsst/ctrl/mpexec/cli/script/confirmable.py: 53%
41 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 09:59 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-26 09:59 +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.
92 Parameters
93 ----------
94 script_func : `~collections.abc.Callable`
95 Script function to call.
96 confirm : `bool`
97 Whether confirmation is required.
99 Returns
100 -------
101 result : `ConfirmableResult`
102 Confirmation from script function.
103 """
104 result = script_func()
105 if result.failed:
106 raise click.ClickException(result.describe_failure)
107 if not result.can_continue:
108 print(result.describe(will=True))
109 return result
110 if confirm:
111 print(result.describe(will=True))
112 if result.can_continue:
113 do_continue = click.confirm("Continue?", default=False)
114 else:
115 do_continue = True
116 if not do_continue:
117 print("Aborted.")
118 else:
119 result.on_confirmation()
120 if not confirm:
121 print(result.describe(will=False))
122 else:
123 print("Done.")
124 return result