Skip to content

Commit

Permalink
address ecoutils import issue, fixes #294
Browse files Browse the repository at this point in the history
  • Loading branch information
mahmoud committed Oct 11, 2021
1 parent d818b4a commit 270e974
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions boltons/ecoutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,59 +354,60 @@ def get_profile(**kwargs):
return ret


_real_safe_repr = pprint._safe_repr


def _fake_json_dumps(val, indent=2):
# never do this. this is a hack for Python 2.4. Python 2.5 added
# the json module for a reason.
def _fake_safe_repr(*a, **kw):
res, is_read, is_rec = _real_safe_repr(*a, **kw)
if res == 'None':
res = 'null'
if res == 'True':
res = 'true'
if res == 'False':
res = 'false'
if not (res.startswith("'") or res.startswith("u'")):
res = res
else:
if res.startswith('u'):
res = res[1:]
try:
import json

def dumps(val, indent):
if indent:
return json.dumps(val, sort_keys=True, indent=indent)
return json.dumps(val, sort_keys=True)

except ImportError:
_real_safe_repr = pprint._safe_repr

def _fake_json_dumps(val, indent=2):
# never do this. this is a hack for Python 2.4. Python 2.5 added
# the json module for a reason.
def _fake_safe_repr(*a, **kw):
res, is_read, is_rec = _real_safe_repr(*a, **kw)
if res == 'None':
res = 'null'
if res == 'True':
res = 'true'
if res == 'False':
res = 'false'
if not (res.startswith("'") or res.startswith("u'")):
res = res
else:
if res.startswith('u'):
res = res[1:]

contents = res[1:-1]
contents = contents.replace('"', '').replace(r'\"', '')
res = '"' + contents + '"'
return res, is_read, is_rec
contents = res[1:-1]
contents = contents.replace('"', '').replace(r'\"', '')
res = '"' + contents + '"'
return res, is_read, is_rec

pprint._safe_repr = _fake_safe_repr
try:
ret = pprint.pformat(val, indent=indent)
finally:
pprint._safe_repr = _real_safe_repr
pprint._safe_repr = _fake_safe_repr
try:
ret = pprint.pformat(val, indent=indent)
finally:
pprint._safe_repr = _real_safe_repr

return ret

def dumps(val, indent):
ret = _fake_json_dumps(val, indent=indent)
if not indent:
ret = re.sub(r'\n\s*', ' ', ret)
return ret

return ret


def get_profile_json(indent=False):
if indent:
indent = 2
else:
indent = 0
try:
import json

def dumps(val, indent):
if indent:
return json.dumps(val, sort_keys=True, indent=indent)
return json.dumps(val, sort_keys=True)

except ImportError:
def dumps(val, indent):
ret = _fake_json_dumps(val, indent=indent)
if not indent:
ret = re.sub(r'\n\s*', ' ', ret)
return ret

data_dict = get_profile()
return dumps(data_dict, indent)
Expand Down

0 comments on commit 270e974

Please sign in to comment.