-
Notifications
You must be signed in to change notification settings - Fork 8
/
webfiles.py
151 lines (128 loc) · 5.14 KB
/
webfiles.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# This program analyses webfiles/* and generates a cpp library for ESP32 / ESP8266
# Each file is converted into PROGMEM data
from pathlib import Path
import mimetypes
cur_dir = Path(__file__).absolute().parent
search_dir = cur_dir / "webfiles"
items = list(search_dir.rglob("*"))
src = cur_dir.parts[-1]
if src == "src":
src = cur_dir.parts[-2]
if src[-7:] == "-master":
src = src[:(len(src)-len("-master"))]
master = src + "_webfiles"
cl = master + "_c"
print("\nAnalyzing dir", search_dir, "..." )
files = {}
content_types = {}
maxuri = 0
for item in items:
if item.is_file():
print(" ", item.relative_to(search_dir))
key = str(item.relative_to(cur_dir)).replace("\\","/").replace("/","_").replace(" ","_").replace(".","_")
files[key]= {}
files[key]['name'] = item.name
files[key]['suffix'] = item.suffix
content_types[files[key]['suffix']] = {}
content_types[files[key]['suffix']]['value'] = mimetypes.types_map[item.suffix]
content_types[files[key]['suffix']]['name'] = master + "_content_type_" + item.suffix.replace(".", "") + "_P"
files[key]['uri'] = "/" + str(item.relative_to(search_dir)).replace("\\","/")
maxuri = max(maxuri, len(files[key]['uri']))
f = open(item, "rb")
content = f.read()
f.close
dumpHexa = ""
size = 0
for c in content:
if size > 0:
dumpHexa += ","
if size % 12 == 0 :
dumpHexa += "\n"
if size % 12 == 0:
dumpHexa += " "
dumpHexa += " 0x%02x" %c
size += 1
files[key]['size'] = size
files[key]['cpp'] = "const char " + key + "_P[] PROGMEM =\n{\n" + dumpHexa + "\n};\n\n"
files[key]['cpp'] += "void " + cl + "::send_" + key + "()\n{\n"
files[key]['cpp'] += " if (" + master + ".server != NULL)\n {\n"
files[key]['cpp'] += " " + master + ".server->send_P(\n"
files[key]['cpp'] += " 200,\n"
files[key]['cpp'] += " " + content_types[files[key]['suffix']]['name'] + ",\n"
files[key]['cpp'] += " " + key + "_P,\n"
files[key]['cpp'] += " " + str(size) + "\n );\n }\n}\n\n"
header = "//Generated by " + str(Path(__file__).name) + "\n\n"
header += "//" + str(len(files)) + " file(s) :\n"
favicon = False
for file in files:
header += "// " + files[file]['uri'] + "\n"
header += "// size : " + str(files[file]['size']) + " bytes\n"
header += "// content_type : " + content_types[files[file]['suffix']]['value'] + "\n"
if file == "webfiles_favicon_ico" :
favicon = True
header += "\n#ifndef " + master + "_h\n"
header += "#define " + master + "_h\n\n"
header += "#if defined(ESP32)\n"
header += " #include <WebServer.h>\n"
header += "#elif defined(ESP8266)\n"
header += " #include <ESP8266WebServer.h>\n"
header += " #define WebServer ESP8266WebServer\n"
header += "#endif\n\n"
header += "class " + cl + "\n{\n"
header += " public :\n " + cl + "();\n\n"
header += " void begin(WebServer &webServer);\n"
if favicon:
header += " void activateFavicon(); //must be specificly activated\n\n"
header += " WebServer *server;\n\n"
header += " private :\n"
for file in files:
header += " static void send_" + file + "();\n"
header += "};\n\nextern " + cl + " " + master + ";\n\n"
for file in files:
header += "extern const char " + file + "_path_P[];\n"
header += "\n#endif\n\n"
master_h = master + ".h"
f = open(cur_dir / master_h, "w")
f.write(header)
f.close
cpp = "//Generated by " + str(Path(__file__).name) + "\n\n"
cpp += "#include \"" + master + ".h\"\n\n"
cpp += cl + " " + master + ";\n\n"
cpp += cl + "::" + cl + "()\n"
cpp += "{\n"
cpp += " server = NULL;\n"
cpp += "}\n\n"
for file in files:
cpp += "const char " + file + "_path_P[] PROGMEM = \"" + files[file]['uri'] + "\";\n"
cpp += '\n'
for content_type in content_types:
cpp += "const char " + content_types[content_type]['name'] + "[] PROGMEM = \"" + content_types[content_type]['value'] + "\";\n"
cpp += "\nvoid " + cl + "::begin(WebServer &webServer)\n"
cpp += "{\n"
cpp += " server = &webServer;\n\n"
cpp += " char path[" + str(maxuri+1) + "];\n"
for file in files:
if file != "webfiles_favicon_ico":
cpp += "\n strcpy_P(path, " + file + "_path_P);\n"
cpp += " server->on(path, send_" + file + ");\n"
cpp += "}\n\n"
if favicon:
cpp += "void " + cl + "::activateFavicon()\n"
cpp += "{\n"
cpp += " char path[" + str(maxuri+1) + "];\n"
cpp += " if (server != NULL)\n"
cpp += " {\n"
cpp += " strcpy_P(path, webfiles_favicon_ico_path_P);\n"
cpp += " server->on(path, send_webfiles_favicon_ico);\n"
cpp += " }\n"
cpp += "}\n\n"
for file in files:
cpp += files[file]['cpp']
master_cpp = master + ".cpp"
f = open(cur_dir / master_cpp, "w")
f.write(cpp)
f.close
print("\nGenerated files :")
print(" ", cur_dir / master_h)
print(" ", cur_dir / master_cpp)
input('Press ENTER to quit...')