2024-04-23 18:22:53 -04:00
|
|
|
#!/usr/bin/env python3 -B
|
|
|
|
# coding=utf-8
|
2022-04-01 10:43:22 -04:00
|
|
|
|
2022-04-17 13:48:43 -04:00
|
|
|
"""
|
2024-04-23 18:22:53 -04:00
|
|
|
Copyright (C) 2022-2024 Plato Mavropoulos
|
2022-04-17 13:48:43 -04:00
|
|
|
"""
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-06 18:13:07 -04:00
|
|
|
def padder(padd_count, tab=False):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Generate padding (spaces or tabs) """
|
|
|
|
|
2022-04-15 11:17:58 -04:00
|
|
|
return ('\t' if tab else ' ') * padd_count
|
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-30 18:33:43 -04:00
|
|
|
def to_string(in_object, sep_char=''):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Get String from given input object """
|
|
|
|
|
|
|
|
if type(in_object).__name__ in ('list', 'tuple'):
|
2022-04-30 18:33:43 -04:00
|
|
|
out_string = sep_char.join(map(str, in_object))
|
2022-04-15 11:17:58 -04:00
|
|
|
else:
|
2022-04-30 18:33:43 -04:00
|
|
|
out_string = str(in_object)
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-30 18:33:43 -04:00
|
|
|
return out_string
|
2022-04-21 06:59:40 -04:00
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-21 06:59:40 -04:00
|
|
|
def file_to_bytes(in_object):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Get Bytes from given buffer or file path """
|
|
|
|
|
2022-04-21 06:59:40 -04:00
|
|
|
object_bytes = in_object
|
2024-04-23 18:22:53 -04:00
|
|
|
|
|
|
|
if type(in_object).__name__ not in ('bytes', 'bytearray'):
|
2022-04-21 06:59:40 -04:00
|
|
|
with open(to_string(in_object), 'rb') as object_data:
|
|
|
|
object_bytes = object_data.read()
|
2024-04-23 18:22:53 -04:00
|
|
|
|
2022-04-21 06:59:40 -04:00
|
|
|
return object_bytes
|
2022-04-30 18:33:43 -04:00
|
|
|
|
2024-04-23 18:22:53 -04:00
|
|
|
|
|
|
|
def bytes_to_hex(buffer: bytes, order: str, data_len: int, slice_len: int | None = None) -> str:
|
|
|
|
""" Converts bytes to hex string, controlling endianess, data size and string slicing """
|
|
|
|
|
|
|
|
# noinspection PyTypeChecker
|
|
|
|
return f'{int.from_bytes(buffer, order):0{data_len * 2}X}'[:slice_len]
|
|
|
|
|
|
|
|
|
2022-04-30 18:33:43 -04:00
|
|
|
def is_encased(in_string, chars):
|
2024-04-23 18:22:53 -04:00
|
|
|
""" Check if string starts and ends with given character(s) """
|
|
|
|
|
2022-04-30 18:33:43 -04:00
|
|
|
return in_string.startswith(chars) and in_string.endswith(chars)
|