1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-28 08:38:44 +02:00
httpie-cli/httpie/utils.py

81 lines
1.9 KiB
Python
Raw Normal View History

2013-03-24 17:23:18 +03:00
from __future__ import division
import json
from collections import OrderedDict
import requests.auth
def load_json_preserve_order(s):
return json.loads(s, object_pairs_hook=OrderedDict)
2016-03-04 19:42:13 +02:00
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):
2013-04-10 17:48:18 +03:00
# Author: Doug Latornell
# Licence: MIT
2019-08-30 10:07:01 +02:00
# URL: https://code.activestate.com/recipes/577081/
"""Return a humanized string representation of a number of bytes.
Assumes `from __future__ import division`.
>>> humanize_bytes(1)
2014-04-24 18:08:40 +03:00
'1 B'
>>> humanize_bytes(1024, precision=1)
'1.0 kB'
2014-04-24 18:08:40 +03:00
>>> humanize_bytes(1024 * 123, precision=1)
'123.0 kB'
2014-04-24 18:08:40 +03:00
>>> humanize_bytes(1024 * 12342, precision=1)
'12.1 MB'
2014-04-24 18:08:40 +03:00
>>> humanize_bytes(1024 * 12342, precision=2)
'12.05 MB'
2014-04-24 18:08:40 +03:00
>>> humanize_bytes(1024 * 1234, precision=2)
'1.21 MB'
2014-04-24 18:08:40 +03:00
>>> humanize_bytes(1024 * 1234 * 1111, precision=2)
'1.31 GB'
2014-04-24 18:08:40 +03:00
>>> humanize_bytes(1024 * 1234 * 1111, precision=1)
'1.3 GB'
"""
abbrevs = [
(1 << 50, 'PB'),
(1 << 40, 'TB'),
(1 << 30, 'GB'),
(1 << 20, 'MB'),
(1 << 10, 'kB'),
2013-04-13 03:49:27 +03:00
(1, 'B')
]
if n == 1:
2013-04-15 06:56:47 +03:00
return '1 B'
for factor, suffix in abbrevs:
if n >= factor:
break
# noinspection PyUnboundLocalVariable
return '%.*f %s' % (precision, n / factor, suffix)
class ExplicitNullAuth(requests.auth.AuthBase):
"""Forces requests to ignore the ``.netrc``.
<https://github.com/psf/requests/issues/2773#issuecomment-174312831>
"""
def __call__(self, r):
return r