| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2011 Google Inc. All rights reserved. |
| 3 # Copyright (c) 2012 Intel Corporation. All rights reserved. |
| 4 # |
| 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are |
| 7 # met: |
| 8 # |
| 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. |
| 11 # * Redistributions in binary form must reproduce the above |
| 12 # copyright notice, this list of conditions and the following disclaimer |
| 13 # in the documentation and/or other materials provided with the |
| 14 # distribution. |
| 15 # * Neither the name of Google Inc. nor the names of its |
| 16 # contributors may be used to endorse or promote products derived from |
| 17 # this software without specific prior written permission. |
| 18 # |
| 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
| 31 import os.path |
| 32 import sys |
| 33 import string |
| 34 import optparse |
| 35 import re |
| 36 try: |
| 37 import json |
| 38 except ImportError: |
| 39 import simplejson as json |
| 40 |
| 41 cmdline_parser = optparse.OptionParser() |
| 42 cmdline_parser.add_option("--output_js_dir") |
| 43 |
| 44 try: |
| 45 arg_options, arg_values = cmdline_parser.parse_args() |
| 46 if (len(arg_values) != 1): |
| 47 raise Exception("Exactly one plain argument expected (found %s)" % len(a
rg_values)) |
| 48 input_json_filename = arg_values[0] |
| 49 output_js_dirname = arg_options.output_js_dir |
| 50 if not output_js_dirname: |
| 51 raise Exception("Output .js directory must be specified") |
| 52 except Exception: |
| 53 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html |
| 54 exc = sys.exc_info()[1] |
| 55 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc) |
| 56 sys.stderr.write("Usage: <script> some.json --output_js_dir <output_js_dir>\
n") |
| 57 exit(1) |
| 58 |
| 59 |
| 60 def fix_camel_case(name): |
| 61 prefix = "" |
| 62 if name[0] == "-": |
| 63 prefix = "Negative" |
| 64 name = name[1:] |
| 65 refined = re.sub(r'-(\w)', lambda pat: pat.group(1).upper(), name) |
| 66 refined = to_title_case(refined) |
| 67 return prefix + re.sub(r'(?i)HTML|XML|WML|API', lambda pat: pat.group(0).upp
er(), refined) |
| 68 |
| 69 |
| 70 def to_title_case(name): |
| 71 return name[:1].upper() + name[1:] |
| 72 |
| 73 |
| 74 class RawTypes(object): |
| 75 @staticmethod |
| 76 def get_js(json_type): |
| 77 if json_type == "boolean": |
| 78 return "boolean" |
| 79 elif json_type == "string": |
| 80 return "string" |
| 81 elif json_type == "array": |
| 82 return "object" |
| 83 elif json_type == "object": |
| 84 return "object" |
| 85 elif json_type == "integer": |
| 86 return "number" |
| 87 elif json_type == "number": |
| 88 return "number" |
| 89 elif json_type == "any": |
| 90 raise Exception("Unsupported") |
| 91 else: |
| 92 raise Exception("Unknown type: %s" % json_type) |
| 93 |
| 94 |
| 95 class TypeData(object): |
| 96 def __init__(self, json_type): |
| 97 if "type" not in json_type: |
| 98 raise Exception("Unknown type") |
| 99 json_type_name = json_type["type"] |
| 100 self.raw_type_js_ = RawTypes.get_js(json_type_name) |
| 101 |
| 102 def get_raw_type_js(self): |
| 103 return self.raw_type_js_ |
| 104 |
| 105 |
| 106 class TypeMap: |
| 107 def __init__(self, api): |
| 108 self.map_ = {} |
| 109 for json_domain in api["domains"]: |
| 110 domain_name = json_domain["domain"] |
| 111 |
| 112 domain_map = {} |
| 113 self.map_[domain_name] = domain_map |
| 114 |
| 115 if "types" in json_domain: |
| 116 for json_type in json_domain["types"]: |
| 117 type_name = json_type["id"] |
| 118 type_data = TypeData(json_type) |
| 119 domain_map[type_name] = type_data |
| 120 |
| 121 def get(self, domain_name, type_name): |
| 122 return self.map_[domain_name][type_name] |
| 123 |
| 124 |
| 125 def resolve_param_raw_type_js(json_parameter, scope_domain_name): |
| 126 if "$ref" in json_parameter: |
| 127 json_ref = json_parameter["$ref"] |
| 128 return get_ref_data_js(json_ref, scope_domain_name) |
| 129 elif "type" in json_parameter: |
| 130 json_type = json_parameter["type"] |
| 131 return RawTypes.get_js(json_type) |
| 132 else: |
| 133 raise Exception("Unknown type") |
| 134 |
| 135 |
| 136 def get_ref_data_js(json_ref, scope_domain_name): |
| 137 dot_pos = json_ref.find(".") |
| 138 if dot_pos == -1: |
| 139 domain_name = scope_domain_name |
| 140 type_name = json_ref |
| 141 else: |
| 142 domain_name = json_ref[:dot_pos] |
| 143 type_name = json_ref[dot_pos + 1:] |
| 144 |
| 145 return type_map.get(domain_name, type_name).get_raw_type_js() |
| 146 |
| 147 |
| 148 input_file = open(input_json_filename, "r") |
| 149 json_string = input_file.read() |
| 150 json_api = json.loads(json_string) |
| 151 |
| 152 |
| 153 class Templates: |
| 154 def get_this_script_path_(absolute_path): |
| 155 absolute_path = os.path.abspath(absolute_path) |
| 156 components = [] |
| 157 |
| 158 def fill_recursive(path_part, depth): |
| 159 if depth <= 0 or path_part == '/': |
| 160 return |
| 161 fill_recursive(os.path.dirname(path_part), depth - 1) |
| 162 components.append(os.path.basename(path_part)) |
| 163 |
| 164 # Typical path is /Source/platform/inspector_protocol/CodeGenerator.py |
| 165 # Let's take 4 components from the real path then. |
| 166 fill_recursive(absolute_path, 4) |
| 167 |
| 168 return "/".join(components) |
| 169 |
| 170 file_header_ = ("// File is generated by %s\n\n" % get_this_script_path_(sys
.argv[0]) + |
| 171 """// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 172 // Use of this source code is governed by a BSD-style license that can be |
| 173 // found in the LICENSE file. |
| 174 """) |
| 175 |
| 176 backend_js = string.Template(file_header_ + """ |
| 177 |
| 178 $domainInitializers |
| 179 """) |
| 180 |
| 181 |
| 182 type_map = TypeMap(json_api) |
| 183 |
| 184 |
| 185 class Generator: |
| 186 backend_js_domain_initializer_list = [] |
| 187 |
| 188 @staticmethod |
| 189 def go(): |
| 190 for json_domain in json_api["domains"]: |
| 191 domain_name = json_domain["domain"] |
| 192 domain_name_lower = domain_name.lower() |
| 193 if domain_name_lower == "console": |
| 194 continue |
| 195 |
| 196 Generator.backend_js_domain_initializer_list.append("// %s.\n" % dom
ain_name) |
| 197 |
| 198 if "types" in json_domain: |
| 199 for json_type in json_domain["types"]: |
| 200 if "type" in json_type and json_type["type"] == "string" and
"enum" in json_type: |
| 201 enum_name = "%s.%s" % (domain_name, json_type["id"]) |
| 202 Generator.process_enum(json_type, enum_name) |
| 203 elif json_type["type"] == "object": |
| 204 if "properties" in json_type: |
| 205 for json_property in json_type["properties"]: |
| 206 if "type" in json_property and json_property["ty
pe"] == "string" and "enum" in json_property: |
| 207 enum_name = "%s.%s%s" % (domain_name, json_t
ype["id"], to_title_case(json_property["name"])) |
| 208 Generator.process_enum(json_property, enum_n
ame) |
| 209 |
| 210 if "events" in json_domain: |
| 211 for json_event in json_domain["events"]: |
| 212 Generator.process_event(json_event, domain_name) |
| 213 |
| 214 if "commands" in json_domain: |
| 215 for json_command in json_domain["commands"]: |
| 216 Generator.process_command(json_command, domain_name) |
| 217 |
| 218 Generator.backend_js_domain_initializer_list.append("\n") |
| 219 |
| 220 @staticmethod |
| 221 def process_enum(json_enum, enum_name): |
| 222 enum_members = [] |
| 223 for member in json_enum["enum"]: |
| 224 enum_members.append("%s: \"%s\"" % (fix_camel_case(member), member)) |
| 225 |
| 226 Generator.backend_js_domain_initializer_list.append("InspectorBackend.re
gisterEnum(\"%s\", {%s});\n" % ( |
| 227 enum_name, ", ".join(enum_members))) |
| 228 |
| 229 @staticmethod |
| 230 def process_event(json_event, domain_name): |
| 231 event_name = json_event["name"] |
| 232 |
| 233 json_parameters = json_event.get("parameters") |
| 234 |
| 235 backend_js_event_param_list = [] |
| 236 if json_parameters: |
| 237 for parameter in json_parameters: |
| 238 parameter_name = parameter["name"] |
| 239 backend_js_event_param_list.append("\"%s\"" % parameter_name) |
| 240 |
| 241 Generator.backend_js_domain_initializer_list.append("InspectorBackend.re
gisterEvent(\"%s.%s\", [%s]);\n" % ( |
| 242 domain_name, event_name, ", ".join(backend_js_event_param_list))) |
| 243 |
| 244 @staticmethod |
| 245 def process_command(json_command, domain_name): |
| 246 json_command_name = json_command["name"] |
| 247 |
| 248 js_parameters_text = "" |
| 249 if "parameters" in json_command: |
| 250 json_params = json_command["parameters"] |
| 251 js_param_list = [] |
| 252 |
| 253 for json_parameter in json_params: |
| 254 json_param_name = json_parameter["name"] |
| 255 js_bind_type = resolve_param_raw_type_js(json_parameter, domain_
name) |
| 256 |
| 257 optional = json_parameter.get("optional") |
| 258 |
| 259 |
| 260 js_param_text = "{\"name\": \"%s\", \"type\": \"%s\", \"optional
\": %s}" % ( |
| 261 json_param_name, |
| 262 js_bind_type, |
| 263 ("true" if ("optional" in json_parameter and json_parameter[
"optional"]) else "false")) |
| 264 |
| 265 js_param_list.append(js_param_text) |
| 266 |
| 267 js_parameters_text = ", ".join(js_param_list) |
| 268 |
| 269 |
| 270 backend_js_reply_param_list = [] |
| 271 if "returns" in json_command: |
| 272 for json_return in json_command["returns"]: |
| 273 json_return_name = json_return["name"] |
| 274 backend_js_reply_param_list.append("\"%s\"" % json_return_name) |
| 275 |
| 276 js_reply_list = "[%s]" % ", ".join(backend_js_reply_param_list) |
| 277 if "error" in json_command: |
| 278 has_error_data_param = "true" |
| 279 else: |
| 280 has_error_data_param = "false" |
| 281 |
| 282 Generator.backend_js_domain_initializer_list.append("InspectorBackend.re
gisterCommand(\"%s.%s\", [%s], %s, %s);\n" % (domain_name, json_command_name, js
_parameters_text, js_reply_list, has_error_data_param)) |
| 283 |
| 284 Generator.go() |
| 285 |
| 286 backend_js_file = open(output_js_dirname + "/InspectorBackendCommands.js", "w") |
| 287 |
| 288 backend_js_file.write(Templates.backend_js.substitute(None, |
| 289 domainInitializers="".join(Generator.backend_js_domain_initializer_list))) |
| 290 |
| 291 backend_js_file.close() |
| OLD | NEW |