Skip to content

Commit

Permalink
Improved --debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbrzt committed Mar 4, 2016
1 parent 4e574e6 commit bb49a1f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This project adheres to `Semantic Versioning <http://semver.org/>`_.
is set
* Changed the default ``--style`` back to ``solarized`` for better support
of light and dark terminals
* Improved ``--debug`` output
* Fixed ``--session`` when used with ``--download``
* Fixed handling of ``Content-Type`` with multiple ``+subtype`` parts

Expand Down
4 changes: 2 additions & 2 deletions httpie/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from httpie.compat import str
from httpie.input import SSL_VERSION_ARG_MAPPING
from httpie.plugins import plugin_manager

from httpie.utils import repr_dict_nice

try:
# https://urllib3.readthedocs.org/en/latest/security.html
Expand Down Expand Up @@ -82,7 +82,7 @@ def get_response(args, config_dir):

def dump_request(kwargs):
sys.stderr.write('\n>>> requests.request(**%s)\n\n'
% pformat(kwargs))
% repr_dict_nice(kwargs))


def encode_headers(headers):
Expand Down
16 changes: 16 additions & 0 deletions httpie/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from httpie.compat import is_windows
from httpie.config import DEFAULT_CONFIG_DIR, Config

from httpie.utils import repr_dict_nice


class Environment(object):
"""
Expand Down Expand Up @@ -82,3 +84,17 @@ def config(self):
else:
self._config.load()
return self._config

def __str__(self):
defaults = dict(type(self).__dict__)
actual = dict(defaults)
actual.update(self.__dict__)
actual['config'] = self.config
return repr_dict_nice(dict(
(key, value)
for key, value in actual.items()
if not key.startswith('_'))
)

def __repr__(self):
return '<{0} {1}>'.format(type(self).__name__, str(self))
8 changes: 6 additions & 2 deletions httpie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""
import sys
import errno
import platform

import requests
from requests import __version__ as requests_version
Expand Down Expand Up @@ -48,11 +49,14 @@ def get_exit_status(http_status, follow=False):
def print_debug_info(env):
env.stderr.writelines([
'HTTPie %s\n' % httpie_version,
'HTTPie data: %s\n' % env.config.directory,
'Requests %s\n' % requests_version,
'Pygments %s\n' % pygments_version,
'Python %s %s\n' % (sys.version, sys.platform)
'Python %s\n%s\n' % (sys.version, sys.executable),
'%s %s' % (platform.system(), platform.release()),
])
env.stderr.write('\n\n')
env.stderr.write(repr(env))
env.stderr.write('\n')


def decode_args(args, stdin_encoding):
Expand Down
16 changes: 16 additions & 0 deletions httpie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ 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 humanize_bytes(n, precision=2):
# Author: Doug Latornell
# Licence: MIT
Expand Down
2 changes: 1 addition & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_error_traceback(get_response):
exc.request = Request(method='GET', url='http://www.google.com')
get_response.side_effect = exc
with raises(ConnectionError):
ret = main(['--ignore-stdin', '--traceback', 'www.google.com'])
main(['--ignore-stdin', '--traceback', 'www.google.com'])


@mock.patch('httpie.core.get_response')
Expand Down
1 change: 0 additions & 1 deletion tests/test_httpie.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def test_debug():
r = http('--debug')
assert r.exit_status == httpie.ExitStatus.OK
assert 'HTTPie %s' % httpie.__version__ in r.stderr
assert 'HTTPie data:' in r.stderr


def test_help():
Expand Down

0 comments on commit bb49a1f

Please sign in to comment.