Fix/improve pretty_format* helpers
This commit is contained in:
parent
b7a576eb52
commit
afdb1430a6
1 changed files with 55 additions and 9 deletions
|
@ -5,30 +5,76 @@
|
||||||
#
|
#
|
||||||
# Pretty formating helpers
|
# Pretty formating helpers
|
||||||
#
|
#
|
||||||
def pretty_format_value(value, encoding='utf8'):
|
|
||||||
|
|
||||||
|
def increment_prefix(prefix):
|
||||||
|
return "%s " % prefix if prefix else " "
|
||||||
|
|
||||||
|
|
||||||
|
def pretty_format_value(value, encoding='utf8', prefix=None):
|
||||||
""" Returned pretty formated value to display """
|
""" Returned pretty formated value to display """
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
return pretty_format_dict(value)
|
return pretty_format_dict(value, encoding=encoding, prefix=prefix)
|
||||||
if isinstance(value, list):
|
if isinstance(value, list):
|
||||||
return ",".join([pretty_format_value(x) for x in value])
|
return pretty_format_list(value, encoding=encoding, prefix=prefix)
|
||||||
if isinstance(value, bytes):
|
if isinstance(value, bytes):
|
||||||
return "'%s'" % value.decode(encoding, errors='replace')
|
return "'%s'" % value.decode(encoding, errors='replace')
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
return "'%s'" % value
|
return "'%s'" % value
|
||||||
return str(value)
|
if value is None:
|
||||||
|
return "None"
|
||||||
|
return "%s (%s)" % (str(value), type(value))
|
||||||
|
|
||||||
|
|
||||||
def pretty_format_dict(attrs):
|
def pretty_format_value_in_list(value, encoding='utf8', prefix=None):
|
||||||
|
"""
|
||||||
|
Returned pretty formated value to display in list
|
||||||
|
|
||||||
|
That method will prefix value with line return and incremented prefix
|
||||||
|
if pretty formated value contains line return.
|
||||||
|
"""
|
||||||
|
prefix = prefix if prefix else ""
|
||||||
|
value = pretty_format_value(value, encoding, prefix)
|
||||||
|
if '\n' in value:
|
||||||
|
inc_prefix = increment_prefix(prefix)
|
||||||
|
value = "\n" + "\n".join([
|
||||||
|
inc_prefix + line
|
||||||
|
for line in value.split('\n')
|
||||||
|
])
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def pretty_format_dict(value, encoding='utf8', prefix=None):
|
||||||
""" Returned pretty formated dict to display """
|
""" Returned pretty formated dict to display """
|
||||||
|
prefix = prefix if prefix else ""
|
||||||
result = []
|
result = []
|
||||||
for attr in sorted(attrs.keys()):
|
for key in sorted(value.keys()):
|
||||||
result.append(" - %s : %s" % (attr, pretty_format_value(attrs[attr])))
|
result.append(
|
||||||
|
"%s- %s : %s" % (
|
||||||
|
prefix, key,
|
||||||
|
pretty_format_value_in_list(
|
||||||
|
value[key],
|
||||||
|
encoding=encoding,
|
||||||
|
prefix=prefix
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
return "\n".join(result)
|
return "\n".join(result)
|
||||||
|
|
||||||
|
|
||||||
def pretty_format_list(row):
|
def pretty_format_list(row, encoding='utf8', prefix=None):
|
||||||
""" Returned pretty formated list to display """
|
""" Returned pretty formated list to display """
|
||||||
|
prefix = prefix if prefix else ""
|
||||||
result = []
|
result = []
|
||||||
for idx, values in enumerate(row):
|
for idx, values in enumerate(row):
|
||||||
result.append(" - #%s : %s" % (idx, pretty_format_value(values)))
|
result.append(
|
||||||
|
"%s- #%s : %s" % (
|
||||||
|
prefix, idx,
|
||||||
|
pretty_format_value_in_list(
|
||||||
|
values,
|
||||||
|
encoding=encoding,
|
||||||
|
prefix=prefix
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
return "\n".join(result)
|
return "\n".join(result)
|
||||||
|
|
Loading…
Reference in a new issue