| 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: |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 cpp_template = jinja_env.get_template(cpp_template_name) | 323 cpp_template = jinja_env.get_template(cpp_template_name) |
| 299 h_file = output_file(h_file_name) | 324 h_file = output_file(h_file_name) |
| 300 cpp_file = output_file(cpp_file_name) | 325 cpp_file = output_file(cpp_file_name) |
| 301 h_file.write(h_template.render(template_context)) | 326 h_file.write(h_template.render(template_context)) |
| 302 cpp_file.write(cpp_template.render(template_context)) | 327 cpp_file.write(cpp_template.render(template_context)) |
| 303 h_file.close() | 328 h_file.close() |
| 304 cpp_file.close() | 329 cpp_file.close() |
| 305 | 330 |
| 306 | 331 |
| 307 jinja_env = initialize_jinja_env(output_dirname) | 332 jinja_env = initialize_jinja_env(output_dirname) |
| 308 generate("Backend") | |
| 309 generate("Dispatcher") | 333 generate("Dispatcher") |
| 310 generate("Frontend") | |
| 311 generate("TypeBuilder") | 334 generate("TypeBuilder") |
| OLD | NEW |