From 9da5c41704fbdfcb9eb626441d328d62ce19a3fa Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sat, 31 Aug 2019 18:00:03 +0200 Subject: [PATCH] Improve --debug output formatting --- httpie/client.py | 6 +++--- httpie/context.py | 10 +++++----- httpie/utils.py | 17 +++-------------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/httpie/client.py b/httpie/client.py index bb5410de..49d04063 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -13,7 +13,7 @@ from httpie import __version__, sessions from httpie.cli.constants import SSL_VERSION_ARG_MAPPING from httpie.cli.dicts import RequestHeadersDict from httpie.plugins import plugin_manager -from httpie.utils import repr_dict_nice +from httpie.utils import repr_dict try: @@ -122,8 +122,8 @@ def get_response( def dump_request(kwargs: dict): - sys.stderr.write('\n>>> requests.request(**%s)\n\n' - % repr_dict_nice(kwargs)) + sys.stderr.write( + f'\n>>> requests.request(**{repr_dict(kwargs)})\n\n') def finalize_headers(headers: RequestHeadersDict) -> RequestHeadersDict: diff --git a/httpie/context.py b/httpie/context.py index 7dc18a15..4c19fc2f 100644 --- a/httpie/context.py +++ b/httpie/context.py @@ -11,7 +11,7 @@ except ImportError: from httpie.compat import is_windows from httpie.config import DEFAULT_CONFIG_DIR, Config -from httpie.utils import repr_dict_nice +from httpie.utils import repr_dict class Environment: @@ -94,11 +94,11 @@ class Environment: actual = dict(defaults) actual.update(self.__dict__) actual['config'] = self.config - return repr_dict_nice(dict( - (key, value) + return repr_dict({ + key: value for key, value in actual.items() - if not key.startswith('_')) - ) + if not key.startswith('_') + }) def __repr__(self): return f'<{type(self).__name__} {self}>' diff --git a/httpie/utils.py b/httpie/utils.py index 39658fcc..c5b60216 100644 --- a/httpie/utils.py +++ b/httpie/utils.py @@ -2,6 +2,7 @@ from __future__ import division import json import mimetypes from collections import OrderedDict +from pprint import pformat import requests.auth @@ -10,20 +11,8 @@ def load_json_preserve_order(s): return json.loads(s, object_pairs_hook=OrderedDict) -def repr_dict_nice(d): - def prepare_dict(d): - for k, v in d.items(): - if isinstance(v, dict): - v = dict(prepare_dict(v)) - elif isinstance(v, bytes): - v = v.decode('utf8') - elif not isinstance(v, (int, str)): - v = repr(v) - yield k, v - return json.dumps( - dict(prepare_dict(d)), - indent=4, sort_keys=True, - ) +def repr_dict(d: dict) -> str: + return pformat(d) def humanize_bytes(n, precision=2):