153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#!/usr/bin/env python 253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# Copyright (c) 2011 Google Inc. All rights reserved. 353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# Copyright (c) 2012 Intel Corporation. All rights reserved. 453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# 553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# Redistribution and use in source and binary forms, with or without 653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# modification, are permitted provided that the following conditions are 753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# met: 853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# 953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# * Redistributions of source code must retain the above copyright 1053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# notice, this list of conditions and the following disclaimer. 1153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# * Redistributions in binary form must reproduce the above 1253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer 1353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# in the documentation and/or other materials provided with the 1453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# distribution. 1553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# * Neither the name of Google Inc. nor the names of its 1653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# contributors may be used to endorse or promote products derived from 1753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# this software without specific prior written permission. 1853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# 1953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 3153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)import os.path 3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)import sys 3353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)import string 3453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)import optparse 3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)import re 3653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)try: 3753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) import json 3853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)except ImportError: 3953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) import simplejson as json 4053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 4153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)cmdline_parser = optparse.OptionParser() 4253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)cmdline_parser.add_option("--output_js_dir") 4353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 4453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)try: 4553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) arg_options, arg_values = cmdline_parser.parse_args() 4653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if (len(arg_values) != 1): 4753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) raise Exception("Exactly one plain argument expected (found %s)" % len(arg_values)) 4853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) input_json_filename = arg_values[0] 4953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) output_js_dirname = arg_options.output_js_dir 5053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if not output_js_dirname: 5153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) raise Exception("Output .js directory must be specified") 5253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)except Exception: 5353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.html 5453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) exc = sys.exc_info()[1] 5553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc) 5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) sys.stderr.write("Usage: <script> protocol.json --output_js_dir <output_js_dir>\n") 5753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) exit(1) 5853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 5953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)def fix_camel_case(name): 6153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) refined = re.sub(r'-(\w)', lambda pat: pat.group(1).upper(), name) 6253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) refined = to_title_case(refined) 6353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return re.sub(r'(?i)HTML|XML|WML|API', lambda pat: pat.group(0).upper(), refined) 6453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 6553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 6653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)def to_title_case(name): 6753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return name[:1].upper() + name[1:] 6853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 6953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 7053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class RawTypes(object): 7153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) @staticmethod 7253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def get_js(json_type): 7353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if json_type == "boolean": 7453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return "boolean" 7553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) elif json_type == "string": 7653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return "string" 7753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) elif json_type == "array": 7853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return "object" 7953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) elif json_type == "object": 8053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return "object" 8153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) elif json_type == "integer": 8253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return "number" 8353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) elif json_type == "number": 8453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return "number" 8553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) elif json_type == "any": 8653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) raise Exception("Unsupported") 8753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) else: 8853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) raise Exception("Unknown type: %s" % json_type) 8953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 9053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 9153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class TypeData(object): 9253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def __init__(self, json_type): 9353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "type" not in json_type: 9453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) raise Exception("Unknown type") 9553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_type_name = json_type["type"] 9653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) self.raw_type_js_ = RawTypes.get_js(json_type_name) 9753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 9853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def get_raw_type_js(self): 9953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return self.raw_type_js_ 10053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 10153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 10253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class TypeMap: 10353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def __init__(self, api): 10453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) self.map_ = {} 10553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_domain in api["domains"]: 10653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domain_name = json_domain["domain"] 10753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 10853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domain_map = {} 10953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) self.map_[domain_name] = domain_map 11053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 11153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "types" in json_domain: 11253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_type in json_domain["types"]: 11353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) type_name = json_type["id"] 11453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) type_data = TypeData(json_type) 11553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domain_map[type_name] = type_data 11653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 11753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def get(self, domain_name, type_name): 11853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return self.map_[domain_name][type_name] 11953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 12053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 12153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)def resolve_param_raw_type_js(json_parameter, scope_domain_name): 12253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "$ref" in json_parameter: 12353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_ref = json_parameter["$ref"] 12453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return get_ref_data_js(json_ref, scope_domain_name) 12553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) elif "type" in json_parameter: 12653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_type = json_parameter["type"] 12753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return RawTypes.get_js(json_type) 12853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) else: 12953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) raise Exception("Unknown type") 13053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 13153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 13253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)def get_ref_data_js(json_ref, scope_domain_name): 13353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) dot_pos = json_ref.find(".") 13453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if dot_pos == -1: 13553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domain_name = scope_domain_name 13653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) type_name = json_ref 13753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) else: 13853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domain_name = json_ref[:dot_pos] 13953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) type_name = json_ref[dot_pos + 1:] 14053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 14153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return type_map.get(domain_name, type_name).get_raw_type_js() 14253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 14353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 14453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)input_file = open(input_json_filename, "r") 14553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)json_string = input_file.read() 14653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)json_api = json.loads(json_string) 14753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 14853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 14953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class Templates: 15053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def get_this_script_path_(absolute_path): 15153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) absolute_path = os.path.abspath(absolute_path) 15253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) components = [] 15353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 15453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def fill_recursive(path_part, depth): 15553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if depth <= 0 or path_part == '/': 15653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return 15753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) fill_recursive(os.path.dirname(path_part), depth - 1) 15853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) components.append(os.path.basename(path_part)) 15953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 16053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) # Typical path is /Source/WebCore/inspector/CodeGeneratorInspector.py 16153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) # Let's take 4 components from the real path then. 16253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) fill_recursive(absolute_path, 4) 16353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 16453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) return "/".join(components) 16553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 16653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) file_header_ = ("// File is generated by %s\n\n" % get_this_script_path_(sys.argv[0]) + 16753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)"""// Copyright (c) 2011 The Chromium Authors. All rights reserved. 16853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 16953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)// found in the LICENSE file. 17053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)""") 17153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 17253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) backend_js = string.Template(file_header_ + """ 17353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 17453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)$domainInitializers 17553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)""") 17653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 17753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 17853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)type_map = TypeMap(json_api) 17953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 18053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 18153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)class Generator: 18253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) backend_js_domain_initializer_list = [] 18353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 18453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) @staticmethod 18553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def go(): 18653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_domain in json_api["domains"]: 18753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domain_name = json_domain["domain"] 18853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domain_name_lower = domain_name.lower() 18953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 19053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) Generator.backend_js_domain_initializer_list.append("// %s.\n" % domain_name) 19153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 19253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "types" in json_domain: 19353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_type in json_domain["types"]: 19453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "type" in json_type and json_type["type"] == "string" and "enum" in json_type: 19553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) enum_name = "%s.%s" % (domain_name, json_type["id"]) 19653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) Generator.process_enum(json_type, enum_name) 19753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) elif json_type["type"] == "object": 19853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "properties" in json_type: 19953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_property in json_type["properties"]: 20053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "type" in json_property and json_property["type"] == "string" and "enum" in json_property: 20153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) enum_name = "%s.%s%s" % (domain_name, json_type["id"], to_title_case(json_property["name"])) 20253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) Generator.process_enum(json_property, enum_name) 20353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 20453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "events" in json_domain: 20553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_event in json_domain["events"]: 20653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) Generator.process_event(json_event, domain_name) 20753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 20853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "commands" in json_domain: 20953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_command in json_domain["commands"]: 21053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) Generator.process_command(json_command, domain_name) 21153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 21253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) Generator.backend_js_domain_initializer_list.append("\n") 21353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 21453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) @staticmethod 21553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def process_enum(json_enum, enum_name): 21653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) enum_members = [] 21753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for member in json_enum["enum"]: 21853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) enum_members.append("%s: \"%s\"" % (fix_camel_case(member), member)) 21953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 22053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) Generator.backend_js_domain_initializer_list.append("InspectorBackend.registerEnum(\"%s\", {%s});\n" % ( 22153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) enum_name, ", ".join(enum_members))) 22253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 22353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) @staticmethod 22453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def process_event(json_event, domain_name): 22553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) event_name = json_event["name"] 22653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 22753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_parameters = json_event.get("parameters") 22853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 22953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) backend_js_event_param_list = [] 23053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if json_parameters: 23153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for parameter in json_parameters: 23253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) parameter_name = parameter["name"] 23353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) backend_js_event_param_list.append("\"%s\"" % parameter_name) 23453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 23553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) Generator.backend_js_domain_initializer_list.append("InspectorBackend.registerEvent(\"%s.%s\", [%s]);\n" % ( 23653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domain_name, event_name, ", ".join(backend_js_event_param_list))) 23753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 23853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) @staticmethod 23953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) def process_command(json_command, domain_name): 24053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_command_name = json_command["name"] 24153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 24253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) js_parameters_text = "" 24353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "parameters" in json_command: 24453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_params = json_command["parameters"] 24553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) js_param_list = [] 24653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 24753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_parameter in json_params: 24853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_param_name = json_parameter["name"] 24953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) js_bind_type = resolve_param_raw_type_js(json_parameter, domain_name) 25053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 25153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) optional = json_parameter.get("optional") 25253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 25353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 25453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) js_param_text = "{\"name\": \"%s\", \"type\": \"%s\", \"optional\": %s}" % ( 25553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_param_name, 25653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) js_bind_type, 25753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) ("true" if ("optional" in json_parameter and json_parameter["optional"]) else "false")) 25853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 25953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) js_param_list.append(js_param_text) 26053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 26153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) js_parameters_text = ", ".join(js_param_list) 26253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 26353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 26453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) backend_js_reply_param_list = [] 26553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) if "returns" in json_command: 26653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) for json_return in json_command["returns"]: 26753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) json_return_name = json_return["name"] 26853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) backend_js_reply_param_list.append("\"%s\"" % json_return_name) 26953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 27053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) js_reply_list = "[%s]" % ", ".join(backend_js_reply_param_list) 271591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch if "error" in json_command: 272591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch has_error_data_param = "true" 273591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch else: 274591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch has_error_data_param = "false" 27553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 276591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch Generator.backend_js_domain_initializer_list.append("InspectorBackend.registerCommand(\"%s.%s\", [%s], %s, %s);\n" % (domain_name, json_command_name, js_parameters_text, js_reply_list, has_error_data_param)) 27753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 27853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)Generator.go() 27953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 28053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)backend_js_file = open(output_js_dirname + "/InspectorBackendCommands.js", "w") 28153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 28253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)backend_js_file.write(Templates.backend_js.substitute(None, 28353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) domainInitializers="".join(Generator.backend_js_domain_initializer_list))) 28453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) 28553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)backend_js_file.close() 286