| 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_domain(file, domains): |
| 92 input_file = open(input_path, "r") | 97 if not os.path.isfile(file): |
| 98 return |
| 99 input_file = open(file, "r") |
| 93 json_string = input_file.read() | 100 json_string = input_file.read() |
| 94 json_string = json_string.replace(": true", ": True") | 101 parsed_json = json.loads(json_string) |
| 95 json_string = json_string.replace(": false", ": False") | 102 domains.append(parsed_json) |
| 96 json_api = eval(json_string)["domains"] | |
| 97 | 103 |
| 104 |
| 105 def load_domains(folder, domains): |
| 106 for f in os.listdir(folder): |
| 107 filename = os.path.join(folder, f) |
| 108 if filename.endswith(".json") and os.path.isfile(filename): |
| 109 load_domain(filename, domains) |
| 110 |
| 111 |
| 112 def generate_protocol_externs(output_path, folder1, folder2): |
| 113 domains = [] |
| 114 load_domains(folder1, domains) |
| 115 load_domains(folder2, domains) |
| 98 output_file = open(output_path, "w") | 116 output_file = open(output_path, "w") |
| 99 | 117 |
| 100 output_file.write( | 118 output_file.write( |
| 101 """ | 119 """ |
| 102 var InspectorBackend = {} | 120 var InspectorBackend = {} |
| 103 | 121 |
| 104 var Protocol = {}; | 122 var Protocol = {}; |
| 105 /** @typedef {string}*/ | 123 /** @typedef {string}*/ |
| 106 Protocol.Error; | 124 Protocol.Error; |
| 107 """) | 125 """) |
| 108 | 126 |
| 109 for domain in json_api: | 127 for domain in domains: |
| 110 domain_name = domain["domain"] | 128 domain_name = domain["domain"] |
| 111 if "types" in domain: | 129 if "types" in domain: |
| 112 for type in domain["types"]: | 130 for type in domain["types"]: |
| 113 type_id = full_qualified_type_id(domain_name, type["id"]) | 131 type_id = full_qualified_type_id(domain_name, type["id"]) |
| 114 ref_types[type_id] = "%sAgent.%s" % (domain_name, type["id"]) | 132 ref_types[type_id] = "%sAgent.%s" % (domain_name, type["id"]) |
| 115 | 133 |
| 116 for domain in json_api: | 134 for domain in domains: |
| 117 domain_name = domain["domain"] | 135 domain_name = domain["domain"] |
| 118 promisified = domain_name in promisified_domains | 136 promisified = domain_name in promisified_domains |
| 119 | 137 |
| 120 output_file.write("\n\n/**\n * @constructor\n*/\n") | 138 output_file.write("\n\n/**\n * @constructor\n*/\n") |
| 121 output_file.write("Protocol.%sAgent = function(){};\n" % domain_name) | 139 output_file.write("Protocol.%sAgent = function(){};\n" % domain_name) |
| 122 | 140 |
| 123 if "commands" in domain: | 141 if "commands" in domain: |
| 124 for command in domain["commands"]: | 142 for command in domain["commands"]: |
| 125 output_file.write("\n/**\n") | 143 output_file.write("\n/**\n") |
| 126 params = [] | 144 params = [] |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 else: | 221 else: |
| 204 params.append(param["name"]) | 222 params.append(param["name"]) |
| 205 output_file.write(" * @param {%s} %s\n" % (param_typ
e(domain_name, param), param["name"])) | 223 output_file.write(" * @param {%s} %s\n" % (param_typ
e(domain_name, param), param["name"])) |
| 206 output_file.write(" */\n") | 224 output_file.write(" */\n") |
| 207 output_file.write("%sAgent.Dispatcher.prototype.%s = function(%s
) {};\n" % (domain_name, event["name"], ", ".join(params))) | 225 output_file.write("%sAgent.Dispatcher.prototype.%s = function(%s
) {};\n" % (domain_name, event["name"], ", ".join(params))) |
| 208 | 226 |
| 209 output_file.write("\n/** @constructor\n * @param {!Object.<string, !Object>}
agentsMap\n */\n") | 227 output_file.write("\n/** @constructor\n * @param {!Object.<string, !Object>}
agentsMap\n */\n") |
| 210 output_file.write("Protocol.Agents = function(agentsMap){this._agentsMap;};\
n") | 228 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") | 229 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") | 230 output_file.write("Protocol.Agents.prototype.registerDispatcher = function(d
omain, dispatcher){};\n") |
| 213 for domain in json_api: | 231 for domain in domains: |
| 214 domain_name = domain["domain"] | 232 domain_name = domain["domain"] |
| 215 uppercase_length = 0 | 233 uppercase_length = 0 |
| 216 while uppercase_length < len(domain_name) and domain_name[uppercase_leng
th].isupper(): | 234 while uppercase_length < len(domain_name) and domain_name[uppercase_leng
th].isupper(): |
| 217 uppercase_length += 1 | 235 uppercase_length += 1 |
| 218 | 236 |
| 219 output_file.write("/** @return {!Protocol.%sAgent}*/\n" % domain_name) | 237 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")
) | 238 output_file.write("Protocol.Agents.prototype.%s = function(){};\n" % (do
main_name[:uppercase_length].lower() + domain_name[uppercase_length:] + "Agent")
) |
| 221 | 239 |
| 222 output_file.write("/**\n * @param {!%sAgent.Dispatcher} dispatcher\n */\
n" % domain_name) | 240 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) | 241 output_file.write("Protocol.Agents.prototype.register%sDispatcher = func
tion(dispatcher) {}\n" % domain_name) |
| 224 | 242 |
| 225 | 243 |
| 226 output_file.close() | 244 output_file.close() |
| 227 | 245 |
| 228 if __name__ == "__main__": | 246 if __name__ == "__main__": |
| 229 import sys | 247 import sys |
| 230 import os.path | 248 import os.path |
| 231 program_name = os.path.basename(__file__) | 249 program_name = os.path.basename(__file__) |
| 232 if len(sys.argv) < 4 or sys.argv[1] != "-o": | 250 if len(sys.argv) < 4 or sys.argv[1] != "-o": |
| 233 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name) | 251 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name) |
| 234 exit(1) | 252 exit(1) |
| 235 output_path = sys.argv[2] | 253 output_path = sys.argv[2] |
| 236 input_path = sys.argv[3] | 254 input_path = sys.argv[3] |
| 237 generate_protocol_externs(output_path, input_path) | 255 generate_protocol_externs(output_path, input_path) |
| OLD | NEW |