OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 Google Inc. All rights reserved. | 2 # Copyright (c) 2011 Google Inc. All rights reserved. |
3 # | 3 # |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
11 # copyright notice, this list of conditions and the following disclaimer | 11 # copyright notice, this list of conditions and the following disclaimer |
12 # in the documentation and/or other materials provided with the | 12 # in the documentation and/or other materials provided with the |
13 # distribution. | 13 # distribution. |
14 # * Neither the name of Google Inc. nor the names of its | 14 # * Neither the name of Google Inc. nor the names of its |
15 # contributors may be used to endorse or promote products derived from | 15 # contributors may be used to endorse or promote products derived from |
16 # this software without specific prior written permission. | 16 # this software without specific prior written permission. |
17 # | 17 # |
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
| 30 import os |
30 import re | 31 import re |
| 32 try: |
| 33 import json |
| 34 except ImportError: |
| 35 import simplejson as json |
31 | 36 |
32 type_traits = { | 37 type_traits = { |
33 "any": "*", | 38 "any": "*", |
34 "string": "string", | 39 "string": "string", |
35 "integer": "number", | 40 "integer": "number", |
36 "number": "number", | 41 "number": "number", |
37 "boolean": "boolean", | 42 "boolean": "boolean", |
38 "array": "!Array.<*>", | 43 "array": "!Array.<*>", |
39 "object": "!Object", | 44 "object": "!Object", |
40 } | 45 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 return type_traits[param["type"]] | 86 return type_traits[param["type"]] |
82 if "$ref" in param: | 87 if "$ref" in param: |
83 type_id = full_qualified_type_id(domain_name, param["$ref"]) | 88 type_id = full_qualified_type_id(domain_name, param["$ref"]) |
84 if type_id in ref_types: | 89 if type_id in ref_types: |
85 return ref_types[type_id] | 90 return ref_types[type_id] |
86 else: | 91 else: |
87 print "Type not found: " + type_id | 92 print "Type not found: " + type_id |
88 return "!! Type not found: " + type_id | 93 return "!! Type not found: " + type_id |
89 | 94 |
90 | 95 |
91 def generate_protocol_externs(output_path, input_path): | 96 def load_schema(file, domains): |
92 input_file = open(input_path, "r") | 97 input_file = open(file, "r") |
93 json_string = input_file.read() | 98 json_string = input_file.read() |
94 json_string = json_string.replace(": true", ": True") | 99 parsed_json = json.loads(json_string) |
95 json_string = json_string.replace(": false", ": False") | 100 domains.append(parsed_json["domains"]) |
96 json_api = eval(json_string)["domains"] | |
97 | 101 |
| 102 |
| 103 def generate_protocol_externs(output_path, file1, file2): |
| 104 domains = [] |
| 105 load_schema(file1, domains) |
| 106 load_schema(file2, domains) |
98 output_file = open(output_path, "w") | 107 output_file = open(output_path, "w") |
99 | 108 |
100 output_file.write( | 109 output_file.write( |
101 """ | 110 """ |
102 var InspectorBackend = {} | 111 var InspectorBackend = {} |
103 | 112 |
104 var Protocol = {}; | 113 var Protocol = {}; |
105 /** @typedef {string}*/ | 114 /** @typedef {string}*/ |
106 Protocol.Error; | 115 Protocol.Error; |
107 """) | 116 """) |
108 | 117 |
109 for domain in json_api: | 118 for domain in domains: |
110 domain_name = domain["domain"] | 119 domain_name = domain["domain"] |
111 if "types" in domain: | 120 if "types" in domain: |
112 for type in domain["types"]: | 121 for type in domain["types"]: |
113 type_id = full_qualified_type_id(domain_name, type["id"]) | 122 type_id = full_qualified_type_id(domain_name, type["id"]) |
114 ref_types[type_id] = "%sAgent.%s" % (domain_name, type["id"]) | 123 ref_types[type_id] = "%sAgent.%s" % (domain_name, type["id"]) |
115 | 124 |
116 for domain in json_api: | 125 for domain in domains: |
117 domain_name = domain["domain"] | 126 domain_name = domain["domain"] |
118 promisified = domain_name in promisified_domains | 127 promisified = domain_name in promisified_domains |
119 | 128 |
120 output_file.write("\n\n/**\n * @constructor\n*/\n") | 129 output_file.write("\n\n/**\n * @constructor\n*/\n") |
121 output_file.write("Protocol.%sAgent = function(){};\n" % domain_name) | 130 output_file.write("Protocol.%sAgent = function(){};\n" % domain_name) |
122 | 131 |
123 if "commands" in domain: | 132 if "commands" in domain: |
124 for command in domain["commands"]: | 133 for command in domain["commands"]: |
125 output_file.write("\n/**\n") | 134 output_file.write("\n/**\n") |
126 params = [] | 135 params = [] |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 else: | 212 else: |
204 params.append(param["name"]) | 213 params.append(param["name"]) |
205 output_file.write(" * @param {%s} %s\n" % (param_typ
e(domain_name, param), param["name"])) | 214 output_file.write(" * @param {%s} %s\n" % (param_typ
e(domain_name, param), param["name"])) |
206 output_file.write(" */\n") | 215 output_file.write(" */\n") |
207 output_file.write("%sAgent.Dispatcher.prototype.%s = function(%s
) {};\n" % (domain_name, event["name"], ", ".join(params))) | 216 output_file.write("%sAgent.Dispatcher.prototype.%s = function(%s
) {};\n" % (domain_name, event["name"], ", ".join(params))) |
208 | 217 |
209 output_file.write("\n/** @constructor\n * @param {!Object.<string, !Object>}
agentsMap\n */\n") | 218 output_file.write("\n/** @constructor\n * @param {!Object.<string, !Object>}
agentsMap\n */\n") |
210 output_file.write("Protocol.Agents = function(agentsMap){this._agentsMap;};\
n") | 219 output_file.write("Protocol.Agents = function(agentsMap){this._agentsMap;};\
n") |
211 output_file.write("/**\n * @param {string} domain\n * @param {!Object} dispa
tcher\n */\n") | 220 output_file.write("/**\n * @param {string} domain\n * @param {!Object} dispa
tcher\n */\n") |
212 output_file.write("Protocol.Agents.prototype.registerDispatcher = function(d
omain, dispatcher){};\n") | 221 output_file.write("Protocol.Agents.prototype.registerDispatcher = function(d
omain, dispatcher){};\n") |
213 for domain in json_api: | 222 for domain in domains: |
214 domain_name = domain["domain"] | 223 domain_name = domain["domain"] |
215 uppercase_length = 0 | 224 uppercase_length = 0 |
216 while uppercase_length < len(domain_name) and domain_name[uppercase_leng
th].isupper(): | 225 while uppercase_length < len(domain_name) and domain_name[uppercase_leng
th].isupper(): |
217 uppercase_length += 1 | 226 uppercase_length += 1 |
218 | 227 |
219 output_file.write("/** @return {!Protocol.%sAgent}*/\n" % domain_name) | 228 output_file.write("/** @return {!Protocol.%sAgent}*/\n" % domain_name) |
220 output_file.write("Protocol.Agents.prototype.%s = function(){};\n" % (do
main_name[:uppercase_length].lower() + domain_name[uppercase_length:] + "Agent")
) | 229 output_file.write("Protocol.Agents.prototype.%s = function(){};\n" % (do
main_name[:uppercase_length].lower() + domain_name[uppercase_length:] + "Agent")
) |
221 | 230 |
222 output_file.write("/**\n * @param {!%sAgent.Dispatcher} dispatcher\n */\
n" % domain_name) | 231 output_file.write("/**\n * @param {!%sAgent.Dispatcher} dispatcher\n */\
n" % domain_name) |
223 output_file.write("Protocol.Agents.prototype.register%sDispatcher = func
tion(dispatcher) {}\n" % domain_name) | 232 output_file.write("Protocol.Agents.prototype.register%sDispatcher = func
tion(dispatcher) {}\n" % domain_name) |
224 | 233 |
225 | 234 |
226 output_file.close() | 235 output_file.close() |
227 | 236 |
228 if __name__ == "__main__": | 237 if __name__ == "__main__": |
229 import sys | 238 import sys |
230 import os.path | 239 import os.path |
231 program_name = os.path.basename(__file__) | 240 program_name = os.path.basename(__file__) |
232 if len(sys.argv) < 4 or sys.argv[1] != "-o": | 241 if len(sys.argv) < 4 or sys.argv[1] != "-o": |
233 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name) | 242 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name) |
234 exit(1) | 243 exit(1) |
235 output_path = sys.argv[2] | 244 output_path = sys.argv[2] |
236 input_path = sys.argv[3] | 245 input_path = sys.argv[3] |
237 generate_protocol_externs(output_path, input_path) | 246 generate_protocol_externs(output_path, input_path) |
OLD | NEW |