forked from YorffoeG/diyscip
-
Notifications
You must be signed in to change notification settings - Fork 4
/
html2cpp.py
77 lines (58 loc) · 1.83 KB
/
html2cpp.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
77
#!/usr/bin/python
##
# DIYSCIP (c) by Geoffroy HUBERT - yorffoeg@gmail.com
# This file is part of DIYSCIP <https://github.com/yorffoeg/diyscip>.
#
# DIYSCIP is licensed under a
# Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
#
# You should have received a copy of the license along with this
# work. If not, see <http://creativecommons.org/licenses/by-nc-sa/4.0/>.
#
# DIYSCIP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY;
import sys, getopt
import gzip
import shutil
def usage():
print '\nhtml2cpp.py -s <source>'
print 'Convert an html source to cpp source\n\n'
def main(argv):
source = None
destination = None
try:
opts, args = getopt.getopt(argv, "s:d:",["source=", "destination="])
except getopt.GetoptError,err:
print str(err)
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-s", "--source"):
source = arg
elif opt in ("-d", "--destination"):
destination = arg
if not source:
usage()
sys.exit(2)
if not destination:
destination = source + '.h'
with open(source, 'rb') as f_in:
with gzip.open(source + '.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
f_in.close()
f_out.close()
hf = open(destination, 'w')
hf.write('/* automatically created by html2cpp from ' + source + ' */\n');
hf.write('const char home[] PROGMEM = {');
gz = open(source + '.gz', 'rb')
ba = bytearray(gz.read())
len= 0
for byte in ba:
hf.write('0x' + ''.join('{:02x}'.format(byte)) + ',')
len = len + 1
gz.close()
hf.write('0};\n');
hf.write('const int home_len = ' + str(len + 1) + ';\n')
hf.close()
if __name__ == "__main__":
main(sys.argv[1:])