Revision 0238c167
b/kamaki/cli/__init__.py | ||
---|---|---|
42 | 42 |
from sys import exit, stdout, argv |
43 | 43 |
|
44 | 44 |
from kamaki.cli.errors import CLIError, CLICmdSpecError |
45 |
from kamaki.cli.utils import magenta, red, yellow, print_dict, remove_colors |
|
45 |
from kamaki.cli.utils import magenta, red, yellow, print_dict, print_list,\ |
|
46 |
remove_colors |
|
46 | 47 |
from kamaki.cli.command_tree import CommandTree |
47 | 48 |
from kamaki.cli.argument import _arguments, parse_known_args |
48 | 49 |
from kamaki.cli.history import History |
... | ... | |
145 | 146 |
|
146 | 147 |
|
147 | 148 |
def _print_error_message(cli_err): |
148 |
errmsg = '%s (%s)' % (cli_err, cli_err.status if cli_err.status else ' ')
|
|
149 |
errmsg = '%s' % cli_err
|
|
149 | 150 |
if cli_err.importance == 1: |
150 | 151 |
errmsg = magenta(errmsg) |
151 | 152 |
elif cli_err.importance == 2: |
... | ... | |
153 | 154 |
elif cli_err.importance > 2: |
154 | 155 |
errmsg = red(errmsg) |
155 | 156 |
stdout.write(errmsg) |
156 |
if cli_err.details is not None and len(cli_err.details) > 0: |
|
157 |
print(': %s' % cli_err.details) |
|
158 |
else: |
|
159 |
print() |
|
157 |
print_list(cli_err.details) |
|
160 | 158 |
|
161 | 159 |
|
162 | 160 |
def get_command_group(unparsed): |
b/kamaki/cli/commands/astakos_cli.py | ||
---|---|---|
56 | 56 |
class astakos_authenticate(_astakos_init): |
57 | 57 |
"""Authenticate a user""" |
58 | 58 |
|
59 |
def main(self): |
|
59 |
def main(self, custom_token=None):
|
|
60 | 60 |
super(astakos_authenticate, self).main() |
61 | 61 |
try: |
62 |
reply = self.client.authenticate() |
|
62 |
reply = self.client.authenticate(custom_token)
|
|
63 | 63 |
except ClientError as err: |
64 | 64 |
raiseCLIError(err) |
65 | 65 |
print_dict(reply) |
b/kamaki/cli/commands/pithos_cli.py | ||
---|---|---|
714 | 714 |
def main(self, container___path): |
715 | 715 |
super(self.__class__, |
716 | 716 |
self).main(container___path, path_is_optional=False) |
717 |
self.client.download_object(self.path, stdout, |
|
717 |
try: |
|
718 |
self.client.download_object(self.path, stdout, |
|
718 | 719 |
range=self.get_argument('range'), |
719 | 720 |
version=self.get_argument('object_version'), |
720 | 721 |
if_match=self.get_argument('if_match'), |
721 | 722 |
if_none_match=self.get_argument('if_none_match'), |
722 | 723 |
if_modified_since=self.get_argument('if_modified_since'), |
723 | 724 |
if_unmodified_since=self.get_argument('if_unmodified_since')) |
725 |
except ClientError as err: |
|
726 |
raiseCLIError(err) |
|
724 | 727 |
|
725 | 728 |
|
726 | 729 |
@command() |
b/kamaki/cli/errors.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
|
35 | 35 |
class CLIError(Exception): |
36 |
def __init__(self, message, status=0, details='', importance=0): |
|
37 |
"""importance is set by the raiser |
|
38 |
0 is the lowest possible importance |
|
39 |
Suggested values: 0, 1, 2, 3 |
|
36 |
def __init__(self, message, details=[], importance=0): |
|
40 | 37 |
""" |
41 |
super(CLIError, self).__init__(message, status, details) |
|
42 |
self.message = message |
|
43 |
self.status = status |
|
44 |
self.details = details |
|
45 |
self.importance = importance |
|
46 |
|
|
47 |
def __unicode__(self): |
|
48 |
return unicode(self.message) |
|
38 |
@message is the main message of the Error |
|
39 |
@detauls is a list of previous errors |
|
40 |
@importance of the output for the user |
|
41 |
Suggested values: 0, 1, 2, 3 |
|
42 |
""" |
|
43 |
super(CLIError, self).__init__(message) |
|
44 |
self.details = details if isinstance(details, list)\ |
|
45 |
else [] if details is None else ['%s' % details] |
|
46 |
try: |
|
47 |
self.importance = int(importance) |
|
48 |
except ValueError: |
|
49 |
self.importance = 0 |
|
49 | 50 |
|
50 | 51 |
|
51 | 52 |
class CLISyntaxError(CLIError): |
52 |
def __init__(self, message='Syntax Error', status=10, details=''): |
|
53 |
super(CLISyntaxError, |
|
54 |
self).__init__(message, status, details, importance=1) |
|
53 |
def __init__(self, message='Syntax Error', details=[], importance=1): |
|
54 |
super(CLISyntaxError, self).__init__(message, details, importance) |
|
55 | 55 |
|
56 | 56 |
|
57 | 57 |
class CLIUnknownCommand(CLIError): |
58 |
def __init__(self, message='Unknown Command', status=12, details=''): |
|
59 |
super(CLIUnknownCommand, |
|
60 |
self).__init__(message, status, details, importance=1) |
|
58 |
def __init__(self, message='Unknown Command', details=[], importance=1): |
|
59 |
super(CLIUnknownCommand, self).__init__(message, details, importance) |
|
61 | 60 |
|
62 | 61 |
|
63 | 62 |
class CLICmdSpecError(CLIError): |
64 | 63 |
def __init__(self, |
65 |
message='Command Specification Error', |
|
66 |
status=13, |
|
67 |
details='', |
|
68 |
importance=1): |
|
69 |
super(CLICmdSpecError, |
|
70 |
self).__init__(message, status, details, importance=0) |
|
64 |
message='Command Specification Error', details=[], importance=0): |
|
65 |
super(CLICmdSpecError, self).__init__(message, details, importance) |
|
71 | 66 |
|
72 | 67 |
|
73 | 68 |
class CLICmdIncompleteError(CLICmdSpecError): |
74 | 69 |
def __init__(self, |
75 |
message='Incomplete Command Error', |
|
76 |
status=14, |
|
77 |
details=''): |
|
78 |
super(CLICmdSpecError, |
|
79 |
self).__init__(message, status, details, importance=1) |
|
70 |
message='Incomplete Command Error', details=[], importance=1): |
|
71 |
super(CLICmdSpecError, self).__init__(message, details, importance) |
|
80 | 72 |
|
81 | 73 |
|
82 |
def raiseCLIError(err, importance=-1): |
|
83 |
if importance < 0: |
|
84 |
if err.status <= 0: |
|
85 |
importance = 0 |
|
86 |
elif err.status <= 400: |
|
87 |
importance = 1 |
|
88 |
elif err.status <= 500: |
|
89 |
importance = 2 |
|
90 |
else: |
|
91 |
importance = 3 |
|
92 |
raise CLIError('%s' % err, err.status, err.details, importance) |
|
74 |
def raiseCLIError(err, importance=0): |
|
75 |
message = '%s' % err |
|
76 |
if err.status: |
|
77 |
message = '(%s) %s' % (err.status, message) |
|
78 |
try: |
|
79 |
status = int(err.status) |
|
80 |
except ValueError: |
|
81 |
raise CLIError(message, err.details, importance) |
|
82 |
importance = status // 100 |
|
83 |
raise CLIError(message, err.details, importance) |
b/kamaki/clients/__init__.py | ||
---|---|---|
40 | 40 |
|
41 | 41 |
|
42 | 42 |
class ClientError(Exception): |
43 |
def __init__(self, message, status=0, details=''):
|
|
43 |
def __init__(self, message, status=0, details=[]):
|
|
44 | 44 |
super(ClientError, self).__init__(message) |
45 | 45 |
self.status = status |
46 | 46 |
self.details = details |
... | ... | |
58 | 58 |
|
59 | 59 |
def _raise_for_status(self, r): |
60 | 60 |
try: |
61 |
details = r.text
|
|
61 |
message = r.text
|
|
62 | 62 |
except: |
63 |
details = ''
|
|
64 |
raise ClientError('%s' % r, status=r.status, details=details)
|
|
63 |
message = '%s' % r
|
|
64 |
raise ClientError(message, status=r.status)
|
|
65 | 65 |
|
66 | 66 |
def set_header(self, name, value, iff=True): |
67 | 67 |
"""Set a header 'name':'value'""" |
... | ... | |
127 | 127 |
if r.content: |
128 | 128 |
recvlog.debug(r.content) |
129 | 129 |
|
130 |
if success is not None: |
|
131 |
# Success can either be an in or a collection |
|
132 |
success = (success,) if isinstance(success, int) else success |
|
133 |
if r.status_code not in success: |
|
134 |
r.release() |
|
135 |
self._raise_for_status(r) |
|
136 |
except ClientError: |
|
137 |
raise |
|
138 | 130 |
except Exception as err: |
131 |
print('WTF? !!!!!!!') |
|
139 | 132 |
from traceback import print_stack |
140 | 133 |
recvlog.debug(print_stack) |
141 | 134 |
self.http_client.reset_headers() |
... | ... | |
144 | 137 |
|
145 | 138 |
self.http_client.reset_headers() |
146 | 139 |
self.http_client.reset_params() |
140 |
|
|
141 |
if success is not None: |
|
142 |
# Success can either be an in or a collection |
|
143 |
success = (success,) if isinstance(success, int) else success |
|
144 |
if r.status_code not in success: |
|
145 |
r.release() |
|
146 |
self._raise_for_status(r) |
|
147 | 147 |
return r |
148 | 148 |
|
149 | 149 |
def delete(self, path, **kwargs): |
b/kamaki/clients/astakos.py | ||
---|---|---|
48 | 48 |
# Fallback to the default |
49 | 49 |
super(AstakosClient, self).raise_for_status(r) |
50 | 50 |
|
51 |
def authenticate(self): |
|
51 |
def authenticate(self, token=None): |
|
52 |
if token: |
|
53 |
self.token = token |
|
52 | 54 |
r = self.get('/im/authenticate') |
53 | 55 |
return r.json |
b/kamaki/clients/connection/__init__.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
from kamaki.clients.connection.errors import HTTPConnectionError |
|
35 |
|
|
36 | 34 |
|
37 | 35 |
class HTTPResponse(object): |
38 | 36 |
|
b/kamaki/clients/connection/errors.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
|
35 | 35 |
class HTTPConnectionError(Exception): |
36 |
""" |
|
37 |
700: Generic connection error |
|
38 |
701: Cannot connect to server |
|
39 |
702: Response format error |
|
40 |
""" |
|
41 |
def __init__(self, message, status=700): |
|
36 |
def __init__(self, message): |
|
42 | 37 |
super(HTTPConnectionError, self).__init__(message) |
43 |
self.status = status |
|
44 | 38 |
|
45 | 39 |
|
46 |
class HTTPResponseFormatError(HTTPConnectionError): |
|
47 |
|
|
48 |
def __init__(self, message, details=''): |
|
49 |
super(HTTPResponseFormatError, self).__init__(message, status=702) |
|
40 |
class HTTPResponseError(Exception): |
|
41 |
def __init__(self, message): |
|
42 |
super(HTTPResponseError, self).__init__(message) |
b/kamaki/clients/connection/kamakicon.py | ||
---|---|---|
36 | 36 |
from synnefo.lib.pool.http import get_http_connection |
37 | 37 |
from kamaki.clients.connection import HTTPConnection, HTTPResponse |
38 | 38 |
from kamaki.clients.connection.errors import HTTPConnectionError |
39 |
from kamaki.clients.connection.errors import HTTPResponseFormatError
|
|
39 |
from kamaki.clients.connection.errors import HTTPResponseError |
|
40 | 40 |
from socket import gaierror |
41 | 41 |
|
42 | 42 |
from json import loads |
... | ... | |
84 | 84 |
try: |
85 | 85 |
return loads(self._content) |
86 | 86 |
except ValueError as err: |
87 |
HTTPResponseFormatError('Response not formated in JSON - %s' % err)
|
|
87 |
HTTPResponseError('Response not formated in JSON - %s' % err) |
|
88 | 88 |
|
89 | 89 |
@json.setter |
90 | 90 |
def json(self, v): |
... | ... | |
148 | 148 |
conn.close() |
149 | 149 |
if isinstance(err, gaierror): |
150 | 150 |
raise HTTPConnectionError( |
151 |
'Cannot connect to %s - %s' % (self.url, err), |
|
152 |
status=701) |
|
151 |
'Cannot connect to %s - %s' % (self.url, err)) |
|
153 | 152 |
raise |
154 | 153 |
return KamakiHTTPResponse(conn) |
Also available in: Unified diff