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 try: | 10 try: |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 config_partial = json_to_object(config_json_string, output_base, config_
base) | 75 config_partial = json_to_object(config_json_string, output_base, config_
base) |
76 config_json_file.close() | 76 config_json_file.close() |
77 defaults = { | 77 defaults = { |
78 ".imported": False, | 78 ".imported": False, |
79 ".imported.export_macro": "", | 79 ".imported.export_macro": "", |
80 ".imported.export_header": False, | 80 ".imported.export_header": False, |
81 ".imported.header": False, | 81 ".imported.header": False, |
82 ".imported.package": False, | 82 ".imported.package": False, |
83 ".protocol.export_macro": "", | 83 ".protocol.export_macro": "", |
84 ".protocol.export_header": False, | 84 ".protocol.export_header": False, |
| 85 ".protocol.options": False, |
85 ".exported": False, | 86 ".exported": False, |
86 ".exported.export_macro": "", | 87 ".exported.export_macro": "", |
87 ".exported.export_header": False, | 88 ".exported.export_header": False, |
88 ".lib": False, | 89 ".lib": False, |
89 ".lib.export_macro": "", | 90 ".lib.export_macro": "", |
90 ".lib.export_header": False, | 91 ".lib.export_header": False, |
91 } | 92 } |
92 return (jinja_dir, config_file, init_defaults(config_partial, "", defaul
ts)) | 93 return (jinja_dir, config_file, init_defaults(config_partial, "", defaul
ts)) |
93 except Exception: | 94 except Exception: |
94 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.h
tml | 95 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.h
tml |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 | 331 |
331 | 332 |
332 def join_arrays(dict, keys): | 333 def join_arrays(dict, keys): |
333 result = [] | 334 result = [] |
334 for key in keys: | 335 for key in keys: |
335 if key in dict: | 336 if key in dict: |
336 result += dict[key] | 337 result += dict[key] |
337 return result | 338 return result |
338 | 339 |
339 | 340 |
340 def has_disable(commands): | 341 def generate_command(protocol, config, domain, command): |
341 for command in commands: | 342 if not config.protocol.options: |
342 if command["name"] == "disable" and (not ("handlers" in command) or "ren
derer" in command["handlers"]): | 343 return domain in protocol.generate_domains |
343 return True | 344 for rule in config.protocol.options: |
| 345 if rule.domain != domain: |
| 346 continue |
| 347 if hasattr(rule, "include"): |
| 348 return command in rule.include |
| 349 if hasattr(rule, "exclude"): |
| 350 return command not in rule.exclude |
| 351 return True |
344 return False | 352 return False |
345 | 353 |
346 | 354 |
| 355 def generate_event(protocol, config, domain, event): |
| 356 if not config.protocol.options: |
| 357 return domain in protocol.generate_domains |
| 358 for rule in config.protocol.options: |
| 359 if rule.domain != domain: |
| 360 continue |
| 361 if hasattr(rule, "include_events"): |
| 362 return event in rule.include_events |
| 363 if hasattr(rule, "exclude_events"): |
| 364 return event not in rule.exclude_events |
| 365 return True |
| 366 return False |
| 367 |
| 368 |
| 369 def is_async_command(protocol, config, domain, command): |
| 370 if not config.protocol.options: |
| 371 return False |
| 372 for rule in config.protocol.options: |
| 373 if rule.domain != domain: |
| 374 continue |
| 375 if hasattr(rule, "async"): |
| 376 return command in rule.async |
| 377 return False |
| 378 return False |
| 379 |
| 380 |
| 381 def generate_disable(protocol, config, domain): |
| 382 if "commands" not in domain: |
| 383 return True |
| 384 for command in domain["commands"]: |
| 385 if command["name"] == "disable" and generate_command(protocol, config, d
omain["domain"], "disable"): |
| 386 return False |
| 387 return True |
| 388 |
| 389 |
347 def format_include(header): | 390 def format_include(header): |
348 return "\"" + header + "\"" if header[0] not in "<\"" else header | 391 return "\"" + header + "\"" if header[0] not in "<\"" else header |
349 | 392 |
350 | 393 |
351 def read_protocol_file(file_name, json_api): | 394 def read_protocol_file(file_name, json_api): |
352 input_file = open(file_name, "r") | 395 input_file = open(file_name, "r") |
353 json_string = input_file.read() | 396 json_string = input_file.read() |
354 input_file.close() | 397 input_file.close() |
355 parsed_json = json.loads(json_string) | 398 parsed_json = json.loads(json_string) |
356 version = parsed_json["version"]["major"] + "." + parsed_json["version"]["mi
nor"] | 399 version = parsed_json["version"]["major"] + "." + parsed_json["version"]["mi
nor"] |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 outputs = dict() | 455 outputs = dict() |
413 | 456 |
414 for domain in protocol.json_api["domains"]: | 457 for domain in protocol.json_api["domains"]: |
415 class_name = domain["domain"] | 458 class_name = domain["domain"] |
416 template_context = { | 459 template_context = { |
417 "config": config, | 460 "config": config, |
418 "domain": domain, | 461 "domain": domain, |
419 "join_arrays": join_arrays, | 462 "join_arrays": join_arrays, |
420 "resolve_type": functools.partial(resolve_type, protocol), | 463 "resolve_type": functools.partial(resolve_type, protocol), |
421 "type_definition": functools.partial(type_definition, protocol), | 464 "type_definition": functools.partial(type_definition, protocol), |
422 "has_disable": has_disable, | 465 "generate_command": functools.partial(generate_command, protocol, co
nfig), |
| 466 "generate_event": functools.partial(generate_event, protocol, config
), |
| 467 "is_async_command": functools.partial(is_async_command, protocol, co
nfig), |
| 468 "generate_disable": functools.partial(generate_disable, protocol, co
nfig), |
423 "format_include": format_include | 469 "format_include": format_include |
424 } | 470 } |
425 | 471 |
426 if domain["domain"] in protocol.generate_domains: | 472 if domain["domain"] in protocol.generate_domains: |
427 outputs[os.path.join(config.protocol.output, class_name + ".h")] = h
_template.render(template_context) | 473 outputs[os.path.join(config.protocol.output, class_name + ".h")] = h
_template.render(template_context) |
428 outputs[os.path.join(config.protocol.output, class_name + ".cpp")] =
cpp_template.render(template_context) | 474 outputs[os.path.join(config.protocol.output, class_name + ".cpp")] =
cpp_template.render(template_context) |
429 if domain["has_exports"]: | 475 if domain["has_exports"]: |
430 outputs[os.path.join(config.exported.output, class_name + ".h")]
= exported_template.render(template_context) | 476 outputs[os.path.join(config.exported.output, class_name + ".h")]
= exported_template.render(template_context) |
431 if domain["domain"] in protocol.imported_domains and domain["has_exports
"]: | 477 if domain["domain"] in protocol.imported_domains and domain["has_exports
"]: |
432 outputs[os.path.join(config.protocol.output, class_name + ".h")] = i
mported_template.render(template_context) | 478 outputs[os.path.join(config.protocol.output, class_name + ".h")] = i
mported_template.render(template_context) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 if up_to_date: | 535 if up_to_date: |
490 sys.exit() | 536 sys.exit() |
491 | 537 |
492 for file_name, content in outputs.iteritems(): | 538 for file_name, content in outputs.iteritems(): |
493 out_file = open(file_name, "w") | 539 out_file = open(file_name, "w") |
494 out_file.write(content) | 540 out_file.write(content) |
495 out_file.close() | 541 out_file.close() |
496 | 542 |
497 | 543 |
498 main() | 544 main() |
OLD | NEW |