comparison env/lib/python3.7/site-packages/click/exceptions.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 from ._compat import filename_to_ui
2 from ._compat import get_text_stderr
3 from ._compat import PY2
4 from .utils import echo
5
6
7 def _join_param_hints(param_hint):
8 if isinstance(param_hint, (tuple, list)):
9 return " / ".join(repr(x) for x in param_hint)
10 return param_hint
11
12
13 class ClickException(Exception):
14 """An exception that Click can handle and show to the user."""
15
16 #: The exit code for this exception
17 exit_code = 1
18
19 def __init__(self, message):
20 ctor_msg = message
21 if PY2:
22 if ctor_msg is not None:
23 ctor_msg = ctor_msg.encode("utf-8")
24 Exception.__init__(self, ctor_msg)
25 self.message = message
26
27 def format_message(self):
28 return self.message
29
30 def __str__(self):
31 return self.message
32
33 if PY2:
34 __unicode__ = __str__
35
36 def __str__(self):
37 return self.message.encode("utf-8")
38
39 def show(self, file=None):
40 if file is None:
41 file = get_text_stderr()
42 echo("Error: {}".format(self.format_message()), file=file)
43
44
45 class UsageError(ClickException):
46 """An internal exception that signals a usage error. This typically
47 aborts any further handling.
48
49 :param message: the error message to display.
50 :param ctx: optionally the context that caused this error. Click will
51 fill in the context automatically in some situations.
52 """
53
54 exit_code = 2
55
56 def __init__(self, message, ctx=None):
57 ClickException.__init__(self, message)
58 self.ctx = ctx
59 self.cmd = self.ctx.command if self.ctx else None
60
61 def show(self, file=None):
62 if file is None:
63 file = get_text_stderr()
64 color = None
65 hint = ""
66 if self.cmd is not None and self.cmd.get_help_option(self.ctx) is not None:
67 hint = "Try '{} {}' for help.\n".format(
68 self.ctx.command_path, self.ctx.help_option_names[0]
69 )
70 if self.ctx is not None:
71 color = self.ctx.color
72 echo("{}\n{}".format(self.ctx.get_usage(), hint), file=file, color=color)
73 echo("Error: {}".format(self.format_message()), file=file, color=color)
74
75
76 class BadParameter(UsageError):
77 """An exception that formats out a standardized error message for a
78 bad parameter. This is useful when thrown from a callback or type as
79 Click will attach contextual information to it (for instance, which
80 parameter it is).
81
82 .. versionadded:: 2.0
83
84 :param param: the parameter object that caused this error. This can
85 be left out, and Click will attach this info itself
86 if possible.
87 :param param_hint: a string that shows up as parameter name. This
88 can be used as alternative to `param` in cases
89 where custom validation should happen. If it is
90 a string it's used as such, if it's a list then
91 each item is quoted and separated.
92 """
93
94 def __init__(self, message, ctx=None, param=None, param_hint=None):
95 UsageError.__init__(self, message, ctx)
96 self.param = param
97 self.param_hint = param_hint
98
99 def format_message(self):
100 if self.param_hint is not None:
101 param_hint = self.param_hint
102 elif self.param is not None:
103 param_hint = self.param.get_error_hint(self.ctx)
104 else:
105 return "Invalid value: {}".format(self.message)
106 param_hint = _join_param_hints(param_hint)
107
108 return "Invalid value for {}: {}".format(param_hint, self.message)
109
110
111 class MissingParameter(BadParameter):
112 """Raised if click required an option or argument but it was not
113 provided when invoking the script.
114
115 .. versionadded:: 4.0
116
117 :param param_type: a string that indicates the type of the parameter.
118 The default is to inherit the parameter type from
119 the given `param`. Valid values are ``'parameter'``,
120 ``'option'`` or ``'argument'``.
121 """
122
123 def __init__(
124 self, message=None, ctx=None, param=None, param_hint=None, param_type=None
125 ):
126 BadParameter.__init__(self, message, ctx, param, param_hint)
127 self.param_type = param_type
128
129 def format_message(self):
130 if self.param_hint is not None:
131 param_hint = self.param_hint
132 elif self.param is not None:
133 param_hint = self.param.get_error_hint(self.ctx)
134 else:
135 param_hint = None
136 param_hint = _join_param_hints(param_hint)
137
138 param_type = self.param_type
139 if param_type is None and self.param is not None:
140 param_type = self.param.param_type_name
141
142 msg = self.message
143 if self.param is not None:
144 msg_extra = self.param.type.get_missing_message(self.param)
145 if msg_extra:
146 if msg:
147 msg += ". {}".format(msg_extra)
148 else:
149 msg = msg_extra
150
151 return "Missing {}{}{}{}".format(
152 param_type,
153 " {}".format(param_hint) if param_hint else "",
154 ". " if msg else ".",
155 msg or "",
156 )
157
158 def __str__(self):
159 if self.message is None:
160 param_name = self.param.name if self.param else None
161 return "missing parameter: {}".format(param_name)
162 else:
163 return self.message
164
165 if PY2:
166 __unicode__ = __str__
167
168 def __str__(self):
169 return self.__unicode__().encode("utf-8")
170
171
172 class NoSuchOption(UsageError):
173 """Raised if click attempted to handle an option that does not
174 exist.
175
176 .. versionadded:: 4.0
177 """
178
179 def __init__(self, option_name, message=None, possibilities=None, ctx=None):
180 if message is None:
181 message = "no such option: {}".format(option_name)
182 UsageError.__init__(self, message, ctx)
183 self.option_name = option_name
184 self.possibilities = possibilities
185
186 def format_message(self):
187 bits = [self.message]
188 if self.possibilities:
189 if len(self.possibilities) == 1:
190 bits.append("Did you mean {}?".format(self.possibilities[0]))
191 else:
192 possibilities = sorted(self.possibilities)
193 bits.append("(Possible options: {})".format(", ".join(possibilities)))
194 return " ".join(bits)
195
196
197 class BadOptionUsage(UsageError):
198 """Raised if an option is generally supplied but the use of the option
199 was incorrect. This is for instance raised if the number of arguments
200 for an option is not correct.
201
202 .. versionadded:: 4.0
203
204 :param option_name: the name of the option being used incorrectly.
205 """
206
207 def __init__(self, option_name, message, ctx=None):
208 UsageError.__init__(self, message, ctx)
209 self.option_name = option_name
210
211
212 class BadArgumentUsage(UsageError):
213 """Raised if an argument is generally supplied but the use of the argument
214 was incorrect. This is for instance raised if the number of values
215 for an argument is not correct.
216
217 .. versionadded:: 6.0
218 """
219
220 def __init__(self, message, ctx=None):
221 UsageError.__init__(self, message, ctx)
222
223
224 class FileError(ClickException):
225 """Raised if a file cannot be opened."""
226
227 def __init__(self, filename, hint=None):
228 ui_filename = filename_to_ui(filename)
229 if hint is None:
230 hint = "unknown error"
231 ClickException.__init__(self, hint)
232 self.ui_filename = ui_filename
233 self.filename = filename
234
235 def format_message(self):
236 return "Could not open file {}: {}".format(self.ui_filename, self.message)
237
238
239 class Abort(RuntimeError):
240 """An internal signalling exception that signals Click to abort."""
241
242
243 class Exit(RuntimeError):
244 """An exception that indicates that the application should exit with some
245 status code.
246
247 :param code: the status code to exit with.
248 """
249
250 __slots__ = ("exit_code",)
251
252 def __init__(self, code=0):
253 self.exit_code = code