Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/private/gen_file.py
4500 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(f"Unknown language: {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(f"const {char_type}* const {name}[] = {{\n")
53
elif "java" == lang:
54
out.write(f" {name}(new StringBuilder()\n")
55
else:
56
raise RuntimeError(f"Unknown language: {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_{}".format(os.path.basename(file_name.upper()).replace(".", "_"))
80
include_stddef = "" if utf8 else "\n#include <stddef.h> // For wchar_t."
81
out.write(
82
f"""{_copyright}
83
84
/* AUTO GENERATED - DO NOT EDIT BY HAND */
85
#ifndef {define_guard}
86
#define {define_guard}
87
{include_stddef}
88
#include <string> // For std::(w)string.
89
90
namespace webdriver {{
91
namespace atoms {{
92
93
"""
94
)
95
96
string_type = "std::string" if utf8 else "std::wstring"
97
char_type = "char" if utf8 else "wchar_t"
98
99
for name, file in js_map.items():
100
if just_declare:
101
out.write(f"extern const {char_type}* const {name.upper()}[];\n")
102
else:
103
contents = open(file).read()
104
write_atom_literal(out, name, contents, "hh", utf8)
105
106
out.write(
107
f"""
108
static inline {string_type} asString(const {char_type}* const atom[]) {{
109
{string_type} source;
110
for (int i = 0; atom[i] != NULL; i++) {{
111
source += atom[i];
112
}}
113
return source;
114
}}
115
116
}} // namespace atoms
117
}} // namespace webdriver
118
119
#endif // {define_guard}
120
"""
121
)
122
123
124
def generate_cc_source(out, js_map, utf8):
125
out.write(
126
f"""{_copyright}
127
128
/* AUTO GENERATED - DO NOT EDIT BY HAND */
129
130
#include <stddef.h> // For NULL.
131
#include "atoms.h"
132
133
namespace webdriver {{
134
namespace atoms {{
135
136
"""
137
)
138
139
for name, file in js_map.items():
140
contents = open(file).read()
141
write_atom_literal(out, name, contents, "cc", utf8)
142
143
out.write("""
144
} // namespace atoms
145
} // namespace webdriver
146
147
""")
148
149
150
def generate_java_source(file_name, out, preamble, js_map):
151
if not file_name.endswith(".java"):
152
raise RuntimeError("File name must end in .java")
153
class_name = os.path.basename(file_name[:-5])
154
155
out.write(_copyright)
156
out.write("\n")
157
out.write(preamble)
158
out.write("")
159
out.write(
160
f"""
161
public enum {class_name} {{
162
163
// AUTO GENERATED - DO NOT EDIT BY HAND
164
"""
165
)
166
167
for name, file in js_map.items():
168
contents = open(file).read()
169
write_atom_literal(out, name, contents, "java", True)
170
171
out.write(
172
f"""
173
;
174
private final String value;
175
176
public String getValue() {{
177
return value;
178
}}
179
180
public String toString() {{
181
return getValue();
182
}}
183
184
{class_name}(String value) {{
185
this.value = value;
186
}}
187
}}
188
"""
189
)
190
191
192
def main(argv=[]):
193
lang = argv[1]
194
file_name = argv[2]
195
preamble = argv[3]
196
utf8 = argv[4] == "true"
197
198
js_map = {}
199
for i in range(5, len(argv), 2):
200
js_map[argv[i]] = argv[i + 1]
201
202
with open(file_name, "w") as out:
203
if "cc" == lang:
204
generate_cc_source(out, js_map, utf8)
205
elif "hdecl" == lang:
206
generate_header(file_name, out, js_map, True, utf8)
207
elif "hh" == lang:
208
generate_header(file_name, out, js_map, False, utf8)
209
elif "java" == lang:
210
generate_java_source(file_name, out, preamble, js_map)
211
else:
212
raise RuntimeError(f"Unknown lang: {lang}")
213
214
215
if __name__ == "__main__":
216
main(sys.argv)
217
218