OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os.path | 5 import os.path |
6 import sys | 6 import sys |
7 import string | 7 import string |
8 import optparse | 8 import optparse |
9 import re | 9 import re |
10 try: | 10 try: |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 trim_blocks=True) | 81 trim_blocks=True) |
82 jinja_env.filters.update({"to_title_case": to_title_case, "dash_to_camelcase
": dash_to_camelcase}) | 82 jinja_env.filters.update({"to_title_case": to_title_case, "dash_to_camelcase
": dash_to_camelcase}) |
83 jinja_env.add_extension('jinja2.ext.loopcontrols') | 83 jinja_env.add_extension('jinja2.ext.loopcontrols') |
84 return jinja_env | 84 return jinja_env |
85 | 85 |
86 | 86 |
87 def output_file(file_name): | 87 def output_file(file_name): |
88 return open(file_name, "w") | 88 return open(file_name, "w") |
89 | 89 |
90 | 90 |
| 91 def topsort_domains(): |
| 92 domains = {} |
| 93 for domain in json_api["domains"]: |
| 94 domains[domain["domain"]] = domain |
| 95 |
| 96 processed = set() |
| 97 result = [] |
| 98 |
| 99 def process(name): |
| 100 if name in processed: |
| 101 return |
| 102 domain = domains[name] |
| 103 deps = [] |
| 104 if "depends" in domain: |
| 105 for dep in domain["depends"]: |
| 106 process(dep) |
| 107 result.append(domain) |
| 108 processed.add(name) |
| 109 |
| 110 for domain in json_api["domains"]: |
| 111 process(domain["domain"]) |
| 112 json_api["domains"] = result |
| 113 |
| 114 |
91 def patch_full_qualified_refs(): | 115 def patch_full_qualified_refs(): |
92 def patch_full_qualified_refs_in_domain(json, domain_name): | 116 def patch_full_qualified_refs_in_domain(json, domain_name): |
93 if isinstance(json, list): | 117 if isinstance(json, list): |
94 for item in json: | 118 for item in json: |
95 patch_full_qualified_refs_in_domain(item, domain_name) | 119 patch_full_qualified_refs_in_domain(item, domain_name) |
96 | 120 |
97 if not isinstance(json, dict): | 121 if not isinstance(json, dict): |
98 return | 122 return |
99 for key in json: | 123 for key in json: |
100 if key == "type" and json[key] == "string": | 124 if key == "type" and json[key] == "string": |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 if type["type"] == "object": | 256 if type["type"] == "object": |
233 type_definitions[domain["domain"] + "." + type["id"]] = create_u
ser_type_definition(domain["domain"], type) | 257 type_definitions[domain["domain"] + "." + type["id"]] = create_u
ser_type_definition(domain["domain"], type) |
234 elif type["type"] == "array": | 258 elif type["type"] == "array": |
235 items_type = type["items"]["type"] | 259 items_type = type["items"]["type"] |
236 type_definitions[domain["domain"] + "." + type["id"]] = wrap_arr
ay_definition(type_definitions[items_type]) | 260 type_definitions[domain["domain"] + "." + type["id"]] = wrap_arr
ay_definition(type_definitions[items_type]) |
237 elif type["type"] == domain["domain"] + ".string": | 261 elif type["type"] == domain["domain"] + ".string": |
238 type_definitions[domain["domain"] + "." + type["id"]] = create_s
tring_type_definition(domain["domain"]) | 262 type_definitions[domain["domain"] + "." + type["id"]] = create_s
tring_type_definition(domain["domain"]) |
239 else: | 263 else: |
240 type_definitions[domain["domain"] + "." + type["id"]] = create_p
rimitive_type_definition(type["type"]) | 264 type_definitions[domain["domain"] + "." + type["id"]] = create_p
rimitive_type_definition(type["type"]) |
241 | 265 |
| 266 topsort_domains() |
242 patch_full_qualified_refs() | 267 patch_full_qualified_refs() |
243 create_type_definitions() | 268 create_type_definitions() |
244 | 269 |
245 | 270 |
246 def type_definition(name): | 271 def type_definition(name): |
247 return type_definitions[name] | 272 return type_definitions[name] |
248 | 273 |
249 | 274 |
250 def resolve_type(property): | 275 def resolve_type(property): |
251 if "$ref" in property: | 276 if "$ref" in property: |
252 return type_definitions[property["$ref"]] | 277 return type_definitions[property["$ref"]] |
253 if property["type"] == "array": | 278 if property["type"] == "array": |
254 return wrap_array_definition(resolve_type(property["items"])) | 279 return wrap_array_definition(resolve_type(property["items"])) |
255 return type_definitions[property["type"]] | 280 return type_definitions[property["type"]] |
256 | 281 |
257 | 282 |
258 def join_arrays(dict, keys): | 283 def join_arrays(dict, keys): |
259 result = [] | 284 result = [] |
260 for key in keys: | 285 for key in keys: |
261 if key in dict: | 286 if key in dict: |
262 result += dict[key] | 287 result += dict[key] |
263 return result | 288 return result |
264 | 289 |
265 | 290 |
| 291 def has_disable(commands): |
| 292 for command in commands: |
| 293 if command["name"] == "disable": |
| 294 return True |
| 295 return False |
| 296 |
| 297 |
266 if os.path.exists(__file__): | 298 if os.path.exists(__file__): |
267 current_script_timestamp = os.path.getmtime(__file__) | 299 current_script_timestamp = os.path.getmtime(__file__) |
268 else: | 300 else: |
269 current_script_timestamp = 0 | 301 current_script_timestamp = 0 |
270 | 302 |
271 | 303 |
272 def is_up_to_date(file, template): | 304 def is_up_to_date(file, template): |
273 if not os.path.exists(file): | 305 if not os.path.exists(file): |
274 return False | 306 return False |
275 timestamp = os.path.getmtime(file) | 307 timestamp = os.path.getmtime(file) |
276 return timestamp > max(os.path.getmtime(module_path + template), | 308 return timestamp > max(os.path.getmtime(module_path + template), |
277 current_script_timestamp, json_timestamp) | 309 current_script_timestamp, json_timestamp) |
278 | 310 |
279 | 311 |
280 def generate(class_name): | 312 def generate(class_name): |
281 h_template_name = "/%s_h.template" % class_name | 313 h_template_name = "/%s_h.template" % class_name |
282 cpp_template_name = "/%s_cpp.template" % class_name | 314 cpp_template_name = "/%s_cpp.template" % class_name |
283 h_file_name = output_dirname + "/" + class_name + ".h" | 315 h_file_name = output_dirname + "/" + class_name + ".h" |
284 cpp_file_name = output_dirname + "/" + class_name + ".cpp" | 316 cpp_file_name = output_dirname + "/" + class_name + ".cpp" |
285 | 317 |
286 if (is_up_to_date(cpp_file_name, cpp_template_name) and | 318 if (is_up_to_date(cpp_file_name, cpp_template_name) and |
287 is_up_to_date(h_file_name, h_template_name)): | 319 is_up_to_date(h_file_name, h_template_name)): |
288 return | 320 return |
289 | 321 |
290 template_context = { | 322 template_context = { |
291 "class_name": class_name, | 323 "class_name": class_name, |
292 "api": json_api, | 324 "api": json_api, |
293 "join_arrays": join_arrays, | 325 "join_arrays": join_arrays, |
294 "resolve_type": resolve_type, | 326 "resolve_type": resolve_type, |
295 "type_definition": type_definition | 327 "type_definition": type_definition, |
| 328 "has_disable": has_disable |
296 } | 329 } |
297 h_template = jinja_env.get_template(h_template_name) | 330 h_template = jinja_env.get_template(h_template_name) |
298 cpp_template = jinja_env.get_template(cpp_template_name) | 331 cpp_template = jinja_env.get_template(cpp_template_name) |
299 h_file = output_file(h_file_name) | 332 h_file = output_file(h_file_name) |
300 cpp_file = output_file(cpp_file_name) | 333 cpp_file = output_file(cpp_file_name) |
301 h_file.write(h_template.render(template_context)) | 334 h_file.write(h_template.render(template_context)) |
302 cpp_file.write(cpp_template.render(template_context)) | 335 cpp_file.write(cpp_template.render(template_context)) |
303 h_file.close() | 336 h_file.close() |
304 cpp_file.close() | 337 cpp_file.close() |
305 | 338 |
306 | 339 |
307 jinja_env = initialize_jinja_env(output_dirname) | 340 jinja_env = initialize_jinja_env(output_dirname) |
308 generate("Backend") | |
309 generate("Dispatcher") | |
310 generate("Frontend") | |
311 generate("TypeBuilder") | 341 generate("TypeBuilder") |
OLD | NEW |