forked from Dynatrace/ufo-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data2h.py
79 lines (63 loc) · 2.25 KB
/
data2h.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import gzip
import shutil
import sys
import binascii
import cStringIO
import io
import os
def convert_file(inputfilename, outputfilename):
try:
filename = outputfilename
inputfile = open(inputfilename, "rb")
outputfile = open(outputfilename, "w")
binary = inputfile.read()
if len(binary) > 0:
#outputfile.write('static const char PROGMEM ') ## used with ESP8266
outputfile.write('static const char ') ## ESP32
index = filename.rfind('/')
if index == -1:
index = filename.rfind('\\')
if index >= 0:
filename = filename[index+1:]
outputfile.write(filename.replace('.', '_'))
outputfile.write('[')
outputfile.write(str(len(binary)))
outputfile.write('] = {\n')
if sys.version_info[0] >= 3:
text = str(binascii.hexlify(binary), 'UTF-8')
else:
text = str(binascii.hexlify(binary))
outputfile.write('0x')
outputfile.write(text[0:2])
i = 2
while i < len(text):
outputfile.write(', ')
if i % 100 == 0:
outputfile.write('\n')
outputfile.write('0x')
outputfile.write(text[i:i+2])
i += 2
outputfile.write('\n};\n\n')
outputfile.close()
inputfile.close()
print("converted: " + inputfilename + " --> " + outputfilename)
except IOError:
print("cannot open input file %s" % inputfilename)
outputfile.close()
try:
if len(sys.argv) < 3:
print("usage: data2h.py <inputfile> <outputfile>")
quit()
if sys.argv[1].endswith("html"):
src = open(sys.argv[1], "rb")
print("HTML file detected, gzipping file first before converting to header file.")
gzf = gzip.GzipFile(filename="html.gz", mode='wb')
gzf.write(src.read())
src.close()
gzf.close()
convert_file("html.gz", sys.argv[2])
os.remove("html.gz")
else:
convert_file(sys.argv[1], sys.argv[2])
except IOError:
print "cannot open ", sys.argv[1], "for reading or ", sys.argv[2], " for writing"