initial import
[vuplus_webkit] / Source / WebCore / inspector / generate-protocol-externs
1 #!/usr/bin/env python
2 # Copyright (c) 2011 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import os.path
31 import sys
32
33 program_name = os.path.basename(__file__)
34 if len(sys.argv) < 4 or sys.argv[1] != "-o":
35     sys.stderr.write("Usage: %s -o OUTPUT_FILE INPUT_FILE\n" % program_name)
36     exit(1)
37
38 output_path = sys.argv[2]
39 input_path = sys.argv[3]
40
41 input_file = open(input_path, "r")
42 json_string = input_file.read()
43 json_string = json_string.replace(": true", ": True")
44 json_string = json_string.replace(": false", ": false")
45 json_api = eval(json_string)
46
47 output_file = open(output_path, "w")
48
49 type_traits = {
50     "string": "string",
51     "integer": "number",
52     "number": "number",
53     "boolean": "boolean",
54     "array": "Array.<*>",
55     "object": "Object"
56 }
57
58 ref_types = {}
59
60 def full_qualified_type_id(domain_name, type_id):
61     if type_id.find(".") == -1:
62        return "%s.%s" % (domain_name, type_id)
63     return type_id
64     
65
66 def param_type(domain_name, param):
67     if "type" in param:
68         return type_traits[param["type"]]
69     if "$ref" in param:
70         type_id = full_qualified_type_id(domain_name, param["$ref"])
71         if type_id in ref_types:
72             ref_type = ref_types[type_id]
73             return type_traits[ref_type["type"]]
74         else:
75             print "Type not found: " + type_id
76             return "!! Type not found: " + type_id
77
78 for domain in json_api:
79     domain_name = domain["domain"]
80     if "types" in domain:
81         for type in domain["types"]:
82             type_id = full_qualified_type_id(domain_name, type["id"])
83             ref_types[type_id] = type;
84
85 for domain in json_api:
86     domain_name = domain["domain"]
87     output_file.write("\n\n\nvar %sAgent = {};\n" % domain_name)
88     if "commands" in domain:
89         for command in domain["commands"]:
90             output_file.write("\n/**\n")
91             params = []
92             if ("parameters" in command):
93                 for in_param in command["parameters"]:
94                     if ("optional" in in_param):
95                         params.append("opt_%s" % in_param["name"])
96                         output_file.write(" * @param {%s=} opt_%s\n" % (param_type(domain_name, in_param), in_param["name"]))
97                     else:
98                         params.append(in_param["name"])
99                         output_file.write(" * @param {%s} %s\n" % (param_type(domain_name, in_param), in_param["name"]))
100             returns = []
101             if ("returns" in command):
102                 for out_param in command["returns"]:
103                     if ("optional" in out_param):
104                         returns.append("%s=" % param_type(domain_name, out_param))
105                     else:
106                         returns.append("%s" % param_type(domain_name, out_param))
107             output_file.write(" * @param {function(%s):void=} opt_callback\n" % ", ".join(returns))
108             output_file.write(" */\n")
109             params.append("opt_callback");
110             output_file.write("%sAgent.%s = function(%s) {}\n" % (domain_name, command["name"], ", ".join(params)))
111             output_file.write("/** @param {function(%s):void=} opt_callback */\n" % ", ".join(returns))
112             output_file.write("%sAgent.%s.invoke = function(obj, opt_callback) {}\n" % (domain_name, command["name"]))
113
114     output_file.write("/** @interface */\n")
115     output_file.write("%sAgent.Dispatcher = function() {};\n" % domain_name)
116     if "events" in domain:
117         for event in domain["events"]:
118             params = []
119             if ("parameters" in event):
120                 output_file.write("/**\n")
121                 for param in event["parameters"]:
122                     if ("optional" in param):
123                         params.append("opt_%s" % param["name"])
124                         output_file.write(" * @param {%s=} opt_%s\n" % (param_type(domain_name, param), param["name"]))
125                     else:
126                         params.append(param["name"])
127                         output_file.write(" * @param {%s} %s\n" % (param_type(domain_name, param), param["name"]))
128                 output_file.write(" */\n")
129             output_file.write("%sAgent.Dispatcher.prototype.%s = function(%s) {};\n" % (domain_name, event["name"], ", ".join(params)));
130     output_file.write("/**\n * @param {%sAgent.Dispatcher} dispatcher\n */\n" % domain_name)
131     output_file.write("InspectorBackend.register%sDispatcher = function(dispatcher) {}\n" % domain_name);
132 output_file.close()