*/
"""
-def make_sublist_group(lst: list, grp: int) -> list:
+def make_sublist_group (lst: list, grp: int) -> list:
"""
Group list elements into sublists.
make_sublist_group([1, 2, 3, 4, 5, 6, 7], 3) = [[1, 2, 3], [4, 5, 6], 7]
"""
return [lst[i:i+grp] for i in range(0, len(lst), grp)]
-def generate_c_array_data(bindata: bytes, element_prefix: str, element_size: int, element_suffix: str, newline_value: str, newline_after: int, element_separator: str):
+def generate_c_array_data (bindata: bytes,
+ element_prefix: str,
+ element_size: int,
+ element_suffix: str,
+ newline_value: str,
+ newline_after: int,
+ element_separator: str) -> str:
"""
- Generate the contents of a C array
+ Generate the contents of a C array as a hex string
+ e.g:
+ 0x10, 0x20, 0x30, 0x40,
+ 0x50, 0x60, 0x70, 0x80,
"""
hexstr = binascii.hexlify(bindata).decode("UTF-8")
array = []
return newline_value.join([element_separator.join(e) + element_separator for e in array])
-def decorate_c_array_data (hexdata: str, var_name: str, var_type: str, newline_value: str):
+def decorate_c_array_data (hexdata: str,
+ var_name: str,
+ var_type: str,
+ newline_value: str):
+ """
+ Place @hexdata into a valid C array named @var_name of C type @var_type.
+ """
ret = var_type + " " + var_name + "[] = {" + newline_value
ret += hexdata + newline_value
ret += "};" + newline_value
assert len(file_content) % args.element_size == 0
# get a relative path from the source file to the header to use in an #include
- source_to_header = os.path.relpath (os.path.dirname(args.header_output), os.path.dirname (args.output))
+ source_to_header = os.path.relpath (os.path.dirname (args.header_output),
+ os.path.dirname (args.output))
ret = autogenerated_notice
- ret += "#include \"" + os.path.join (source_to_header, os.path.basename (args.header_output)) + "\"" + args.linebreak_value
+ ret += "#include \""
+ ret += os.path.join (source_to_header,
+ os.path.basename (args.header_output))
+ ret += "\"" + args.linebreak_value
- ret += decorate_c_array_data (generate_c_array_data (file_content, args.element_prefix, args.element_size, args.element_suffix, args.linebreak_value, args.linebreak, args.separator), args.array_name, args.element_type, args.linebreak_value)
+ arr_data = generate_c_array_data (file_content,
+ args.element_prefix,
+ args.element_size,
+ args.element_suffix,
+ args.linebreak_value,
+ args.linebreak,
+ args.separator)
+ ret += decorate_c_array_data (arr_data,
+ args.array_name,
+ args.element_type,
+ args.linebreak_value)
with open (args.output, 'w') as f:
f.write (ret)