| 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 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 "string": "string", | 39 "string": "string", |
| 40 "integer": "number", | 40 "integer": "number", |
| 41 "number": "number", | 41 "number": "number", |
| 42 "boolean": "boolean", | 42 "boolean": "boolean", |
| 43 "array": "!Array<*>", | 43 "array": "!Array<*>", |
| 44 "object": "!Object", | 44 "object": "!Object", |
| 45 } | 45 } |
| 46 | 46 |
| 47 ref_types = {} | 47 ref_types = {} |
| 48 | 48 |
| 49 PROMISIFIED_DOMAINS = frozenset([ |
| 50 "HeapProfiler", |
| 51 "Profiler", |
| 52 ]) |
| 53 |
| 49 | 54 |
| 50 def full_qualified_type_id(domain_name, type_id): | 55 def full_qualified_type_id(domain_name, type_id): |
| 51 if type_id.find(".") == -1: | 56 if type_id.find(".") == -1: |
| 52 return "%s.%s" % (domain_name, type_id) | 57 return "%s.%s" % (domain_name, type_id) |
| 53 return type_id | 58 return type_id |
| 54 | 59 |
| 55 | 60 |
| 56 def fix_camel_case(name): | 61 def fix_camel_case(name): |
| 57 prefix = "" | 62 prefix = "" |
| 58 if name[0] == "-": | 63 if name[0] == "-": |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 115 |
| 111 for domain in domains: | 116 for domain in domains: |
| 112 domain_name = domain["domain"] | 117 domain_name = domain["domain"] |
| 113 if "types" in domain: | 118 if "types" in domain: |
| 114 for type in domain["types"]: | 119 for type in domain["types"]: |
| 115 type_id = full_qualified_type_id(domain_name, type["id"]) | 120 type_id = full_qualified_type_id(domain_name, type["id"]) |
| 116 ref_types[type_id] = "Protocol.%s.%s" % (domain_name, type["id"]
) | 121 ref_types[type_id] = "Protocol.%s.%s" % (domain_name, type["id"]
) |
| 117 | 122 |
| 118 for domain in domains: | 123 for domain in domains: |
| 119 domain_name = domain["domain"] | 124 domain_name = domain["domain"] |
| 125 is_promisified = domain_name in PROMISIFIED_DOMAINS |
| 120 | 126 |
| 121 output_file.write("Protocol.%s = {};\n" % domain_name) | 127 output_file.write("Protocol.%s = {};\n" % domain_name) |
| 122 output_file.write("\n\n/**\n * @constructor\n*/\n") | 128 output_file.write("\n\n/**\n * @constructor\n*/\n") |
| 123 output_file.write("Protocol.%sAgent = function(){};\n" % domain_name) | 129 output_file.write("Protocol.%sAgent = function(){};\n" % domain_name) |
| 124 | 130 |
| 125 if "commands" in domain: | 131 if "commands" in domain: |
| 126 for command in domain["commands"]: | 132 for command in domain["commands"]: |
| 127 output_file.write("\n/**\n") | 133 output_file.write("\n/**\n") |
| 128 params = [] | 134 params = [] |
| 129 in_param_to_type = {} | 135 in_param_to_type = {} |
| (...skipping 15 matching lines...) Expand all Loading... |
| 145 if ("error" in command): | 151 if ("error" in command): |
| 146 returns.append("%s=" % param_type(domain_name, command["erro
r"])) | 152 returns.append("%s=" % param_type(domain_name, command["erro
r"])) |
| 147 if (has_return_value): | 153 if (has_return_value): |
| 148 for out_param in command["returns"]: | 154 for out_param in command["returns"]: |
| 149 out_param_type = param_type(domain_name, out_param) | 155 out_param_type = param_type(domain_name, out_param) |
| 150 out_param_to_type[out_param["name"]] = out_param_type | 156 out_param_to_type[out_param["name"]] = out_param_type |
| 151 if ("optional" in out_param): | 157 if ("optional" in out_param): |
| 152 returns.append("%s=" % out_param_type) | 158 returns.append("%s=" % out_param_type) |
| 153 else: | 159 else: |
| 154 returns.append("%s" % out_param_type) | 160 returns.append("%s" % out_param_type) |
| 155 output_file.write(" * @param {function(%s):T=} opt_callback\n" %
", ".join(returns)) | 161 |
| 156 output_file.write(" * @return {!Promise<T>}\n") | 162 if is_promisified: |
| 157 output_file.write(" * @template T\n") | 163 if has_return_value and len(command["returns"]) > 0: |
| 158 params.append("opt_callback") | 164 out_param_type = param_type(domain_name, command["return
s"][0]) |
| 165 if re.match(r"^[!?]", out_param_type[:1]): |
| 166 out_param_type = out_param_type[1:] |
| 167 out_param_type = "?%s" % out_param_type |
| 168 else: |
| 169 out_param_type = "undefined" |
| 170 output_file.write(" * @return {!Promise<%s>}\n" % out_param_
type) |
| 171 else: |
| 172 output_file.write(" * @param {function(%s):T=} opt_callback\
n" % ", ".join(returns)) |
| 173 output_file.write(" * @return {!Promise<T>}\n") |
| 174 output_file.write(" * @template T\n") |
| 175 params.append("opt_callback") |
| 159 | 176 |
| 160 output_file.write(" */\n") | 177 output_file.write(" */\n") |
| 161 output_file.write("Protocol.%sAgent.prototype.%s = function(%s)
{};\n" % | 178 output_file.write("Protocol.%sAgent.prototype.%s = function(%s)
{};\n" % |
| 162 (domain_name, command["name"], ", ".join(param
s))) | 179 (domain_name, command["name"], ", ".join(param
s))) |
| 163 | 180 |
| 164 request_object_properties = [] | 181 request_object_properties = [] |
| 182 request_type = "Protocol.%sAgent.%sRequest" % (domain_name, to_t
itle_case(command["name"])) |
| 165 for param in in_param_to_type: | 183 for param in in_param_to_type: |
| 166 request_object_properties.append("%s: %s" % (param, in_param
_to_type[param])) | 184 request_object_properties.append("%s: %s" % (param, in_param
_to_type[param])) |
| 167 if request_object_properties: | 185 if request_object_properties: |
| 168 output_file.write("/** @typedef {!{%s}} */\n" % (", ".join(r
equest_object_properties))) | 186 output_file.write("/** @typedef {!{%s}} */\n" % (", ".join(r
equest_object_properties))) |
| 169 else: | 187 else: |
| 170 output_file.write("/** @typedef {Object|undefined} */\n") | 188 output_file.write("/** @typedef {Object|undefined} */\n") |
| 171 request = "Protocol.%sAgent.%sRequest" % (domain_name, to_title_
case(command["name"])) | 189 output_file.write("%s;\n" % request_type) |
| 172 output_file.write("%s;\n" % request) | |
| 173 | 190 |
| 174 response_object_properties = [] | 191 response_object_properties = [] |
| 192 response_type = "Protocol.%sAgent.%sResponse" % (domain_name, to
_title_case(command["name"])) |
| 175 for param in out_param_to_type: | 193 for param in out_param_to_type: |
| 176 response_object_properties.append("%s: %s" % (param, out_par
am_to_type[param])) | 194 response_object_properties.append("%s: %s" % (param, out_par
am_to_type[param])) |
| 177 if response_object_properties: | 195 if response_object_properties: |
| 178 output_file.write("/** @typedef {!{%s}} */\n" % (", ".join(r
esponse_object_properties))) | 196 output_file.write("/** @typedef {!{%s}} */\n" % (", ".join(r
esponse_object_properties))) |
| 179 else: | 197 else: |
| 180 output_file.write("/** @typedef {Object|undefined} */\n") | 198 output_file.write("/** @typedef {Object|undefined} */\n") |
| 181 response = "Protocol.%sAgent.%sResponse" % (domain_name, to_titl
e_case(command["name"])) | 199 output_file.write("%s;\n" % response_type) |
| 182 output_file.write("%s;\n" % response) | |
| 183 | 200 |
| 184 output_file.write("/**\n") | 201 output_file.write("/**\n") |
| 185 output_file.write(" * @param {!%s} obj\n" % request) | 202 output_file.write(" * @param {!%s} obj\n" % request_type) |
| 186 output_file.write(" * @return {!Promise<!%s>}" % response) | 203 output_file.write(" * @return {!Promise<!%s>}" % response_type) |
| 187 output_file.write(" */\n") | 204 output_file.write(" */\n") |
| 188 output_file.write("Protocol.%sAgent.prototype.invoke_%s = functi
on(obj) {};\n" % | 205 output_file.write("Protocol.%sAgent.prototype.invoke_%s = functi
on(obj) {};\n" % |
| 189 (domain_name, command["name"])) | 206 (domain_name, command["name"])) |
| 190 | 207 |
| 191 if "types" in domain: | 208 if "types" in domain: |
| 192 for type in domain["types"]: | 209 for type in domain["types"]: |
| 193 if type["type"] == "object": | 210 if type["type"] == "object": |
| 194 typedef_args = [] | 211 typedef_args = [] |
| 195 if "properties" in type: | 212 if "properties" in type: |
| 196 for property in type["properties"]: | 213 for property in type["properties"]: |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 import sys | 272 import sys |
| 256 import os.path | 273 import os.path |
| 257 program_name = os.path.basename(__file__) | 274 program_name = os.path.basename(__file__) |
| 258 if len(sys.argv) < 5 or sys.argv[1] != "-o": | 275 if len(sys.argv) < 5 or sys.argv[1] != "-o": |
| 259 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE_1 INPUT_FILE_2\n"
% program_name) | 276 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE_1 INPUT_FILE_2\n"
% program_name) |
| 260 exit(1) | 277 exit(1) |
| 261 output_path = sys.argv[2] | 278 output_path = sys.argv[2] |
| 262 input_path_1 = sys.argv[3] | 279 input_path_1 = sys.argv[3] |
| 263 input_path_2 = sys.argv[4] | 280 input_path_2 = sys.argv[4] |
| 264 generate_protocol_externs(output_path, input_path_1, input_path_2) | 281 generate_protocol_externs(output_path, input_path_1, input_path_2) |
| OLD | NEW |