Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/private/gen_file.py
2884 views
1
import os
2
import sys
3
4
_copyright = """/*
5
* Copyright 2011-2014 Software Freedom Conservancy
6
*
7
* Licensed under the Apache License, Version 2.0 (the \"License\");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
10
*
11
* http://www.apache.org/licenses/LICENSE-2.0
12
*
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an \"AS IS\" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
18
*/
19
"""
20
21
22
def get_atom_name(name):
23
# We had a todo here to convert camelCase and snake_case to BIG_SNAKE_CASE, but this code
24
# will be removed when BiDi is the default, so we're not going to bother with that.
25
name = os.path.basename(name)
26
return name.upper()
27
28
29
def write_atom_literal(out, name, contents, lang, utf8):
30
# Escape the contents of the file so it can be stored as a literal.
31
contents = contents.replace("\\", "\\\\")
32
contents = contents.replace("\f", "\\f")
33
contents = contents.replace("\n", "\\n")
34
contents = contents.replace("\r", "\\r")
35
contents = contents.replace("\t", "\\t")
36
contents = contents.replace('"', '\\"')
37
38
if "cc" == lang or "hh" == lang:
39
if utf8:
40
line_format = ' "{}",\n'
41
else:
42
line_format = ' L"{}",\n'
43
elif "java" == lang:
44
line_format = ' .append\("{}")\n'
45
else:
46
raise RuntimeError("Unknown language: %s " % lang)
47
48
name = get_atom_name(name)
49
50
if "cc" == lang or "hh" == lang:
51
char_type = "char" if utf8 else "wchar_t"
52
out.write("const %s* const %s[] = {\n" % (char_type, name))
53
elif "java" == lang:
54
out.write(" %s(new StringBuilder()\n" % name)
55
else:
56
raise RuntimeError("Unknown language: %s " % lang)
57
58
# Make the header file play nicely in a terminal: limit lines to 80
59
# characters, but make sure we don't cut off a line in the middle
60
# of an escape sequence.
61
while len(contents) > 70:
62
diff = 70
63
while contents[diff - 1] == "\\":
64
diff = diff - 1
65
line = contents[0:diff]
66
contents = contents[diff:]
67
68
out.write(line_format.format(line))
69
if len(contents) > 0:
70
out.write(line_format.format(contents))
71
72
if "cc" == lang or "hh" == lang:
73
out.write(" NULL\n};\n\n")
74
elif "java" == lang:
75
out.write(" .toString()),\n")
76
77
78
def generate_header(file_name, out, js_map, just_declare, utf8):
79
define_guard = "WEBDRIVER_%s" % os.path.basename(file_name.upper()).replace(
80
".", "_"
81
)
82
include_stddef = "" if utf8 else "\n#include <stddef.h> // For wchar_t."
83
out.write(
84
"""%s
85
86
/* AUTO GENERATED - DO NOT EDIT BY HAND */
87
#ifndef %s
88
#define %s
89
%s
90
#include <string> // For std::(w)string.
91
92
namespace webdriver {
93
namespace atoms {
94
95
"""
96
% (_copyright, define_guard, define_guard, include_stddef)
97
)
98
99
string_type = "std::string" if utf8 else "std::wstring"
100
char_type = "char" if utf8 else "wchar_t"
101
102
for name, file in js_map.items():
103
if just_declare:
104
out.write("extern const %s* const %s[];\n" % (char_type, name.upper()))
105
else:
106
contents = open(file, "r").read()
107
write_atom_literal(out, name, contents, "hh", utf8)
108
109
out.write(
110
"""
111
static inline %s asString(const %s* const atom[]) {
112
%s source;
113
for (int i = 0; atom[i] != NULL; i++) {
114
source += atom[i];
115
}
116
return source;
117
}
118
119
} // namespace atoms
120
} // namespace webdriver
121
122
#endif // %s
123
"""
124
% (string_type, char_type, string_type, define_guard)
125
)
126
127
128
def generate_cc_source(out, js_map, utf8):
129
out.write(
130
"""%s
131
132
/* AUTO GENERATED - DO NOT EDIT BY HAND */
133
134
#include <stddef.h> // For NULL.
135
#include "atoms.h"
136
137
namespace webdriver {
138
namespace atoms {
139
140
"""
141
% _copyright
142
)
143
144
for name, file in js_map.items():
145
contents = open(file, "r").read()
146
write_atom_literal(out, name, contents, "cc", utf8)
147
148
out.write("""
149
} // namespace atoms
150
} // namespace webdriver
151
152
""")
153
154
155
def generate_java_source(file_name, out, preamble, js_map):
156
if not file_name.endswith(".java"):
157
raise RuntimeError("File name must end in .java")
158
class_name = os.path.basename(file_name[:-5])
159
160
out.write(_copyright)
161
out.write("\n")
162
out.write(preamble)
163
out.write("")
164
out.write(
165
"""
166
public enum %s {
167
168
// AUTO GENERATED - DO NOT EDIT BY HAND
169
"""
170
% class_name
171
)
172
173
for name, file in js_map.items():
174
contents = open(file, "r").read()
175
write_atom_literal(out, name, contents, "java", True)
176
177
out.write(
178
"""
179
;
180
private final String value;
181
182
public String getValue() {
183
return value;
184
}
185
186
public String toString() {
187
return getValue();
188
}
189
190
%s(String value) {
191
this.value = value;
192
}
193
}
194
"""
195
% class_name
196
)
197
198
199
def main(argv=[]):
200
lang = argv[1]
201
file_name = argv[2]
202
preamble = argv[3]
203
utf8 = argv[4] == "true"
204
205
js_map = {}
206
for i in range(5, len(argv), 2):
207
js_map[argv[i]] = argv[i + 1]
208
209
with open(file_name, "w") as out:
210
if "cc" == lang:
211
generate_cc_source(out, js_map, utf8)
212
elif "hdecl" == lang:
213
generate_header(file_name, out, js_map, True, utf8)
214
elif "hh" == lang:
215
generate_header(file_name, out, js_map, False, utf8)
216
elif "java" == lang:
217
generate_java_source(file_name, out, preamble, js_map)
218
else:
219
raise RuntimeError("Unknown lang: %s" % lang)
220
221
222
if __name__ == "__main__":
223
main(sys.argv)
224
225