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-16 12:56:12 +01:00
|
|
|
"""Increment the given prefix with two spaces"""
|
2023-01-06 19:36:14 +01:00
|
|
|
return f'{prefix if prefix else " "} '
|
2021-11-18 19:56:51 +01:00
|
|
|
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def pretty_format_value(value, encoding="utf8", prefix=None):
|
|
|
|
"""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-16 12:56:12 +01:00
|
|
|
return f"{value} ({type(value)})"
|
2021-11-18 19:56:51 +01:00
|
|
|
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def pretty_format_value_in_list(value, encoding="utf8", prefix=None):
|
2021-11-18 19:56:51 +01:00
|
|
|
"""
|
|
|
|
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)
|
2023-01-16 12:56:12 +01:00
|
|
|
if "\n" in value:
|
2021-11-18 19:56:51 +01:00
|
|
|
inc_prefix = increment_prefix(prefix)
|
2023-01-16 12:56:12 +01:00
|
|
|
value = "\n" + "\n".join([inc_prefix + line for line in value.split("\n")])
|
2021-11-18 19:56:51 +01:00
|
|
|
return value
|
2021-05-26 11:56:45 +02:00
|
|
|
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def pretty_format_dict(value, encoding="utf8", prefix=None):
|
|
|
|
"""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-16 12:56:12 +01:00
|
|
|
f"{prefix}- {key} : "
|
|
|
|
+ pretty_format_value_in_list(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)
|
|
|
|
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def pretty_format_list(row, encoding="utf8", prefix=None):
|
|
|
|
"""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-16 12:56:12 +01:00
|
|
|
f"{prefix}- #{idx} : "
|
|
|
|
+ pretty_format_value_in_list(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)
|
2023-07-10 11:56:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def pretty_format_timedelta(timedelta):
|
|
|
|
"""Format timedelta object"""
|
|
|
|
seconds = int(timedelta.total_seconds())
|
|
|
|
if seconds < 1:
|
|
|
|
return "less than one second"
|
|
|
|
periods = [
|
|
|
|
("year", 60 * 60 * 24 * 365),
|
|
|
|
("month", 60 * 60 * 24 * 30),
|
|
|
|
("day", 60 * 60 * 24),
|
|
|
|
("hour", 60 * 60),
|
|
|
|
("minute", 60),
|
|
|
|
("second", 1),
|
|
|
|
]
|
|
|
|
|
|
|
|
strings = []
|
|
|
|
for period_name, period_seconds in periods:
|
|
|
|
if seconds >= period_seconds:
|
|
|
|
period_value, seconds = divmod(seconds, period_seconds)
|
|
|
|
strings.append(f'{period_value} {period_name}{"s" if period_value > 1 else ""}')
|
|
|
|
|
|
|
|
return ", ".join(strings)
|