Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / browser / devtools / devtools_protocol_constants_generator.py
1 #!/usr/bin/python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import sys
7 import string
8 import json
9
10 template_h = string.Template("""\
11 // Copyright 2013 The Chromium Authors. All rights reserved.
12 // Use of this source code is governed by a BSD-style license that can be
13 // found in the LICENSE file.
14
15 #ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
16 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
17
18 // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
19 // Generated by
20 //  content/browser/devtools/devtools_protocol_constants_generator.py from
21 //  third_party/WebKit/Source/devtools/protocol.json and
22 //  content/browser/devtools/browser_protocol.json).
23
24 namespace content {
25 namespace devtools {
26
27 extern const char kResult[];
28 $contents
29
30 }  // devtools
31 }  // content
32
33 #endif  // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_
34 """)
35
36 template_cc = string.Template("""\
37 // Copyright 2013 The Chromium Authors. All rights reserved.
38 // Use of this source code is governed by a BSD-style license that can be
39 // found in the LICENSE file.
40
41 // THIS FILE IS AUTOGENERATED. DO NOT EDIT.
42 // Generated by
43 //  content/browser/devtools/devtools_protocol_constants_generator.py from
44 //  third_party/WebKit/Source/devtools/protocol.json and
45 //  content/browser/devtools/browser_protocol.json).
46
47 #include "content/browser/devtools/devtools_protocol_constants.h"
48
49 namespace content {
50 namespace devtools {
51
52 const char kResult[] = "result";
53 $contents
54
55 }  // devtools
56 }  // content
57 """)
58
59 def Capitalize(s):
60   return s[:1].capitalize() + s[1:]
61
62 references = []
63
64 def CreateNamespace(domain_name, data, keys, prefixes, name = None):
65   result = {}
66   if name:
67     result["kName"] = name
68   for i, key in enumerate(keys):
69     if key in data:
70       for parameter in data[key]:
71         parameter_name = parameter["name"];
72         result[prefixes[i] + Capitalize(parameter_name)] = parameter_name
73         if "enum" in parameter:
74           enum_name = Capitalize(parameter_name)
75           result[enum_name] = {}
76           for enum in parameter["enum"]:
77             result[enum_name]["kEnum" + Capitalize(enum)] = enum
78         reference = ""
79         if "$ref" in parameter:
80           reference = parameter["$ref"]
81         if "items" in parameter and "$ref" in parameter["items"]:
82           reference = parameter["items"]["$ref"]
83         if reference:
84           if not "." in reference:
85             reference = domain_name + "." + reference
86           references.append(reference)
87   return result
88
89 def IsHandledInBrowser(item):
90   return "handlers" in item and "browser" in item["handlers"]
91
92 def FormatContents(tree, indent, format_string):
93   outer = dict((key, value) for key, value in tree.iteritems()
94                 if not isinstance(value, dict))
95   inner = dict((key, value) for key, value in tree.iteritems()
96               if isinstance(value, dict))
97   body = ""
98   body += "".join(indent + format_string.format(key, value)
99                  for (key, value) in sorted(outer.items()))
100   body += "".join(FormatNamespace(key, value, indent, format_string)
101                  for (key, value) in sorted(inner.items()))
102   return body
103
104 def FormatNamespace(title, tree, indent, format_string):
105   if (not tree):
106     return ""
107   body = '\n' + indent + "namespace " + title + " {\n"
108   body += FormatContents(tree, indent + "  ", format_string)
109   body += indent + "} // " + title + "\n"
110   return body
111
112 def CreateHeader(tree, output_file):
113   contents = FormatContents(tree, "", "extern const char {0}[];\n")
114   output_file.write(template_h.substitute({"contents": contents}))
115
116 def CreateBody(tree, output_file):
117   contents = FormatContents(tree, "", "const char {0}[] = \"{1}\";\n")
118   output_file.write(template_cc.substitute({"contents": contents}))
119
120 blink_protocol_data = open(sys.argv[1]).read()
121 browser_protocol_data = open(sys.argv[2]).read()
122
123 blink_protocol = json.loads(blink_protocol_data)
124 browser_protocol = json.loads(browser_protocol_data)
125
126 domains = blink_protocol["domains"] + browser_protocol["domains"]
127
128 namespace_tree = {}
129
130 for domain in domains:
131   domain_value = {}
132   domain_namespace_name = Capitalize(domain["domain"])
133   if "commands" in domain:
134     for command in domain["commands"]:
135       if (IsHandledInBrowser(command)):
136         domain_value[command["name"]] = CreateNamespace(domain["domain"],
137             command, ["parameters", "returns"], ["kParam", "kResponse"],
138             domain_namespace_name + "." + command["name"])
139
140   if "events" in domain:
141     for event in domain["events"]:
142       if IsHandledInBrowser(event):
143         domain_value[event["name"]] = CreateNamespace(domain["domain"],
144             event, ["parameters"], ["kParam"],
145             domain_namespace_name + "." + event["name"])
146   if domain_value:
147     namespace_tree[domain_namespace_name] = domain_value
148
149 while (references):
150   reference = references.pop();
151   path = reference.split(".");
152   parent_namespace = namespace_tree;
153   for path_segment in path[0:-1]:
154     if path_segment not in parent_namespace:
155       parent_namespace[path_segment] = {}
156     parent_namespace = parent_namespace[path_segment]
157   if (path[-1] not in parent_namespace):
158     try:
159       domain = [d for d in domains if d["domain"] == path[0]][0]
160       ref_type = [t for t in domain["types"] if t["id"] == path[1]][0]
161       parent_namespace[ref_type["id"]] = CreateNamespace(path[0],
162           ref_type, ["properties"], ["kParam"])
163     except IndexError:
164       sys.stderr.write("Failed to resolve type [{0}].\n".format(reference))
165       sys.exit(1)
166
167 for (namespace_name, namespace) in namespace_tree.items():
168   namespace["kName"] = namespace_name
169
170 with open(sys.argv[3], "w") as f:
171   CreateBody(namespace_tree, f)
172
173 with open(sys.argv[4], "w") as f:
174   CreateHeader(namespace_tree, f)