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 optparse | 7 import optparse |
8 import collections | 8 import collections |
9 import functools | 9 import functools |
10 import re | 10 import re |
11 import copy | |
11 try: | 12 try: |
12 import json | 13 import json |
13 except ImportError: | 14 except ImportError: |
14 import simplejson as json | 15 import simplejson as json |
15 | 16 |
16 # Path handling for libraries and templates | 17 # Path handling for libraries and templates |
17 # Paths have to be normalized because Jinja uses the exact template path to | 18 # Paths have to be normalized because Jinja uses the exact template path to |
18 # determine the hash used in the cache filename, and we need a pre-caching step | 19 # determine the hash used in the cache filename, and we need a pre-caching step |
19 # to be concurrency-safe. Use absolute path because __file__ is absolute if | 20 # to be concurrency-safe. Use absolute path because __file__ is absolute if |
20 # module is imported, and relative if executed directly. | 21 # module is imported, and relative if executed directly. |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
198 domain = domain_json["domain"] | 199 domain = domain_json["domain"] |
199 clear = domain not in exported_domains and domain not in imported_domain s | 200 clear = domain not in exported_domains and domain not in imported_domain s |
200 if not has_exports(domain_json, clear): | 201 if not has_exports(domain_json, clear): |
201 continue | 202 continue |
202 if domain in exported_domains: | 203 if domain in exported_domains: |
203 protocol.exported_domains.append(domain) | 204 protocol.exported_domains.append(domain) |
204 if domain in imported_domains: | 205 if domain in imported_domains: |
205 protocol.imported_domains.append(domain) | 206 protocol.imported_domains.append(domain) |
206 | 207 |
207 | 208 |
208 def create_imported_type_definition(domain_name, type, imported_namespace): | 209 def create_imported_type_definition(domain_name, type_id, imported_namespace): |
209 # pylint: disable=W0622 | 210 # pylint: disable=W0622 |
210 return { | 211 return { |
211 "return_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain_name, type["id"]), | 212 "return_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain_name, type_id), |
212 "pass_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, d omain_name, type["id"]), | 213 "pass_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, d omain_name, type_id), |
213 "to_raw_type": "%s.get()", | 214 "to_raw_type": "%s.get()", |
214 "to_pass_type": "std::move(%s)", | 215 "to_pass_type": "std::move(%s)", |
215 "to_rvalue": "std::move(%s)", | 216 "to_rvalue": "std::move(%s)", |
216 "type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain _name, type["id"]), | 217 "type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain _name, type_id), |
217 "raw_type": "%s::%s::API::%s" % (imported_namespace, domain_name, type[" id"]), | 218 "raw_type": "%s::%s::API::%s" % (imported_namespace, domain_name, type_i d), |
218 "raw_pass_type": "%s::%s::API::%s*" % (imported_namespace, domain_name, type["id"]), | 219 "raw_pass_type": "%s::%s::API::%s*" % (imported_namespace, domain_name, type_id), |
219 "raw_return_type": "%s::%s::API::%s*" % (imported_namespace, domain_name , type["id"]), | 220 "raw_return_type": "%s::%s::API::%s*" % (imported_namespace, domain_name , type_id), |
220 } | 221 } |
221 | 222 |
222 | 223 |
223 def create_user_type_definition(domain_name, type): | 224 def create_user_type_definition(domain_name, type_id): |
224 # pylint: disable=W0622 | 225 # pylint: disable=W0622 |
225 return { | 226 return { |
226 "return_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type[ "id"]), | 227 "return_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type_ id), |
227 "pass_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["i d"]), | 228 "pass_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type_id ), |
228 "to_raw_type": "%s.get()", | 229 "to_raw_type": "%s.get()", |
229 "to_pass_type": "std::move(%s)", | 230 "to_pass_type": "std::move(%s)", |
230 "to_rvalue": "std::move(%s)", | 231 "to_rvalue": "std::move(%s)", |
231 "type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["id"]), | 232 "type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type_id), |
232 "raw_type": "protocol::%s::%s" % (domain_name, type["id"]), | 233 "raw_type": "protocol::%s::%s" % (domain_name, type_id), |
233 "raw_pass_type": "protocol::%s::%s*" % (domain_name, type["id"]), | 234 "raw_pass_type": "protocol::%s::%s*" % (domain_name, type_id), |
234 "raw_return_type": "protocol::%s::%s*" % (domain_name, type["id"]), | 235 "raw_return_type": "protocol::%s::%s*" % (domain_name, type_id), |
235 } | 236 } |
236 | 237 |
237 | 238 |
238 def create_object_type_definition(): | 239 def create_object_type_definition(): |
239 # pylint: disable=W0622 | 240 # pylint: disable=W0622 |
240 return { | 241 return { |
241 "return_type": "std::unique_ptr<protocol::DictionaryValue>", | 242 "return_type": "std::unique_ptr<protocol::DictionaryValue>", |
242 "pass_type": "std::unique_ptr<protocol::DictionaryValue>", | 243 "pass_type": "std::unique_ptr<protocol::DictionaryValue>", |
243 "to_raw_type": "%s.get()", | 244 "to_raw_type": "%s.get()", |
244 "to_pass_type": "std::move(%s)", | 245 "to_pass_type": "std::move(%s)", |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 | 330 |
330 def create_type_definitions(protocol, imported_namespace): | 331 def create_type_definitions(protocol, imported_namespace): |
331 protocol.type_definitions = {} | 332 protocol.type_definitions = {} |
332 protocol.type_definitions["number"] = create_primitive_type_definition("numb er") | 333 protocol.type_definitions["number"] = create_primitive_type_definition("numb er") |
333 protocol.type_definitions["integer"] = create_primitive_type_definition("int eger") | 334 protocol.type_definitions["integer"] = create_primitive_type_definition("int eger") |
334 protocol.type_definitions["boolean"] = create_primitive_type_definition("boo lean") | 335 protocol.type_definitions["boolean"] = create_primitive_type_definition("boo lean") |
335 protocol.type_definitions["object"] = create_object_type_definition() | 336 protocol.type_definitions["object"] = create_object_type_definition() |
336 protocol.type_definitions["any"] = create_any_type_definition() | 337 protocol.type_definitions["any"] = create_any_type_definition() |
337 for domain in protocol.json_api["domains"]: | 338 for domain in protocol.json_api["domains"]: |
338 protocol.type_definitions[domain["domain"] + ".string"] = create_string_ type_definition() | 339 protocol.type_definitions[domain["domain"] + ".string"] = create_string_ type_definition() |
339 if not ("types" in domain): | 340 for type in domain["types"] if "types" in domain else []: |
dgozman
2016/11/21 22:29:16
Advanced python right here!
| |
340 continue | |
341 for type in domain["types"]: | |
342 type_name = domain["domain"] + "." + type["id"] | 341 type_name = domain["domain"] + "." + type["id"] |
343 if type["type"] == "object" and domain["domain"] in protocol.importe d_domains: | 342 if type["type"] == "object" and domain["domain"] in protocol.importe d_domains: |
344 protocol.type_definitions[type_name] = create_imported_type_defi nition(domain["domain"], type, imported_namespace) | 343 protocol.type_definitions[type_name] = create_imported_type_defi nition(domain["domain"], type["id"], imported_namespace) |
345 elif type["type"] == "object": | 344 elif type["type"] == "object": |
346 protocol.type_definitions[type_name] = create_user_type_definiti on(domain["domain"], type) | 345 protocol.type_definitions[type_name] = create_user_type_definiti on(domain["domain"], type["id"]) |
347 elif type["type"] == "array": | 346 elif type["type"] == "array": |
348 items_type = type["items"]["type"] | 347 items_type = type["items"]["type"] |
349 protocol.type_definitions[type_name] = wrap_array_definition(pro tocol.type_definitions[items_type]) | 348 protocol.type_definitions[type_name] = wrap_array_definition(pro tocol.type_definitions[items_type]) |
350 elif type["type"] == domain["domain"] + ".string": | 349 elif type["type"] == domain["domain"] + ".string": |
351 protocol.type_definitions[type_name] = create_string_type_defini tion() | 350 protocol.type_definitions[type_name] = create_string_type_defini tion() |
352 else: | 351 else: |
353 protocol.type_definitions[type_name] = create_primitive_type_def inition(type["type"]) | 352 protocol.type_definitions[type_name] = create_primitive_type_def inition(type["type"]) |
354 | 353 for event in domain["events"] if "events" in domain else []: |
354 type_id = to_title_case(event["name"]) + "Notification" | |
355 type_name = domain["domain"] + "." + type_id | |
356 if domain["domain"] in protocol.imported_domains: | |
357 protocol.type_definitions[type_name] = create_imported_type_defi nition(domain["domain"], type_id, imported_namespace) | |
358 else: | |
359 protocol.type_definitions[type_name] = create_user_type_definiti on(domain["domain"], type_id) | |
355 | 360 |
356 def type_definition(protocol, name): | 361 def type_definition(protocol, name): |
357 return protocol.type_definitions[name] | 362 return protocol.type_definitions[name] |
358 | 363 |
359 | 364 |
360 def resolve_type(protocol, prop): | 365 def resolve_type(protocol, prop): |
361 if "$ref" in prop: | 366 if "$ref" in prop: |
362 return protocol.type_definitions[prop["$ref"]] | 367 return protocol.type_definitions[prop["$ref"]] |
363 if prop["type"] == "array": | 368 if prop["type"] == "array": |
364 return wrap_array_definition(resolve_type(protocol, prop["items"])) | 369 return wrap_array_definition(resolve_type(protocol, prop["items"])) |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 | 499 |
495 h_template = jinja_env.get_template("templates/TypeBuilder_h.template") | 500 h_template = jinja_env.get_template("templates/TypeBuilder_h.template") |
496 cpp_template = jinja_env.get_template("templates/TypeBuilder_cpp.template") | 501 cpp_template = jinja_env.get_template("templates/TypeBuilder_cpp.template") |
497 exported_template = jinja_env.get_template("templates/Exported_h.template") | 502 exported_template = jinja_env.get_template("templates/Exported_h.template") |
498 imported_template = jinja_env.get_template("templates/Imported_h.template") | 503 imported_template = jinja_env.get_template("templates/Imported_h.template") |
499 | 504 |
500 outputs = dict() | 505 outputs = dict() |
501 | 506 |
502 for domain in protocol.json_api["domains"]: | 507 for domain in protocol.json_api["domains"]: |
503 class_name = domain["domain"] | 508 class_name = domain["domain"] |
509 if "events" in domain: | |
510 for event in domain["events"]: | |
511 event_type = dict() | |
512 event_type["description"] = "Wrapper for notification params" | |
513 event_type["type"] = "object" | |
514 event_type["id"] = to_title_case(event["name"]) + "Notification" | |
515 if "parameters" in event: | |
516 event_type["properties"] = copy.deepcopy(event["parameters"] ) | |
517 if "types" not in domain: | |
518 domain["types"] = list() | |
519 domain["types"].append(event_type) | |
520 | |
504 template_context = { | 521 template_context = { |
505 "config": config, | 522 "config": config, |
506 "domain": domain, | 523 "domain": domain, |
507 "join_arrays": join_arrays, | 524 "join_arrays": join_arrays, |
508 "resolve_type": functools.partial(resolve_type, protocol), | 525 "resolve_type": functools.partial(resolve_type, protocol), |
509 "type_definition": functools.partial(type_definition, protocol), | 526 "type_definition": functools.partial(type_definition, protocol), |
510 "generate_command": functools.partial(generate_command, protocol, co nfig), | 527 "generate_command": functools.partial(generate_command, protocol, co nfig), |
511 "generate_event": functools.partial(generate_event, protocol, config ), | 528 "generate_event": functools.partial(generate_event, protocol, config ), |
512 "is_async_command": functools.partial(is_async_command, protocol, co nfig), | 529 "is_async_command": functools.partial(is_async_command, protocol, co nfig), |
513 "generate_disable": functools.partial(generate_disable, protocol, co nfig), | 530 "generate_disable": functools.partial(generate_disable, protocol, co nfig), |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
580 if up_to_date: | 597 if up_to_date: |
581 sys.exit() | 598 sys.exit() |
582 | 599 |
583 for file_name, content in outputs.iteritems(): | 600 for file_name, content in outputs.iteritems(): |
584 out_file = open(file_name, "w") | 601 out_file = open(file_name, "w") |
585 out_file.write(content) | 602 out_file.write(content) |
586 out_file.close() | 603 out_file.close() |
587 | 604 |
588 | 605 |
589 main() | 606 main() |
OLD | NEW |