OLD | NEW |
| (Empty) |
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 #include <string> | |
25 | |
26 namespace content { | |
27 namespace devtools { | |
28 | |
29 extern const char kProtocolVersion[]; | |
30 | |
31 bool IsSupportedProtocolVersion(const std::string& version); | |
32 | |
33 extern const char kResult[]; | |
34 $contents | |
35 | |
36 } // devtools | |
37 } // content | |
38 | |
39 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_PROTOCOL_CONSTANTS_H_ | |
40 """) | |
41 | |
42 template_cc = string.Template("""\ | |
43 // Copyright 2013 The Chromium Authors. All rights reserved. | |
44 // Use of this source code is governed by a BSD-style license that can be | |
45 // found in the LICENSE file. | |
46 | |
47 // THIS FILE IS AUTOGENERATED. DO NOT EDIT. | |
48 // Generated by | |
49 // content/browser/devtools/devtools_protocol_constants_generator.py from | |
50 // third_party/WebKit/Source/devtools/protocol.json and | |
51 // content/browser/devtools/browser_protocol.json). | |
52 | |
53 #include "base/strings/string_number_conversions.h" | |
54 #include "base/strings/string_util.h" | |
55 #include "content/browser/devtools/devtools_protocol_constants.h" | |
56 | |
57 namespace content { | |
58 namespace devtools { | |
59 | |
60 const char kProtocolVersion[] = "$major.$minor"; | |
61 | |
62 bool IsSupportedProtocolVersion(const std::string& version) { | |
63 std::vector<std::string> tokens; | |
64 Tokenize(version, ".", &tokens); | |
65 int major, minor; | |
66 return tokens.size() == 2 && | |
67 base::StringToInt(tokens[0], &major) && major == $major && | |
68 base::StringToInt(tokens[1], &minor) && minor <= $minor; | |
69 } | |
70 | |
71 const char kResult[] = "result"; | |
72 $contents | |
73 | |
74 } // devtools | |
75 } // content | |
76 """) | |
77 | |
78 def Capitalize(s): | |
79 return s[:1].capitalize() + s[1:] | |
80 | |
81 references = [] | |
82 | |
83 def CreateNamespace(domain_name, data, keys, prefixes, name = None): | |
84 result = {} | |
85 if name: | |
86 result["kName"] = name | |
87 for i, key in enumerate(keys): | |
88 if key in data: | |
89 for parameter in data[key]: | |
90 parameter_name = parameter["name"]; | |
91 result[prefixes[i] + Capitalize(parameter_name)] = parameter_name | |
92 if "enum" in parameter: | |
93 enum_name = Capitalize(parameter_name) | |
94 result[enum_name] = {} | |
95 for enum in parameter["enum"]: | |
96 result[enum_name]["kEnum" + Capitalize(enum)] = enum | |
97 reference = "" | |
98 if "$ref" in parameter: | |
99 reference = parameter["$ref"] | |
100 if "items" in parameter and "$ref" in parameter["items"]: | |
101 reference = parameter["items"]["$ref"] | |
102 if reference: | |
103 if not "." in reference: | |
104 reference = domain_name + "." + reference | |
105 references.append(reference) | |
106 return result | |
107 | |
108 def IsHandledInBrowser(item): | |
109 return "handlers" in item and "browser" in item["handlers"] | |
110 | |
111 def FormatContents(tree, indent, format_string): | |
112 outer = dict((key, value) for key, value in tree.iteritems() | |
113 if not isinstance(value, dict)) | |
114 inner = dict((key, value) for key, value in tree.iteritems() | |
115 if isinstance(value, dict)) | |
116 body = "" | |
117 body += "".join(indent + format_string.format(key, value) | |
118 for (key, value) in sorted(outer.items())) | |
119 body += "".join(FormatNamespace(key, value, indent, format_string) | |
120 for (key, value) in sorted(inner.items())) | |
121 return body | |
122 | |
123 def FormatNamespace(title, tree, indent, format_string): | |
124 if (not tree): | |
125 return "" | |
126 body = '\n' + indent + "namespace " + title + " {\n" | |
127 body += FormatContents(tree, indent + " ", format_string) | |
128 body += indent + "} // " + title + "\n" | |
129 return body | |
130 | |
131 def CreateHeader(tree, output_file): | |
132 contents = FormatContents(tree, "", "extern const char {0}[];\n") | |
133 output_file.write(template_h.substitute({"contents": contents})) | |
134 | |
135 def CreateBody(tree, version, output_file): | |
136 contents = FormatContents(tree, "", "const char {0}[] = \"{1}\";\n") | |
137 output_file.write(template_cc.substitute({ | |
138 "major": version["major"], | |
139 "minor": version["minor"], | |
140 "contents": contents | |
141 })) | |
142 | |
143 blink_protocol_data = open(sys.argv[1]).read() | |
144 browser_protocol_data = open(sys.argv[2]).read() | |
145 | |
146 blink_protocol = json.loads(blink_protocol_data) | |
147 browser_protocol = json.loads(browser_protocol_data) | |
148 | |
149 blink_version = blink_protocol["version"] | |
150 | |
151 domains = blink_protocol["domains"] + browser_protocol["domains"] | |
152 | |
153 namespace_tree = {} | |
154 | |
155 for domain in domains: | |
156 domain_value = {} | |
157 domain_namespace_name = Capitalize(domain["domain"]) | |
158 if "commands" in domain: | |
159 for command in domain["commands"]: | |
160 if (IsHandledInBrowser(command)): | |
161 domain_value[command["name"]] = CreateNamespace(domain["domain"], | |
162 command, ["parameters", "returns"], ["kParam", "kResponse"], | |
163 domain_namespace_name + "." + command["name"]) | |
164 | |
165 if "events" in domain: | |
166 for event in domain["events"]: | |
167 if IsHandledInBrowser(event): | |
168 domain_value[event["name"]] = CreateNamespace(domain["domain"], | |
169 event, ["parameters"], ["kParam"], | |
170 domain_namespace_name + "." + event["name"]) | |
171 if domain_value: | |
172 namespace_tree[domain_namespace_name] = domain_value | |
173 | |
174 while (references): | |
175 reference = references.pop(); | |
176 path = reference.split("."); | |
177 parent_namespace = namespace_tree; | |
178 for path_segment in path[0:-1]: | |
179 if path_segment not in parent_namespace: | |
180 parent_namespace[path_segment] = {} | |
181 parent_namespace = parent_namespace[path_segment] | |
182 if (path[-1] not in parent_namespace): | |
183 try: | |
184 domain = [d for d in domains if d["domain"] == path[0]][0] | |
185 ref_type = [t for t in domain["types"] if t["id"] == path[1]][0] | |
186 parent_namespace[ref_type["id"]] = CreateNamespace(path[0], | |
187 ref_type, ["properties"], ["kParam"]) | |
188 except IndexError: | |
189 sys.stderr.write("Failed to resolve type [{0}].\n".format(reference)) | |
190 sys.exit(1) | |
191 | |
192 for (namespace_name, namespace) in namespace_tree.items(): | |
193 namespace["kName"] = namespace_name | |
194 | |
195 with open(sys.argv[3], "w") as f: | |
196 CreateBody(namespace_tree, blink_version, f) | |
197 | |
198 with open(sys.argv[4], "w") as f: | |
199 CreateHeader(namespace_tree, f) | |
OLD | NEW |