import os
import struct 
import argparse


def file_2_compressed_array(input_file,output_file):
    with open(input_file, "rb") as file_r:
        fileContent = file_r.read()
    with open(output_file, "w") as file_w:
        file_w.write("""
# cython: linetrace=True
# **************************************
#   _     _       ___                
#  | |   (_)     |__ \               
#  | |__  _ _ __    ) |_ __  _   _   
#  | '_ \| | '_ \  / /| '_ \| | | |  
#  | |_) | | | | |/ /_| |_) | |_| |  
#  |_.__/|_|_| |_|____| .__/ \__, |  
#                     | |     __/ |  
#                     |_|    |___/   
#
# Auto generated by bin2py
# author: Charles Watkins
# web:    github.com/chris17453
# **************************************
from cpython cimport array
import array

file_name="{0}"
file_data=array.array('B',[
    """.format(input_file))
        content_len=len(fileContent)
        for i in range(0,content_len):
            if i>0:
                if i%16==0:
                    file_w.write("\n    ")
            chunk=struct.unpack('B', fileContent[i])[0] 

            file_w.write("0x{0:02X}".format(chunk))
            if i!=content_len-1:
                file_w.write(",")

        file_w.write("]\n")



if __name__=='__main__':

    parser = argparse.ArgumentParser(
        prog='bin2py',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
        description="""turn a binary file into a python array""")
    # actions

    parser.add_argument('-i', '--input',     help='binary file to load', default= None,metavar='FILE')
    parser.add_argument('-o', '--output',    help='python file to output', default= None,metavar='FILE')
    
    args = parser.parse_args()

    if args.input and args.output:
        file_2_compressed_array(args.input,args.output)
    else:
        parser.print_help()
    