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
30import os.path
31import sys
32
33program_name = os.path.basename(__file__)
34if 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
38output_path = sys.argv[2]
39input_path = sys.argv[3]
40
41input_file = open(input_path, "r")
42json_string = input_file.read()
43json_string = json_string.replace(": true", ": True")
44json_string = json_string.replace(": false", ": false")
45json_api = eval(json_string)
46
47output_file = open(output_path, "w")
48output_file.write("""/*
49 * Copyright (C) 2011 Google, Inc. All Rights Reserved.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 *    notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 *    notice, this list of conditions and the following disclaimer in the
58 *    documentation and/or other materials provided with the distribution.
59 *
60 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
61 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
63 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
64 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
65 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
66 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
67 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
68 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
69 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
70 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
71 */
72 
73module core {""")
74
75type_traits = {
76    "string": "String",
77    "integer": "int",
78    "number": "double",
79    "boolean": "boolean",
80    "array": "Array",
81    "object": "Object"
82}
83
84ref_types = {}
85
86macro_traits = {
87    "Database": "DATABASE",
88    "DOMStorage": "DOM_STORAGE",
89    "ApplicationCache": "OFFLINE_WEB_APPLICATIONS",
90    "Debugger": "JAVASCRIPT_DEBUGGER",
91    "BrowserDebugger": "JAVASCRIPT_DEBUGGER",
92    "Profiler": "JAVASCRIPT_DEBUGGER"
93}
94
95def full_qualified_type_id(domain_name, type_id):
96    if type_id.find(".") == -1:
97       return "%s.%s" % (domain_name, type_id)
98    return type_id
99    
100
101def param_type(domain_name, param):
102    if "type" in param:
103        return type_traits[param["type"]]
104    if "$ref" in param:
105        type_id = full_qualified_type_id(domain_name, param["$ref"])
106        if type_id in ref_types:
107            ref_type = ref_types[type_id]
108            return type_traits[ref_type["type"]]
109        else:
110            return "Object"
111
112for domain in json_api:
113    domain_name = domain["domain"]
114    if domain_name in macro_traits:
115        output_file.write("\n#if defined(ENABLE_%s) && ENABLE_%s" % (macro_traits[domain_name], macro_traits[domain_name]))
116    output_file.write("\n    interface [Conditional=INSPECTOR] %s {" % domain_name)
117
118    if "types" in domain:
119        for type in domain["types"]:
120            type_id = full_qualified_type_id(domain_name, type["id"])
121            ref_types[type_id] = type;
122
123    if "commands" in domain:
124        for command in domain["commands"]:
125            params = []
126            if ("parameters" in command):
127                for in_param in command["parameters"]:
128                    if ("optional" in in_param):
129                        optional = " [optional]"
130                    else:
131                        optional = ""
132                    params.append("in%s %s %s" % (optional, param_type(domain_name, in_param), in_param["name"]))
133            if ("returns" in command):
134                for out_param in command["returns"]:
135                    params.append("out %s %s" % (param_type(domain_name, out_param), out_param["name"]))
136            output_file.write("\n        void %s(%s);" % (command["name"], ", ".join(params)))
137
138    if "events" in domain:
139        for event in domain["events"]:
140            params = []
141            if ("parameters" in event):
142                for in_param in event["parameters"]:
143                    if ("optional" in in_param):
144                        optional = " [optional]"
145                    else:
146                        optional = ""
147                    params.append("out%s %s %s" % (optional, param_type(domain_name, in_param), in_param["name"]))
148            output_file.write("\n        [event] void %s(%s);" % (event["name"], ", ".join(params)))
149
150    output_file.write("\n    };")
151    if domain["domain"] in macro_traits:
152        output_file.write("\n#endif // ENABLE_%s" % macro_traits[domain["domain"]])
153output_file.write("\n}\n")
154output_file.close()
155