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

Side by Side Diff: Source/bindings/scripts/interface_merger.py

Issue 15959019: Rewrite generate-bindings.pl in Python (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « Source/bindings/scripts/idl-to-json.pl ('k') | Source/bindings/scripts/semantic_analyzer.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright (C) 2013 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 # import blink_idl_parser # FIXME: not checked in yet
31 import blink_idl_parser_perl
32 import os.path
33 import shlex
34
35
36 class IdlNotFoundError(Exception):
37 # Raised if can't find IDL in dependencies file or additional files list
38 pass
39
40
41 class InterfaceNotFoundError(Exception):
42 # Raised if (partial) interface not found in target
43 pass
44
45
46 class InvalidDependencyError(Exception):
47 # Raised if a supplementary file is not in fact a dependency
48 pass
49
50
51 def parse_file(target_idl_file, defines, preprocessor, use_perl_parser, verbose) :
52 """Wrapper function so can switch parser between Perl and Python"""
53 if use_perl_parser:
54 return blink_idl_parser_perl.parse_file(target_idl_file, defines, prepro cessor)
55 else:
56 if verbose: # otherwise 'verbose' not used
57 print 'Not implemented yet'
58 # parser = BlinkIDLParser(verbose=verbose)
59 # # FIXME: actually "preprocess and parse"
60 # return parser.Parse(target_idl_file, defines, preprocessor)
61
62
63 def compute_supplementary_idl_files(target_idl_basename, supplementary_dependenc ies_filename, additional_idl_files_string):
64 # The format of a supplemental dependency file:
65 #
66 # DOMWindow.idl P.idl Q.idl R.idl
67 # Document.idl S.idl
68 # Event.idl
69 # ...
70 #
71 # The above indicates that DOMWindow.idl is supplemented by P.idl, Q.idl and R.idl,
72 # Document.idl is supplemented by S.idl, and Event.idl is supplemented by no IDLs.
73 # The IDL that supplements another IDL (e.g. P.idl) never appears in the dep endency file.
74 supplementary_idl_files = None
75 with open(supplementary_dependencies_filename) as supplementary_dependencies _file:
76 for line in supplementary_dependencies_file:
77 idl_filename, _, dependency_files = line.partition(' ')
78 if os.path.basename(idl_filename) == target_idl_basename:
79 supplementary_idl_files = dependency_files.split()
80
81 if supplementary_idl_files is None:
82 # additional_idl_fpiles is list of IDL files which should not be include d in
83 # DerivedSources*.cpp (i.e. they are not described in the supplemental
84 # dependency file) but should generate .h and .cpp files.
85 additional_idl_files_list = shlex.split(additional_idl_files_string)
86 for additional_idl_filename in additional_idl_files_list:
87 if os.path.basename(additional_idl_filename) == target_idl_basename:
88 break
89 else:
90 raise IdlNotFoundError
91 return supplementary_idl_files
92
93
94 def merge_partial_interface(target_document_interfaces, partial_interface, targe t_interface_name, interface_name):
95 """Merge partial_interface into target_document_interfaces.
96
97 No return: modifies target_document_interfaces in place.
98 """
99 # FIXME: more elegant would be if 'interfaces' were a dict, rather
100 # than AST, so we could skip the search and just do:
101 # target_data_node = target_document['interfaces'][target_interface_name]
102 for target_interface in target_document_interfaces:
103 if target_interface['name'] == target_interface_name:
104 target_data_node = target_interface
105 break
106 else:
107 raise InterfaceNotFoundError('Could not find interface "{target_inte rface_name}" in {target_interface_name}.idl.'.format(**locals))
108
109 for attribute in partial_interface['attributes']:
110 attribute['signature']['extendedAttributes']['ImplementedBy'] = interfac e_name
111 # Add interface-wide extended attributes to each attribute.
112 for extended_attribute_name, extended_attribute_value in partial_interfa ce['extendedAttributes'].iteritems():
113 attribute['signature']['extendedAttributes'][extended_attribute_name ] = extended_attribute_value
114 target_data_node['attributes'].append(attribute)
115
116 for function in partial_interface['functions']:
117 function['signature']['extendedAttributes']['ImplementedBy'] = interface _name
118 # Add interface-wide extended attributes to each method.
119 for extended_attribute_name, extended_attribute_value in partial_interfa ce['extendedAttributes'].iteritems():
120 function['signature']['extendedAttributes'][extended_attribute_name] = extended_attribute_value
121 target_data_node['functions'].append(function)
122
123 for constant in partial_interface['constants']:
124 constant['extendedAttributes']['ImplementedBy'] = interface_name
125 # Add interface-wide extended attributes to each constant.
126 for extended_attribute_name, extended_attribute_value in partial_interfa ce['extendedAttributes'].iteritems():
127 constant['extendedAttributes'][extended_attribute_name] = extended_a ttribute_value
128 target_data_node['constants'].append(constant)
129
130 # Replace interface with augmented one
131 for i, target_interface in enumerate(target_document_interfaces):
132 if target_interface['name'] == target_interface_name:
133 target_document_interfaces[i] = target_data_node
134
135
136 def merge_partial_interfaces(target_document, target_interface_name, target_idl_ file, supplementary_idl_files, options):
137 """Merge partial interfaces in supplementary_idl_files into target_document.
138
139 No return: modifies target_document in place.
140 """
141 for idl_file in supplementary_idl_files:
142 if idl_file == target_idl_file:
143 # FIXME: this should never happen; it's a circular dependency!
144 continue
145
146 interface_name, _ = os.path.splitext(os.path.basename(idl_file))
147 document = parse_file(idl_file, options.defines, options.preprocessor, o ptions.perl_parser, options.verbose)
148
149 for interface in document['interfaces']:
150 # Supplementary files must contain *only* partial interfaces
151 # for the single target interface
152 if not(interface['isPartial'] and interface['name'] == target_interf ace_name):
153 raise InvalidDependencyError('%(idl_file) is not a supplementary dependency of %(target_idl_file). There maybe a bug in the the supplementary de pendency generator (preprocess_idls.py).')
154
155 merge_partial_interface(target_document['interfaces'], interface, ta rget_interface_name, interface_name)
OLDNEW
« no previous file with comments | « Source/bindings/scripts/idl-to-json.pl ('k') | Source/bindings/scripts/semantic_analyzer.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698