2021-05-26 14:44:30 +02:00
|
|
|
""" Some really common helper functions """
|
|
|
|
|
2021-05-26 11:56:45 +02:00
|
|
|
#
|
|
|
|
# Pretty formating helpers
|
|
|
|
#
|
2021-11-18 19:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def increment_prefix(prefix):
|
2023-01-06 19:36:14 +01:00
|
|
|
""" Increment the given prefix with two spaces """
|
|
|
|
return f'{prefix if prefix else " "} '
|
2021-11-18 19:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def pretty_format_value(value, encoding='utf8', prefix=None):
|
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):
|
2021-11-18 19:56:51 +01:00
|
|
|
return pretty_format_dict(value, encoding=encoding, prefix=prefix)
|
2021-05-26 11:56:45 +02:00
|
|
|
if isinstance(value, list):
|
2021-11-18 19:56:51 +01:00
|
|
|
return pretty_format_list(value, encoding=encoding, prefix=prefix)
|
2021-05-26 14:44:30 +02:00
|
|
|
if isinstance(value, bytes):
|
2023-01-06 19:36:14 +01:00
|
|
|
return f"'{value.decode(encoding, errors='replace')}'"
|
2021-05-26 14:44:30 +02:00
|
|
|
if isinstance(value, str):
|
2023-01-06 19:36:14 +01:00
|
|
|
return f"'{value}'"
|
2021-11-18 19:56:51 +01:00
|
|
|
if value is None:
|
|
|
|
return "None"
|
2023-01-06 19:36:14 +01:00
|
|
|
return f'{value} ({type(value)})'
|
2021-11-18 19:56:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-05-26 11:56:45 +02:00
|
|
|
|
|
|
|
|
2021-11-18 19:56:51 +01:00
|
|
|
def pretty_format_dict(value, encoding='utf8', prefix=None):
|
2021-05-26 14:44:30 +02:00
|
|
|
""" Returned pretty formated dict to display """
|
2021-11-18 19:56:51 +01:00
|
|
|
prefix = prefix if prefix else ""
|
2021-05-26 11:56:45 +02:00
|
|
|
result = []
|
2021-11-18 19:56:51 +01:00
|
|
|
for key in sorted(value.keys()):
|
|
|
|
result.append(
|
2023-01-06 22:13:28 +01:00
|
|
|
f'{prefix}- {key} : '
|
|
|
|
+ pretty_format_value_in_list(
|
2023-01-06 19:36:14 +01:00
|
|
|
value[key],
|
|
|
|
encoding=encoding,
|
|
|
|
prefix=prefix
|
2021-11-18 19:56:51 +01:00
|
|
|
)
|
|
|
|
)
|
2021-05-26 11:56:45 +02:00
|
|
|
return "\n".join(result)
|
|
|
|
|
|
|
|
|
2021-11-18 19:56:51 +01:00
|
|
|
def pretty_format_list(row, encoding='utf8', prefix=None):
|
2021-05-26 14:44:30 +02:00
|
|
|
""" Returned pretty formated list to display """
|
2021-11-18 19:56:51 +01:00
|
|
|
prefix = prefix if prefix else ""
|
2021-05-26 11:56:45 +02:00
|
|
|
result = []
|
|
|
|
for idx, values in enumerate(row):
|
2021-11-18 19:56:51 +01:00
|
|
|
result.append(
|
2023-01-06 22:13:28 +01:00
|
|
|
f'{prefix}- #{idx} : '
|
|
|
|
+ pretty_format_value_in_list(
|
2023-01-06 19:36:14 +01:00
|
|
|
values,
|
|
|
|
encoding=encoding,
|
|
|
|
prefix=prefix
|
2021-11-18 19:56:51 +01:00
|
|
|
)
|
|
|
|
)
|
2021-05-26 11:56:45 +02:00
|
|
|
return "\n".join(result)
|