From 869192654c4056b9a1f824bc5798f55ca9c60f8a Mon Sep 17 00:00:00 2001 From: Joe Heck Date: Thu, 16 Aug 2012 18:18:22 -0700 Subject: [PATCH] splitting http req and resp logging also some pep8 cleanup in shell.py Change-Id: I71aa2586a0196c0a6ba64b892b56c9d221bdcc1d --- keystoneclient/client.py | 24 +++++++++++++++--------- keystoneclient/shell.py | 5 +++-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/keystoneclient/client.py b/keystoneclient/client.py index 476655186..11e64085b 100644 --- a/keystoneclient/client.py +++ b/keystoneclient/client.py @@ -61,6 +61,13 @@ class HTTPClient(httplib2.Http): self.force_exception_to_status_code = True self.disable_ssl_certificate_validation = insecure + # logging setup + self.debug_log = os.environ.get('KEYSTONECLIENT_DEBUG', False) + if self.debug_log: + ch = logging.StreamHandler() + _logger.setLevel(logging.DEBUG) + _logger.addHandler(ch) + def authenticate(self): """ Authenticate against the keystone API. @@ -77,12 +84,8 @@ class HTTPClient(httplib2.Http): """ raise NotImplementedError - def http_log(self, args, kwargs, resp, body): - if os.environ.get('KEYSTONECLIENT_DEBUG', False): - ch = logging.StreamHandler() - _logger.setLevel(logging.DEBUG) - _logger.addHandler(ch) - elif not _logger.isEnabledFor(logging.DEBUG): + def http_log_req(self, args, kwargs): + if not self.debug_log: return string_parts = ['curl -i'] @@ -99,7 +102,10 @@ class HTTPClient(httplib2.Http): _logger.debug("REQ: %s\n" % "".join(string_parts)) if 'body' in kwargs: _logger.debug("REQ BODY: %s\n" % (kwargs['body'])) - _logger.debug("RESP: %s\nRESP BODY: %s\n", resp, body) + + def http_log_resp(self, resp, body): + if self.debug_log: + _logger.debug("RESP: %s\nRESP BODY: %s\n", resp, body) def request(self, url, method, **kwargs): """ Send an http request with the specified characteristics. @@ -115,11 +121,11 @@ class HTTPClient(httplib2.Http): request_kwargs['headers']['Content-Type'] = 'application/json' request_kwargs['body'] = json.dumps(kwargs['body']) + self.http_log_req((url, method,), request_kwargs) resp, body = super(HTTPClient, self).request(url, method, **request_kwargs) - - self.http_log((url, method,), request_kwargs, resp, body) + self.http_log_resp(resp, body) if body: try: diff --git a/keystoneclient/shell.py b/keystoneclient/shell.py index 899086322..821949f64 100644 --- a/keystoneclient/shell.py +++ b/keystoneclient/shell.py @@ -195,7 +195,8 @@ class OpenStackIdentityShell(object): return parser def _add_bash_completion_subparser(self, subparsers): - subparser = subparsers.add_parser('bash_completion', + subparser = subparsers.add_parser( + 'bash_completion', add_help=False, formatter_class=OpenStackHelpFormatter ) @@ -372,7 +373,7 @@ class OpenStackIdentityShell(object): print ' '.join(commands | options) @utils.arg('command', metavar='', nargs='?', - help='Display help for ') + help='Display help for ') def do_help(self, args): """ Display help about this program or one of its subcommands.