Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: third_party/WebKit/Source/platform/inspector_protocol/CodeGenerator.py

Issue 2296043004: [DevTools] Various tweaks to inspector_protocol. (Closed)
Patch Set: optional exports Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 14 matching lines...) Expand all
25 def read_config(): 25 def read_config():
26 # pylint: disable=W0703 26 # pylint: disable=W0703
27 def json_to_object(data, output_base, config_base): 27 def json_to_object(data, output_base, config_base):
28 def json_object_hook(object_dict): 28 def json_object_hook(object_dict):
29 items = [(k, os.path.join(config_base, v) if k == "path" else v) for (k, v) in object_dict.items()] 29 items = [(k, os.path.join(config_base, v) if k == "path" else v) for (k, v) in object_dict.items()]
30 items = [(k, os.path.join(output_base, v) if k == "output" else v) f or (k, v) in items] 30 items = [(k, os.path.join(output_base, v) if k == "output" else v) f or (k, v) in items]
31 keys, values = zip(*items) 31 keys, values = zip(*items)
32 return collections.namedtuple('X', keys)(*values) 32 return collections.namedtuple('X', keys)(*values)
33 return json.loads(data, object_hook=json_object_hook) 33 return json.loads(data, object_hook=json_object_hook)
34 34
35 def init_defaults(config_tuple, path, defaults):
36 keys = list(config_tuple._fields) # pylint: disable=E1101
37 values = [getattr(config_tuple, k) for k in keys]
38 for i in xrange(len(keys)):
39 if hasattr(values[i], "_fields"):
40 values[i] = init_defaults(values[i], path + "." + keys[i], defau lts)
41 for optional in defaults:
42 if optional.find(path + ".") != 0:
43 continue
44 optional_key = optional[len(path) + 1:]
45 if optional_key.find(".") == -1 and optional_key not in keys:
46 keys.append(optional_key)
47 values.append(defaults[optional])
48 return collections.namedtuple('X', keys)(*values)
49
35 try: 50 try:
36 cmdline_parser = optparse.OptionParser() 51 cmdline_parser = optparse.OptionParser()
37 cmdline_parser.add_option("--output_base") 52 cmdline_parser.add_option("--output_base")
38 cmdline_parser.add_option("--jinja_dir") 53 cmdline_parser.add_option("--jinja_dir")
39 cmdline_parser.add_option("--config") 54 cmdline_parser.add_option("--config")
40 arg_options, _ = cmdline_parser.parse_args() 55 arg_options, _ = cmdline_parser.parse_args()
41 jinja_dir = arg_options.jinja_dir 56 jinja_dir = arg_options.jinja_dir
42 if not jinja_dir: 57 if not jinja_dir:
43 raise Exception("jinja directory must be specified") 58 raise Exception("jinja directory must be specified")
44 output_base = arg_options.output_base 59 output_base = arg_options.output_base
45 if not output_base: 60 if not output_base:
46 raise Exception("Base output directory must be specified") 61 raise Exception("Base output directory must be specified")
47 config_file = arg_options.config 62 config_file = arg_options.config
48 if not config_file: 63 if not config_file:
49 raise Exception("Config file name must be specified") 64 raise Exception("Config file name must be specified")
50 config_base = os.path.dirname(config_file) 65 config_base = os.path.dirname(config_file)
51 except Exception: 66 except Exception:
52 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.h tml 67 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.h tml
53 exc = sys.exc_info()[1] 68 exc = sys.exc_info()[1]
54 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc) 69 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc)
55 exit(1) 70 exit(1)
56 71
57 try: 72 try:
58 config_json_file = open(config_file, "r") 73 config_json_file = open(config_file, "r")
59 config_json_string = config_json_file.read() 74 config_json_string = config_json_file.read()
60 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)
61 keys = list(config_partial._fields) # pylint: disable=E1101
62 values = [getattr(config_partial, k) for k in keys]
63 for optional in ["imported", "exported", "lib"]:
64 if optional not in keys:
65 keys.append(optional)
66 values.append(False)
67 config_json_file.close() 76 config_json_file.close()
68 return (jinja_dir, config_file, collections.namedtuple('X', keys)(*value s)) 77 defaults = {
78 ".imported": False,
79 ".imported.export_macro": "",
80 ".imported.export_header": False,
81 ".imported.header": False,
82 ".imported.package": False,
83 ".protocol.export_macro": "",
84 ".protocol.export_header": False,
85 ".exported": False,
86 ".exported.export_macro": "",
87 ".exported.export_header": False,
88 ".lib": False,
89 ".lib.export_macro": "",
90 ".lib.export_header": False,
91 }
92 return (jinja_dir, config_file, init_defaults(config_partial, "", defaul ts))
69 except Exception: 93 except Exception:
70 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.h tml 94 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.h tml
71 exc = sys.exc_info()[1] 95 exc = sys.exc_info()[1]
72 sys.stderr.write("Failed to parse config file: %s\n\n" % exc) 96 sys.stderr.write("Failed to parse config file: %s\n\n" % exc)
73 exit(1) 97 exit(1)
74 98
75 99
76 def to_title_case(name): 100 def to_title_case(name):
77 return name[:1].upper() + name[1:] 101 return name[:1].upper() + name[1:]
78 102
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 return result 337 return result
314 338
315 339
316 def has_disable(commands): 340 def has_disable(commands):
317 for command in commands: 341 for command in commands:
318 if command["name"] == "disable": 342 if command["name"] == "disable":
319 return True 343 return True
320 return False 344 return False
321 345
322 346
347 def format_include(header):
348 return "\"" + header + "\"" if header[0] not in "<\"" else header
349
350
323 def read_protocol_file(file_name, json_api): 351 def read_protocol_file(file_name, json_api):
324 input_file = open(file_name, "r") 352 input_file = open(file_name, "r")
325 json_string = input_file.read() 353 json_string = input_file.read()
326 input_file.close() 354 input_file.close()
327 parsed_json = json.loads(json_string) 355 parsed_json = json.loads(json_string)
328 version = parsed_json["version"]["major"] + "." + parsed_json["version"]["mi nor"] 356 version = parsed_json["version"]["major"] + "." + parsed_json["version"]["mi nor"]
329 domains = [] 357 domains = []
330 for domain in parsed_json["domains"]: 358 for domain in parsed_json["domains"]:
331 domains.append(domain["domain"]) 359 domains.append(domain["domain"])
332 domain["version"] = version 360 domain["version"] = version
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 outputs = dict() 412 outputs = dict()
385 413
386 for domain in protocol.json_api["domains"]: 414 for domain in protocol.json_api["domains"]:
387 class_name = domain["domain"] 415 class_name = domain["domain"]
388 template_context = { 416 template_context = {
389 "config": config, 417 "config": config,
390 "domain": domain, 418 "domain": domain,
391 "join_arrays": join_arrays, 419 "join_arrays": join_arrays,
392 "resolve_type": functools.partial(resolve_type, protocol), 420 "resolve_type": functools.partial(resolve_type, protocol),
393 "type_definition": functools.partial(type_definition, protocol), 421 "type_definition": functools.partial(type_definition, protocol),
394 "has_disable": has_disable 422 "has_disable": has_disable,
423 "format_include": format_include,
395 } 424 }
396 425
397 if domain["domain"] in protocol.generate_domains: 426 if domain["domain"] in protocol.generate_domains:
398 outputs[os.path.join(config.protocol.output, class_name + ".h")] = h _template.render(template_context) 427 outputs[os.path.join(config.protocol.output, class_name + ".h")] = h _template.render(template_context)
399 outputs[os.path.join(config.protocol.output, class_name + ".cpp")] = cpp_template.render(template_context) 428 outputs[os.path.join(config.protocol.output, class_name + ".cpp")] = cpp_template.render(template_context)
400 if domain["has_exports"]: 429 if domain["has_exports"]:
401 outputs[os.path.join(config.exported.output, class_name + ".h")] = exported_template.render(template_context) 430 outputs[os.path.join(config.exported.output, class_name + ".h")] = exported_template.render(template_context)
402 if domain["domain"] in protocol.imported_domains and domain["has_exports "]: 431 if domain["domain"] in protocol.imported_domains and domain["has_exports "]:
403 outputs[os.path.join(config.protocol.output, class_name + ".h")] = i mported_template.render(template_context) 432 outputs[os.path.join(config.protocol.output, class_name + ".h")] = i mported_template.render(template_context)
404 433
405 if config.lib: 434 if config.lib:
406 template_context = { 435 template_context = {
407 "config": config 436 "config": config,
437 "format_include": format_include,
408 } 438 }
409 439
410 lib_templates_dir = os.path.join(module_path, "lib") 440 lib_templates_dir = os.path.join(module_path, "lib")
411 # Note these should be sorted in the right order. 441 # Note these should be sorted in the right order.
412 # TODO(dgozman): sort them programmatically based on commented includes. 442 # TODO(dgozman): sort them programmatically based on commented includes.
413 lib_h_templates = [ 443 lib_h_templates = [
414 "Collections_h.template", 444 "Collections_h.template",
415 "ErrorSupport_h.template", 445 "ErrorSupport_h.template",
416 "Values_h.template", 446 "Values_h.template",
417 "Object_h.template", 447 "Object_h.template",
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 if up_to_date: 490 if up_to_date:
461 sys.exit() 491 sys.exit()
462 492
463 for file_name, content in outputs.iteritems(): 493 for file_name, content in outputs.iteritems():
464 out_file = open(file_name, "w") 494 out_file = open(file_name, "w")
465 out_file.write(content) 495 out_file.write(content)
466 out_file.close() 496 out_file.close()
467 497
468 498
469 main() 499 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698