Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: third_party/WebKit/Source/devtools/scripts/generate_protocol_externs.py

Issue 2464463002: Revert of DevTools: clean up scripts folder (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright (c) 2011 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import os
31 import re
32 try:
33 import json
34 except ImportError:
35 import simplejson as json
36
37 type_traits = {
38 "any": "*",
39 "string": "string",
40 "integer": "number",
41 "number": "number",
42 "boolean": "boolean",
43 "array": "!Array.<*>",
44 "object": "!Object",
45 }
46
47 promisified_domains = {
48 "Accessibility",
49 "Animation",
50 "Browser",
51 "CSS",
52 "Emulation",
53 "HeapProfiler",
54 "Profiler",
55 "LayerTree"
56 }
57
58 ref_types = {}
59
60 def full_qualified_type_id(domain_name, type_id):
61 if type_id.find(".") == -1:
62 return "%s.%s" % (domain_name, type_id)
63 return type_id
64
65
66 def fix_camel_case(name):
67 prefix = ""
68 if name[0] == "-":
69 prefix = "Negative"
70 name = name[1:]
71 refined = re.sub(r'-(\w)', lambda pat: pat.group(1).upper(), name)
72 refined = to_title_case(refined)
73 return prefix + re.sub(r'(?i)HTML|XML|WML|API', lambda pat: pat.group(0).upp er(), refined)
74
75
76 def to_title_case(name):
77 return name[:1].upper() + name[1:]
78
79
80 def generate_enum(name, json):
81 enum_members = []
82 for member in json["enum"]:
83 enum_members.append(" %s: \"%s\"" % (fix_camel_case(member), member))
84 return "\n/** @enum {string} */\n%s = {\n%s\n};\n" % (name, (",\n".join(enum _members)))
85
86
87 def param_type(domain_name, param):
88 if "type" in param:
89 if param["type"] == "array":
90 items = param["items"]
91 return "!Array.<%s>" % param_type(domain_name, items)
92 else:
93 return type_traits[param["type"]]
94 if "$ref" in param:
95 type_id = full_qualified_type_id(domain_name, param["$ref"])
96 if type_id in ref_types:
97 return ref_types[type_id]
98 else:
99 print "Type not found: " + type_id
100 return "!! Type not found: " + type_id
101
102
103 def load_schema(file, domains):
104 input_file = open(file, "r")
105 json_string = input_file.read()
106 parsed_json = json.loads(json_string)
107 domains.extend(parsed_json["domains"])
108
109
110 def generate_protocol_externs(output_path, file1, file2):
111 domains = []
112 load_schema(file1, domains)
113 load_schema(file2, domains)
114 output_file = open(output_path, "w")
115
116 for domain in domains:
117 domain_name = domain["domain"]
118 if "types" in domain:
119 for type in domain["types"]:
120 type_id = full_qualified_type_id(domain_name, type["id"])
121 ref_types[type_id] = "%sAgent.%s" % (domain_name, type["id"])
122
123 for domain in domains:
124 domain_name = domain["domain"]
125 promisified = domain_name in promisified_domains
126
127 output_file.write("\n\n/**\n * @constructor\n*/\n")
128 output_file.write("Protocol.%sAgent = function(){};\n" % domain_name)
129
130 if "commands" in domain:
131 for command in domain["commands"]:
132 output_file.write("\n/**\n")
133 params = []
134 has_return_value = "returns" in command
135 explicit_parameters = promisified and has_return_value
136 if "parameters" in command:
137 for in_param in command["parameters"]:
138 if "optional" in in_param:
139 if explicit_parameters:
140 params.append("%s" % in_param["name"])
141 output_file.write(" * @param {%s|undefined} %s\n " %
142 (param_type(domain_name, in_pa ram), in_param["name"]))
143 else:
144 params.append("opt_%s" % in_param["name"])
145 output_file.write(" * @param {%s=} opt_%s\n" %
146 (param_type(domain_name, in_pa ram), in_param["name"]))
147 else:
148 params.append(in_param["name"])
149 output_file.write(" * @param {%s} %s\n" % (param_typ e(domain_name, in_param), in_param["name"]))
150 returns = []
151 returns.append("?Protocol.Error")
152 if ("error" in command):
153 returns.append("%s=" % param_type(domain_name, command["erro r"]))
154 if (has_return_value):
155 for out_param in command["returns"]:
156 if ("optional" in out_param):
157 returns.append("%s=" % param_type(domain_name, out_p aram))
158 else:
159 returns.append("%s" % param_type(domain_name, out_pa ram))
160 callback_return_type = "void="
161 if explicit_parameters:
162 callback_return_type = "T"
163 elif promisified:
164 callback_return_type = "T="
165 output_file.write(" * @param {function(%s):%s} opt_callback\n" % (", ".join(returns), callback_return_type))
166 if (promisified):
167 output_file.write(" * @return {!Promise.<T>}\n")
168 output_file.write(" * @template T\n")
169 params.append("opt_callback")
170
171 output_file.write(" */\n")
172 output_file.write("Protocol.%sAgent.prototype.%s = function(%s) {}\n" % (domain_name, command["name"], ", ".join(params)))
173 output_file.write("/** @param {function(%s):void=} opt_callback */\n" % ", ".join(returns))
174 output_file.write("Protocol.%sAgent.prototype.invoke_%s = functi on(obj, opt_callback) {}\n" % (domain_name, command["name"]))
175
176 output_file.write("\n\n\nvar %sAgent = function(){};\n" % domain_name)
177
178 if "types" in domain:
179 for type in domain["types"]:
180 if type["type"] == "object":
181 typedef_args = []
182 if "properties" in type:
183 for property in type["properties"]:
184 suffix = ""
185 if ("optional" in property):
186 suffix = "|undefined"
187 if "enum" in property:
188 enum_name = "%sAgent.%s%s" % (domain_name, type[ "id"], to_title_case(property["name"]))
189 output_file.write(generate_enum(enum_name, prope rty))
190 typedef_args.append("%s:(%s%s)" % (property["nam e"], enum_name, suffix))
191 else:
192 typedef_args.append("%s:(%s%s)" % (property["nam e"], param_type(domain_name, property), suffix))
193 if (typedef_args):
194 output_file.write("\n/** @typedef {!{%s}} */\n%sAgent.%s ;\n" % (", ".join(typedef_args), domain_name, type["id"]))
195 else:
196 output_file.write("\n/** @typedef {!Object} */\n%sAgent. %s;\n" % (domain_name, type["id"]))
197 elif type["type"] == "string" and "enum" in type:
198 output_file.write(generate_enum("%sAgent.%s" % (domain_name, type["id"]), type))
199 elif type["type"] == "array":
200 output_file.write("\n/** @typedef {!Array.<!%s>} */\n%sAgent .%s;\n" % (param_type(domain_name, type["items"]), domain_name, type["id"]))
201 else:
202 output_file.write("\n/** @typedef {%s} */\n%sAgent.%s;\n" % (type_traits[type["type"]], domain_name, type["id"]))
203
204 output_file.write("/** @interface */\n")
205 output_file.write("%sAgent.Dispatcher = function() {};\n" % domain_name)
206 if "events" in domain:
207 for event in domain["events"]:
208 params = []
209 if ("parameters" in event):
210 output_file.write("/**\n")
211 for param in event["parameters"]:
212 if ("optional" in param):
213 params.append("opt_%s" % param["name"])
214 output_file.write(" * @param {%s=} opt_%s\n" % (para m_type(domain_name, param), param["name"]))
215 else:
216 params.append(param["name"])
217 output_file.write(" * @param {%s} %s\n" % (param_typ e(domain_name, param), param["name"]))
218 output_file.write(" */\n")
219 output_file.write("%sAgent.Dispatcher.prototype.%s = function(%s ) {};\n" % (domain_name, event["name"], ", ".join(params)))
220
221 for domain in domains:
222 domain_name = domain["domain"]
223 uppercase_length = 0
224 while uppercase_length < len(domain_name) and domain_name[uppercase_leng th].isupper():
225 uppercase_length += 1
226
227 output_file.write("/** @return {!Protocol.%sAgent}*/\n" % domain_name)
228 output_file.write("Protocol.Target.prototype.%s = function(){};\n" % (do main_name[:uppercase_length].lower() + domain_name[uppercase_length:] + "Agent") )
229
230 output_file.write("/**\n * @param {!%sAgent.Dispatcher} dispatcher\n */\ n" % domain_name)
231 output_file.write("Protocol.Target.prototype.register%sDispatcher = func tion(dispatcher) {}\n" % domain_name)
232
233
234 output_file.close()
235
236 if __name__ == "__main__":
237 import sys
238 import os.path
239 program_name = os.path.basename(__file__)
240 if len(sys.argv) < 5 or sys.argv[1] != "-o":
241 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE_1 INPUT_FILE_2\n" % program_name)
242 exit(1)
243 output_path = sys.argv[2]
244 input_path_1 = sys.argv[3]
245 input_path_2 = sys.argv[4]
246 generate_protocol_externs(output_path, input_path_1, input_path_2)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698