2021-05-26 14:44:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
""" Some really common helper functions """
|
|
|
|
|
2021-05-26 11:56:45 +02:00
|
|
|
#
|
|
|
|
# Pretty formating helpers
|
|
|
|
#
|
|
|
|
def pretty_format_value(value, encoding='utf8'):
|
2021-05-26 14:44:30 +02:00
|
|
|
""" Returned pretty formated value to display """
|
2021-05-26 11:56:45 +02:00
|
|
|
if isinstance(value, dict):
|
|
|
|
return pretty_format_dict(value)
|
|
|
|
if isinstance(value, list):
|
|
|
|
return ",".join([pretty_format_value(x) for x in value])
|
2021-05-26 14:44:30 +02:00
|
|
|
if isinstance(value, bytes):
|
2021-05-26 11:56:45 +02:00
|
|
|
return "'%s'" % value.decode(encoding, errors='replace')
|
2021-05-26 14:44:30 +02:00
|
|
|
if isinstance(value, str):
|
2021-05-26 11:56:45 +02:00
|
|
|
return "'%s'" % value
|
|
|
|
return str(value)
|
|
|
|
|
|
|
|
|
|
|
|
def pretty_format_dict(attrs):
|
2021-05-26 14:44:30 +02:00
|
|
|
""" Returned pretty formated dict to display """
|
2021-05-26 11:56:45 +02:00
|
|
|
result = []
|
|
|
|
for attr in sorted(attrs.keys()):
|
|
|
|
result.append(" - %s : %s" % (attr, pretty_format_value(attrs[attr])))
|
|
|
|
return "\n".join(result)
|
|
|
|
|
|
|
|
|
|
|
|
def pretty_format_list(row):
|
2021-05-26 14:44:30 +02:00
|
|
|
""" Returned pretty formated list to display """
|
2021-05-26 11:56:45 +02:00
|
|
|
result = []
|
|
|
|
for idx, values in enumerate(row):
|
|
|
|
result.append(" - #%s : %s" % (idx, pretty_format_value(values)))
|
|
|
|
return "\n".join(result)
|