Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / build / scripts / make_qualified_names.py
blobf93e83e6ad26c439a7308aa16baf1e3ed32caca2
1 #!/usr/bin/env python
2 # Copyright (C) 2013 Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
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.
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.
30 import sys
32 import hasher
33 import in_generator
34 import name_utilities
35 import template_expander
37 from in_file import InFile
40 def _symbol(entry):
41 # FIXME: Remove this special case for the ugly x-webkit-foo attributes.
42 if entry['name'].startswith('x-webkit-'):
43 return entry['name'].replace('-', '')[1:]
44 return entry['name'].replace('-', '_')
47 class MakeQualifiedNamesWriter(in_generator.Writer):
48 defaults = {
50 default_parameters = {
51 'attrsNullNamespace': None,
52 'export': '',
53 'namespace': '',
54 'namespacePrefix': '',
55 'namespaceURI': '',
57 filters = {
58 'hash': hasher.hash,
59 'enable_conditional': name_utilities.enable_conditional_if_endif,
60 'symbol': _symbol,
61 'to_macro_style': name_utilities.to_macro_style,
64 def __init__(self, in_file_paths):
65 super(MakeQualifiedNamesWriter, self).__init__(None)
66 assert len(in_file_paths) <= 2, 'MakeQualifiedNamesWriter requires at most 2 in files, got %d.' % len(in_file_paths)
68 if len(in_file_paths) == 2:
69 self.tags_in_file = InFile.load_from_files([in_file_paths.pop(0)], self.defaults, self.valid_values, self.default_parameters)
70 else:
71 self.tags_in_file = None
73 self.attrs_in_file = InFile.load_from_files([in_file_paths.pop()], self.defaults, self.valid_values, self.default_parameters)
75 self.namespace = self._parameter('namespace')
77 namespace_prefix = self._parameter('namespacePrefix') or self.namespace.lower()
78 namespace_uri = self._parameter('namespaceURI')
80 use_namespace_for_attrs = self.attrs_in_file.parameters['attrsNullNamespace'] is None
82 self._outputs = {
83 (self.namespace + "Names.h"): self.generate_header,
84 (self.namespace + "Names.cpp"): self.generate_implementation,
86 self._template_context = {
87 'attrs': self.attrs_in_file.name_dictionaries,
88 'export': self._parameter('export'),
89 'namespace': self.namespace,
90 'namespace_prefix': namespace_prefix,
91 'namespace_uri': namespace_uri,
92 'tags': self.tags_in_file.name_dictionaries if self.tags_in_file else [],
93 'use_namespace_for_attrs': use_namespace_for_attrs,
96 def _parameter(self, name):
97 parameter = self.attrs_in_file.parameters[name].strip('"')
98 if self.tags_in_file:
99 assert parameter == self.tags_in_file.parameters[name].strip('"'), 'Both in files must have the same %s.' % name
100 return parameter
102 @template_expander.use_jinja('MakeQualifiedNames.h.tmpl', filters=filters)
103 def generate_header(self):
104 return self._template_context
106 @template_expander.use_jinja('MakeQualifiedNames.cpp.tmpl', filters=filters)
107 def generate_implementation(self):
108 return self._template_context
111 if __name__ == "__main__":
112 in_generator.Maker(MakeQualifiedNamesWriter).main(sys.argv)