Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 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(arg_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).upper(), refined) |
| 68 | |
| 69 | |
| 70 | def to_title_case(name): |
| 71 | return name[:1].upper() + name[1:] |
| 72 | |
| 73 | |
| 74 | class RawTypes(object): |
| 75 | |
| 76 | @staticmethod |
| 77 | def get_js(json_type): |
| 78 | if json_type == "boolean": |
| 79 | return "boolean" |
| 80 | elif json_type == "string": |
| 81 | return "string" |
Johannes Henkel | ff4efae | 2018-10-16 17:55:58 +0000 | [diff] [blame] | 82 | elif json_type == "binary": |
| 83 | return "string" |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 84 | elif json_type == "array": |
| 85 | return "object" |
| 86 | elif json_type == "object": |
| 87 | return "object" |
| 88 | elif json_type == "integer": |
| 89 | return "number" |
| 90 | elif json_type == "number": |
| 91 | return "number" |
| 92 | elif json_type == "any": |
| 93 | raise Exception("Unsupported") |
| 94 | else: |
| 95 | raise Exception("Unknown type: %s" % json_type) |
| 96 | |
| 97 | |
| 98 | class TypeData(object): |
| 99 | |
| 100 | def __init__(self, json_type): |
| 101 | if "type" not in json_type: |
| 102 | raise Exception("Unknown type") |
| 103 | json_type_name = json_type["type"] |
| 104 | self.raw_type_js_ = RawTypes.get_js(json_type_name) |
| 105 | |
| 106 | def get_raw_type_js(self): |
| 107 | return self.raw_type_js_ |
| 108 | |
| 109 | |
| 110 | class TypeMap: |
| 111 | |
| 112 | def __init__(self, api): |
| 113 | self.map_ = {} |
| 114 | for json_domain in api["domains"]: |
| 115 | domain_name = json_domain["domain"] |
| 116 | |
| 117 | domain_map = {} |
| 118 | self.map_[domain_name] = domain_map |
| 119 | |
| 120 | if "types" in json_domain: |
| 121 | for json_type in json_domain["types"]: |
| 122 | type_name = json_type["id"] |
| 123 | type_data = TypeData(json_type) |
| 124 | domain_map[type_name] = type_data |
| 125 | |
| 126 | def get(self, domain_name, type_name): |
| 127 | return self.map_[domain_name][type_name] |
| 128 | |
| 129 | |
| 130 | def resolve_param_raw_type_js(json_parameter, scope_domain_name): |
| 131 | if "$ref" in json_parameter: |
| 132 | json_ref = json_parameter["$ref"] |
| 133 | return get_ref_data_js(json_ref, scope_domain_name) |
| 134 | elif "type" in json_parameter: |
| 135 | json_type = json_parameter["type"] |
| 136 | return RawTypes.get_js(json_type) |
| 137 | else: |
| 138 | raise Exception("Unknown type") |
| 139 | |
| 140 | |
| 141 | def get_ref_data_js(json_ref, scope_domain_name): |
| 142 | dot_pos = json_ref.find(".") |
| 143 | if dot_pos == -1: |
| 144 | domain_name = scope_domain_name |
| 145 | type_name = json_ref |
| 146 | else: |
| 147 | domain_name = json_ref[:dot_pos] |
| 148 | type_name = json_ref[dot_pos + 1:] |
| 149 | |
| 150 | return type_map.get(domain_name, type_name).get_raw_type_js() |
| 151 | |
| 152 | |
| 153 | input_file = open(input_json_filename, "r") |
| 154 | json_string = input_file.read() |
| 155 | json_api = json.loads(json_string) |
| 156 | |
| 157 | |
| 158 | class Templates: |
| 159 | |
| 160 | def get_this_script_path_(absolute_path): |
| 161 | absolute_path = os.path.abspath(absolute_path) |
| 162 | components = [] |
| 163 | |
| 164 | def fill_recursive(path_part, depth): |
| 165 | if depth <= 0 or path_part == '/': |
| 166 | return |
| 167 | fill_recursive(os.path.dirname(path_part), depth - 1) |
| 168 | components.append(os.path.basename(path_part)) |
| 169 | |
| 170 | # Typical path is /Source/platform/inspector_protocol/CodeGenerator.py |
| 171 | # Let's take 4 components from the real path then. |
| 172 | fill_recursive(absolute_path, 4) |
| 173 | |
| 174 | return "/".join(components) |
| 175 | |
| 176 | file_header_ = ("// File is generated by %s\n\n" % get_this_script_path_(sys.argv[0]) + |
| 177 | """// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 178 | // Use of this source code is governed by a BSD-style license that can be |
| 179 | // found in the LICENSE file. |
| 180 | """) |
| 181 | |
| 182 | backend_js = string.Template(file_header_ + """ |
| 183 | |
| 184 | $domainInitializers |
| 185 | """) |
| 186 | |
| 187 | |
| 188 | type_map = TypeMap(json_api) |
| 189 | |
| 190 | |
| 191 | class Generator: |
| 192 | backend_js_domain_initializer_list = [] |
| 193 | |
| 194 | @staticmethod |
| 195 | def go(): |
| 196 | for json_domain in json_api["domains"]: |
| 197 | domain_name = json_domain["domain"] |
| 198 | domain_name_lower = domain_name.lower() |
| 199 | if domain_name_lower == "console": |
| 200 | continue |
| 201 | |
| 202 | Generator.backend_js_domain_initializer_list.append("// %s.\n" % domain_name) |
| 203 | |
| 204 | if "types" in json_domain: |
| 205 | for json_type in json_domain["types"]: |
| 206 | if "type" in json_type and json_type["type"] == "string" and "enum" in json_type: |
| 207 | enum_name = "%s.%s" % (domain_name, json_type["id"]) |
| 208 | Generator.process_enum(json_type, enum_name) |
| 209 | elif json_type["type"] == "object": |
| 210 | if "properties" in json_type: |
| 211 | for json_property in json_type["properties"]: |
| 212 | if "type" in json_property and json_property["type"] == "string" and "enum" in json_property: |
| 213 | enum_name = "%s.%s%s" % (domain_name, json_type["id"], to_title_case(json_property["name"])) |
| 214 | Generator.process_enum(json_property, enum_name) |
| 215 | |
| 216 | if "events" in json_domain: |
| 217 | for json_event in json_domain["events"]: |
| 218 | Generator.process_event(json_event, domain_name) |
| 219 | |
| 220 | if "commands" in json_domain: |
| 221 | for json_command in json_domain["commands"]: |
| 222 | Generator.process_command(json_command, domain_name) |
| 223 | |
| 224 | Generator.backend_js_domain_initializer_list.append("\n") |
| 225 | |
| 226 | @staticmethod |
| 227 | def process_enum(json_enum, enum_name): |
| 228 | enum_members = [] |
| 229 | for member in json_enum["enum"]: |
| 230 | enum_members.append("%s: \"%s\"" % (fix_camel_case(member), member)) |
| 231 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 232 | Generator.backend_js_domain_initializer_list.append( |
| 233 | "Protocol.inspectorBackend.registerEnum(\"%s\", {%s});\n" % (enum_name, ", ".join(enum_members))) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 234 | |
| 235 | @staticmethod |
| 236 | def process_event(json_event, domain_name): |
| 237 | event_name = json_event["name"] |
| 238 | |
| 239 | json_parameters = json_event.get("parameters") |
| 240 | |
| 241 | backend_js_event_param_list = [] |
| 242 | if json_parameters: |
| 243 | for parameter in json_parameters: |
| 244 | parameter_name = parameter["name"] |
| 245 | backend_js_event_param_list.append("\"%s\"" % parameter_name) |
| 246 | |
| 247 | Generator.backend_js_domain_initializer_list.append("Protocol.inspectorBackend.registerEvent(\"%s.%s\", [%s]);\n" % |
| 248 | (domain_name, event_name, ", ".join(backend_js_event_param_list))) |
| 249 | |
| 250 | @staticmethod |
| 251 | def process_command(json_command, domain_name): |
| 252 | json_command_name = json_command["name"] |
| 253 | |
| 254 | js_parameters_text = "" |
| 255 | if "parameters" in json_command: |
| 256 | json_params = json_command["parameters"] |
| 257 | js_param_list = [] |
| 258 | |
| 259 | for json_parameter in json_params: |
| 260 | json_param_name = json_parameter["name"] |
| 261 | js_bind_type = resolve_param_raw_type_js(json_parameter, domain_name) |
| 262 | |
| 263 | optional = json_parameter.get("optional") |
| 264 | |
| 265 | js_param_text = "{\"name\": \"%s\", \"type\": \"%s\", \"optional\": %s}" % (json_param_name, js_bind_type, ( |
| 266 | "true" if ("optional" in json_parameter and json_parameter["optional"]) else "false")) |
| 267 | |
| 268 | js_param_list.append(js_param_text) |
| 269 | |
| 270 | js_parameters_text = ", ".join(js_param_list) |
| 271 | |
| 272 | backend_js_reply_param_list = [] |
| 273 | if "returns" in json_command: |
| 274 | for json_return in json_command["returns"]: |
| 275 | json_return_name = json_return["name"] |
| 276 | backend_js_reply_param_list.append("\"%s\"" % json_return_name) |
| 277 | |
| 278 | js_reply_list = "[%s]" % ", ".join(backend_js_reply_param_list) |
| 279 | if "error" in json_command: |
| 280 | has_error_data_param = "true" |
| 281 | else: |
| 282 | has_error_data_param = "false" |
| 283 | |
| 284 | Generator.backend_js_domain_initializer_list.append( |
| 285 | "Protocol.inspectorBackend.registerCommand(\"%s.%s\", [%s], %s, %s);\n" % |
| 286 | (domain_name, json_command_name, js_parameters_text, js_reply_list, has_error_data_param)) |
| 287 | |
| 288 | |
| 289 | Generator.go() |
| 290 | |
| 291 | backend_js_file = open(output_js_dirname + "/InspectorBackendCommands.js", "w") |
| 292 | |
| 293 | backend_js_file.write( |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 294 | Templates.backend_js.substitute(None, domainInitializers="".join(Generator.backend_js_domain_initializer_list))) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 +0000 | [diff] [blame] | 295 | |
| 296 | backend_js_file.close() |