Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/click/exceptions.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
| author | bcclaywell |
|---|---|
| date | Mon, 12 Oct 2015 17:43:33 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d67268158946 |
|---|---|
| 1 from ._compat import PY2, filename_to_ui, get_text_stderr | |
| 2 from .utils import echo | |
| 3 | |
| 4 | |
| 5 class ClickException(Exception): | |
| 6 """An exception that Click can handle and show to the user.""" | |
| 7 | |
| 8 #: The exit code for this exception | |
| 9 exit_code = 1 | |
| 10 | |
| 11 def __init__(self, message): | |
| 12 if PY2: | |
| 13 if message is not None: | |
| 14 message = message.encode('utf-8') | |
| 15 Exception.__init__(self, message) | |
| 16 self.message = message | |
| 17 | |
| 18 def format_message(self): | |
| 19 return self.message | |
| 20 | |
| 21 def show(self, file=None): | |
| 22 if file is None: | |
| 23 file = get_text_stderr() | |
| 24 echo('Error: %s' % self.format_message(), file=file) | |
| 25 | |
| 26 | |
| 27 class UsageError(ClickException): | |
| 28 """An internal exception that signals a usage error. This typically | |
| 29 aborts any further handling. | |
| 30 | |
| 31 :param message: the error message to display. | |
| 32 :param ctx: optionally the context that caused this error. Click will | |
| 33 fill in the context automatically in some situations. | |
| 34 """ | |
| 35 exit_code = 2 | |
| 36 | |
| 37 def __init__(self, message, ctx=None): | |
| 38 ClickException.__init__(self, message) | |
| 39 self.ctx = ctx | |
| 40 | |
| 41 def show(self, file=None): | |
| 42 if file is None: | |
| 43 file = get_text_stderr() | |
| 44 color = None | |
| 45 if self.ctx is not None: | |
| 46 color = self.ctx.color | |
| 47 echo(self.ctx.get_usage() + '\n', file=file, color=color) | |
| 48 echo('Error: %s' % self.format_message(), file=file, color=color) | |
| 49 | |
| 50 | |
| 51 class BadParameter(UsageError): | |
| 52 """An exception that formats out a standardized error message for a | |
| 53 bad parameter. This is useful when thrown from a callback or type as | |
| 54 Click will attach contextual information to it (for instance, which | |
| 55 parameter it is). | |
| 56 | |
| 57 .. versionadded:: 2.0 | |
| 58 | |
| 59 :param param: the parameter object that caused this error. This can | |
| 60 be left out, and Click will attach this info itself | |
| 61 if possible. | |
| 62 :param param_hint: a string that shows up as parameter name. This | |
| 63 can be used as alternative to `param` in cases | |
| 64 where custom validation should happen. If it is | |
| 65 a string it's used as such, if it's a list then | |
| 66 each item is quoted and separated. | |
| 67 """ | |
| 68 | |
| 69 def __init__(self, message, ctx=None, param=None, | |
| 70 param_hint=None): | |
| 71 UsageError.__init__(self, message, ctx) | |
| 72 self.param = param | |
| 73 self.param_hint = param_hint | |
| 74 | |
| 75 def format_message(self): | |
| 76 if self.param_hint is not None: | |
| 77 param_hint = self.param_hint | |
| 78 elif self.param is not None: | |
| 79 param_hint = self.param.opts or [self.param.human_readable_name] | |
| 80 else: | |
| 81 return 'Invalid value: %s' % self.message | |
| 82 if isinstance(param_hint, (tuple, list)): | |
| 83 param_hint = ' / '.join('"%s"' % x for x in param_hint) | |
| 84 return 'Invalid value for %s: %s' % (param_hint, self.message) | |
| 85 | |
| 86 | |
| 87 class MissingParameter(BadParameter): | |
| 88 """Raised if click required an option or argument but it was not | |
| 89 provided when invoking the script. | |
| 90 | |
| 91 .. versionadded:: 4.0 | |
| 92 | |
| 93 :param param_type: a string that indicates the type of the parameter. | |
| 94 The default is to inherit the parameter type from | |
| 95 the given `param`. Valid values are ``'parameter'``, | |
| 96 ``'option'`` or ``'argument'``. | |
| 97 """ | |
| 98 | |
| 99 def __init__(self, message=None, ctx=None, param=None, | |
| 100 param_hint=None, param_type=None): | |
| 101 BadParameter.__init__(self, message, ctx, param, param_hint) | |
| 102 self.param_type = param_type | |
| 103 | |
| 104 def format_message(self): | |
| 105 if self.param_hint is not None: | |
| 106 param_hint = self.param_hint | |
| 107 elif self.param is not None: | |
| 108 param_hint = self.param.opts or [self.param.human_readable_name] | |
| 109 else: | |
| 110 param_hint = None | |
| 111 if isinstance(param_hint, (tuple, list)): | |
| 112 param_hint = ' / '.join('"%s"' % x for x in param_hint) | |
| 113 | |
| 114 param_type = self.param_type | |
| 115 if param_type is None and self.param is not None: | |
| 116 param_type = self.param.param_type_name | |
| 117 | |
| 118 msg = self.message | |
| 119 msg_extra = self.param.type.get_missing_message(self.param) | |
| 120 if msg_extra: | |
| 121 if msg: | |
| 122 msg += '. ' + msg_extra | |
| 123 else: | |
| 124 msg = msg_extra | |
| 125 | |
| 126 return 'Missing %s%s%s%s' % ( | |
| 127 param_type, | |
| 128 param_hint and ' %s' % param_hint or '', | |
| 129 msg and '. ' or '.', | |
| 130 msg or '', | |
| 131 ) | |
| 132 | |
| 133 | |
| 134 class NoSuchOption(UsageError): | |
| 135 """Raised if click attempted to handle an option that does not | |
| 136 exist. | |
| 137 | |
| 138 .. versionadded:: 4.0 | |
| 139 """ | |
| 140 | |
| 141 def __init__(self, option_name, message=None, possibilities=None, | |
| 142 ctx=None): | |
| 143 if message is None: | |
| 144 message = 'no such option: %s' % option_name | |
| 145 UsageError.__init__(self, message, ctx) | |
| 146 self.option_name = option_name | |
| 147 self.possibilities = possibilities | |
| 148 | |
| 149 def format_message(self): | |
| 150 bits = [self.message] | |
| 151 if self.possibilities: | |
| 152 if len(self.possibilities) == 1: | |
| 153 bits.append('Did you mean %s?' % self.possibilities[0]) | |
| 154 else: | |
| 155 possibilities = sorted(self.possibilities) | |
| 156 bits.append('(Possible options: %s)' % ', '.join(possibilities)) | |
| 157 return ' '.join(bits) | |
| 158 | |
| 159 | |
| 160 class BadOptionUsage(UsageError): | |
| 161 """Raised if an option is generally supplied but the use of the option | |
| 162 was incorrect. This is for instance raised if the number of arguments | |
| 163 for an option is not correct. | |
| 164 | |
| 165 .. versionadded:: 4.0 | |
| 166 """ | |
| 167 | |
| 168 def __init__(self, option_name, message, ctx=None): | |
| 169 UsageError.__init__(self, message, ctx) | |
| 170 | |
| 171 | |
| 172 class FileError(ClickException): | |
| 173 """Raised if a file cannot be opened.""" | |
| 174 | |
| 175 def __init__(self, filename, hint=None): | |
| 176 ui_filename = filename_to_ui(filename) | |
| 177 if hint is None: | |
| 178 hint = 'unknown error' | |
| 179 ClickException.__init__(self, hint) | |
| 180 self.ui_filename = ui_filename | |
| 181 self.filename = filename | |
| 182 | |
| 183 def format_message(self): | |
| 184 return 'Could not open file %s: %s' % (self.ui_filename, self.message) | |
| 185 | |
| 186 | |
| 187 class Abort(RuntimeError): | |
| 188 """An internal signalling exception that signals Click to abort.""" |
