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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 | 101 |
102 for domain in json_api: | 102 for domain in json_api: |
103 domain_name = domain["domain"] | 103 domain_name = domain["domain"] |
104 if "types" in domain: | 104 if "types" in domain: |
105 for type in domain["types"]: | 105 for type in domain["types"]: |
106 type_id = full_qualified_type_id(domain_name, type["id"]) | 106 type_id = full_qualified_type_id(domain_name, type["id"]) |
107 ref_types[type_id] = "%sAgent.%s" % (domain_name, type["id"]) | 107 ref_types[type_id] = "%sAgent.%s" % (domain_name, type["id"]) |
108 | 108 |
109 for domain in json_api: | 109 for domain in json_api: |
110 domain_name = domain["domain"] | 110 domain_name = domain["domain"] |
111 output_file.write("\n\n\nvar %sAgent = {};\n" % domain_name) | 111 |
vsevik
2014/03/06 14:49:45
Why did this move?
sergeyv
2014/03/06 16:18:34
I've done this move, cause now we need to create A
| |
112 if "types" in domain: | 112 output_file.write("\n\n/**\n * @constructor\n*/\n") |
113 for type in domain["types"]: | 113 output_file.write("Protocol.%sAgent = function(){};\n" % domain_name) |
114 if type["type"] == "object": | |
115 typedef_args = [] | |
116 if "properties" in type: | |
117 for property in type["properties"]: | |
118 suffix = "" | |
119 if ("optional" in property): | |
120 suffix = "|undefined" | |
121 if "enum" in property: | |
122 enum_name = "%sAgent.%s%s" % (domain_name, type[ "id"], to_title_case(property["name"])) | |
123 output_file.write(generate_enum(enum_name, prope rty)) | |
124 typedef_args.append("%s:(%s%s)" % (property["nam e"], enum_name, suffix)) | |
125 else: | |
126 typedef_args.append("%s:(%s%s)" % (property["nam e"], param_type(domain_name, property), suffix)) | |
127 if (typedef_args): | |
128 output_file.write("\n/** @typedef {!{%s}} */\n%sAgent.%s ;\n" % (", ".join(typedef_args), domain_name, type["id"])) | |
129 else: | |
130 output_file.write("\n/** @typedef {!Object} */\n%sAgent. %s;\n" % (domain_name, type["id"])) | |
131 elif type["type"] == "string" and "enum" in type: | |
132 output_file.write(generate_enum("%sAgent.%s" % (domain_name, type["id"]), type)) | |
133 elif type["type"] == "array": | |
134 output_file.write("\n/** @typedef {!Array.<!%s>} */\n%sAgent .%s;\n" % (param_type(domain_name, type["items"]), domain_name, type["id"])) | |
135 else: | |
136 output_file.write("\n/** @typedef {%s} */\n%sAgent.%s;\n" % (type_traits[type["type"]], domain_name, type["id"])) | |
137 | 114 |
138 if "commands" in domain: | 115 if "commands" in domain: |
139 for command in domain["commands"]: | 116 for command in domain["commands"]: |
140 output_file.write("\n/**\n") | 117 output_file.write("\n/**\n") |
141 params = [] | 118 params = [] |
142 if ("parameters" in command): | 119 if ("parameters" in command): |
143 for in_param in command["parameters"]: | 120 for in_param in command["parameters"]: |
144 if ("optional" in in_param): | 121 if ("optional" in in_param): |
145 params.append("opt_%s" % in_param["name"]) | 122 params.append("opt_%s" % in_param["name"]) |
146 output_file.write(" * @param {%s=} opt_%s\n" % (para m_type(domain_name, in_param), in_param["name"])) | 123 output_file.write(" * @param {%s=} opt_%s\n" % (para m_type(domain_name, in_param), in_param["name"])) |
147 else: | 124 else: |
148 params.append(in_param["name"]) | 125 params.append(in_param["name"]) |
149 output_file.write(" * @param {%s} %s\n" % (param_typ e(domain_name, in_param), in_param["name"])) | 126 output_file.write(" * @param {%s} %s\n" % (param_typ e(domain_name, in_param), in_param["name"])) |
150 returns = ["?Protocol.Error"] | 127 returns = ["?Protocol.Error"] |
151 if ("error" in command): | 128 if ("error" in command): |
152 returns.append("%s=" % param_type(domain_name, command["erro r"])) | 129 returns.append("%s=" % param_type(domain_name, command["erro r"])) |
153 if ("returns" in command): | 130 if ("returns" in command): |
154 for out_param in command["returns"]: | 131 for out_param in command["returns"]: |
155 if ("optional" in out_param): | 132 if ("optional" in out_param): |
156 returns.append("%s=" % param_type(domain_name, out_p aram)) | 133 returns.append("%s=" % param_type(domain_name, out_p aram)) |
157 else: | 134 else: |
158 returns.append("%s" % param_type(domain_name, out_pa ram)) | 135 returns.append("%s" % param_type(domain_name, out_pa ram)) |
159 output_file.write(" * @param {function(%s):void=} opt_callback\n " % ", ".join(returns)) | 136 output_file.write(" * @param {function(%s):void=} opt_callback\n " % ", ".join(returns)) |
160 output_file.write(" */\n") | 137 output_file.write(" */\n") |
161 params.append("opt_callback") | 138 params.append("opt_callback") |
162 output_file.write("%sAgent.%s = function(%s) {}\n" % (domain_nam e, command["name"], ", ".join(params))) | 139 output_file.write("Protocol.%sAgent.prototype.%s = function(%s) {}\n" % (domain_name, command["name"], ", ".join(params))) |
163 output_file.write("/** @param {function(%s):void=} opt_callback */\n" % ", ".join(returns)) | 140 output_file.write("/** @param {function(%s):void=} opt_callback */\n" % ", ".join(returns)) |
164 output_file.write("%sAgent.invoke_%s = function(obj, opt_callbac k) {}\n" % (domain_name, command["name"])) | 141 output_file.write("Protocol.%sAgent.prototype.invoke_%s = functi on(obj, opt_callback) {}\n" % (domain_name, command["name"])) |
142 | |
143 output_file.write("\n\n\nvar %sAgent = new Protocol.%sAgent();\n" % (dom ain_name, domain_name)) | |
144 | |
145 if "types" in domain: | |
146 for type in domain["types"]: | |
147 if type["type"] == "object": | |
148 typedef_args = [] | |
149 if "properties" in type: | |
150 for property in type["properties"]: | |
151 suffix = "" | |
152 if ("optional" in property): | |
153 suffix = "|undefined" | |
154 if "enum" in property: | |
155 enum_name = "%sAgent.%s%s" % (domain_name, type[ "id"], to_title_case(property["name"])) | |
156 output_file.write(generate_enum(enum_name, prope rty)) | |
157 typedef_args.append("%s:(%s%s)" % (property["nam e"], enum_name, suffix)) | |
158 else: | |
159 typedef_args.append("%s:(%s%s)" % (property["nam e"], param_type(domain_name, property), suffix)) | |
160 if (typedef_args): | |
161 output_file.write("\n/** @typedef {!{%s}} */\n%sAgent.%s ;\n" % (", ".join(typedef_args), domain_name, type["id"])) | |
162 else: | |
163 output_file.write("\n/** @typedef {!Object} */\n%sAgent. %s;\n" % (domain_name, type["id"])) | |
164 elif type["type"] == "string" and "enum" in type: | |
165 output_file.write(generate_enum("%sAgent.%s" % (domain_name, type["id"]), type)) | |
166 elif type["type"] == "array": | |
167 output_file.write("\n/** @typedef {!Array.<!%s>} */\n%sAgent .%s;\n" % (param_type(domain_name, type["items"]), domain_name, type["id"])) | |
168 else: | |
169 output_file.write("\n/** @typedef {%s} */\n%sAgent.%s;\n" % (type_traits[type["type"]], domain_name, type["id"])) | |
165 | 170 |
166 output_file.write("/** @interface */\n") | 171 output_file.write("/** @interface */\n") |
167 output_file.write("%sAgent.Dispatcher = function() {};\n" % domain_name) | 172 output_file.write("%sAgent.Dispatcher = function() {};\n" % domain_name) |
168 if "events" in domain: | 173 if "events" in domain: |
169 for event in domain["events"]: | 174 for event in domain["events"]: |
170 params = [] | 175 params = [] |
171 if ("parameters" in event): | 176 if ("parameters" in event): |
172 output_file.write("/**\n") | 177 output_file.write("/**\n") |
173 for param in event["parameters"]: | 178 for param in event["parameters"]: |
174 if ("optional" in param): | 179 if ("optional" in param): |
175 params.append("opt_%s" % param["name"]) | 180 params.append("opt_%s" % param["name"]) |
176 output_file.write(" * @param {%s=} opt_%s\n" % (para m_type(domain_name, param), param["name"])) | 181 output_file.write(" * @param {%s=} opt_%s\n" % (para m_type(domain_name, param), param["name"])) |
177 else: | 182 else: |
178 params.append(param["name"]) | 183 params.append(param["name"]) |
179 output_file.write(" * @param {%s} %s\n" % (param_typ e(domain_name, param), param["name"])) | 184 output_file.write(" * @param {%s} %s\n" % (param_typ e(domain_name, param), param["name"])) |
180 output_file.write(" */\n") | 185 output_file.write(" */\n") |
181 output_file.write("%sAgent.Dispatcher.prototype.%s = function(%s ) {};\n" % (domain_name, event["name"], ", ".join(params))) | 186 output_file.write("%sAgent.Dispatcher.prototype.%s = function(%s ) {};\n" % (domain_name, event["name"], ", ".join(params))) |
182 output_file.write("/**\n * @param {%sAgent.Dispatcher} dispatcher\n */\n " % domain_name) | 187 output_file.write("/**\n * @param {%sAgent.Dispatcher} dispatcher\n */\n " % domain_name) |
183 output_file.write("InspectorBackend.register%sDispatcher = function(disp atcher) {}\n" % domain_name) | 188 output_file.write("InspectorBackend.register%sDispatcher = function(disp atcher) {}\n" % domain_name) |
189 | |
190 output_file.write("\n/** @constructor */\n") | |
191 output_file.write("Protocol.Agents = function(){};\n") | |
192 | |
193 for domain in json_api: | |
194 domain_name = domain["domain"] | |
195 uppercase_length = 0 | |
196 while uppercase_length < len(domain_name) and domain_name[uppercase_leng th].isupper(): | |
197 uppercase_length += 1 | |
198 | |
199 output_file.write("/** @return {!Protocol.%sAgent}*/\n" % domain_name) | |
200 output_file.write("Protocol.Agents.prototype.%s = function(){};\n" % (do main_name[:uppercase_length].lower() + domain_name[uppercase_length:] + "Agent") ) | |
201 | |
184 output_file.close() | 202 output_file.close() |
185 | 203 |
186 if __name__ == "__main__": | 204 if __name__ == "__main__": |
187 import sys | 205 import sys |
188 import os.path | 206 import os.path |
189 program_name = os.path.basename(__file__) | 207 program_name = os.path.basename(__file__) |
190 if len(sys.argv) < 4 or sys.argv[1] != "-o": | 208 if len(sys.argv) < 4 or sys.argv[1] != "-o": |
191 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name) | 209 sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name) |
192 exit(1) | 210 exit(1) |
193 output_path = sys.argv[2] | 211 output_path = sys.argv[2] |
194 input_path = sys.argv[3] | 212 input_path = sys.argv[3] |
195 generate_protocol_externs(output_path, input_path) | 213 generate_protocol_externs(output_path, input_path) |
OLD | NEW |