OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (C) 2013 Google Inc. All rights reserved. | 2 # Copyright (C) 2013 Google Inc. All rights reserved. |
3 # | 3 # |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
(...skipping 12 matching lines...) Expand all Loading... |
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 import sys | 30 import sys |
31 | 31 |
32 import hasher | 32 import hasher |
33 import in_generator | 33 import json5_generator |
34 import name_utilities | 34 import name_utilities |
35 import template_expander | 35 import template_expander |
36 | 36 |
37 from in_file import InFile | 37 from json5_generator import Json5File |
38 | 38 |
39 | 39 |
40 def _symbol(entry): | 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('-', '_') | 41 return entry['name'].replace('-', '_') |
45 | 42 |
46 | 43 |
47 class MakeQualifiedNamesWriter(in_generator.Writer): | 44 class MakeQualifiedNamesWriter(json5_generator.Writer): |
48 defaults = { | 45 default_parameters = {} |
49 } | 46 default_metadata = { |
50 default_parameters = { | |
51 'attrsNullNamespace': None, | 47 'attrsNullNamespace': None, |
52 'export': '', | 48 'export': '', |
53 'namespace': '', | 49 'namespace': '', |
54 'namespacePrefix': '', | 50 'namespacePrefix': '', |
55 'namespaceURI': '', | 51 'namespaceURI': '', |
56 } | 52 } |
57 filters = { | 53 filters = { |
58 'hash': hasher.hash, | 54 'hash': hasher.hash, |
59 'symbol': _symbol, | 55 'symbol': _symbol, |
60 'to_macro_style': name_utilities.to_macro_style, | 56 'to_macro_style': name_utilities.to_macro_style, |
61 } | 57 } |
62 | 58 |
63 def __init__(self, in_file_paths): | 59 def __init__(self, json5_file_paths): |
64 super(MakeQualifiedNamesWriter, self).__init__(None) | 60 super(MakeQualifiedNamesWriter, self).__init__(None) |
65 assert len(in_file_paths) <= 2, 'MakeQualifiedNamesWriter requires at mo
st 2 in files, got %d.' % len(in_file_paths) | 61 assert len(json5_file_paths) <= 2, 'MakeQualifiedNamesWriter requires at
most 2 in files, got %d.' % len(json5_file_paths) |
66 | 62 |
67 if len(in_file_paths) == 2: | 63 if len(json5_file_paths) == 2: |
68 self.tags_in_file = InFile.load_from_files([in_file_paths.pop(0)], s
elf.defaults, self.valid_values, self.default_parameters) | 64 self.tags_json5_file = Json5File.load_from_files( |
| 65 [json5_file_paths.pop(0)], self.default_metadata, self.default_p
arameters) |
69 else: | 66 else: |
70 self.tags_in_file = None | 67 self.tags_json5_file = None |
71 | 68 |
72 self.attrs_in_file = InFile.load_from_files([in_file_paths.pop()], self.
defaults, self.valid_values, self.default_parameters) | 69 self.attrs_json5_file = Json5File.load_from_files([json5_file_paths.pop(
)], self.default_metadata, self.default_parameters) |
73 | 70 |
74 self.namespace = self._parameter('namespace') | 71 self.namespace = self._metadata('namespace') |
75 | 72 |
76 namespace_prefix = self._parameter('namespacePrefix') or self.namespace.
lower() | 73 namespace_prefix = self._metadata('namespacePrefix') or self.namespace.l
ower() |
77 namespace_uri = self._parameter('namespaceURI') | 74 namespace_uri = self._metadata('namespaceURI') |
78 | 75 |
79 use_namespace_for_attrs = self.attrs_in_file.parameters['attrsNullNamesp
ace'] is None | 76 use_namespace_for_attrs = self.attrs_json5_file.metadata['attrsNullNames
pace'] is None |
80 | 77 |
81 self._outputs = { | 78 self._outputs = { |
82 (self.namespace + "Names.h"): self.generate_header, | 79 (self.namespace + "Names.h"): self.generate_header, |
83 (self.namespace + "Names.cpp"): self.generate_implementation, | 80 (self.namespace + "Names.cpp"): self.generate_implementation, |
84 } | 81 } |
85 self._template_context = { | 82 self._template_context = { |
86 'attrs': self.attrs_in_file.name_dictionaries, | 83 'attrs': self.attrs_json5_file.name_dictionaries, |
87 'export': self._parameter('export'), | 84 'export': self._metadata('export'), |
88 'namespace': self.namespace, | 85 'namespace': self.namespace, |
89 'namespace_prefix': namespace_prefix, | 86 'namespace_prefix': namespace_prefix, |
90 'namespace_uri': namespace_uri, | 87 'namespace_uri': namespace_uri, |
91 'tags': self.tags_in_file.name_dictionaries if self.tags_in_file els
e [], | 88 'tags': self.tags_json5_file.name_dictionaries if self.tags_json5_fi
le else [], |
92 'use_namespace_for_attrs': use_namespace_for_attrs, | 89 'use_namespace_for_attrs': use_namespace_for_attrs, |
93 } | 90 } |
94 | 91 |
95 def _parameter(self, name): | 92 def _metadata(self, name): |
96 parameter = self.attrs_in_file.parameters[name].strip('"') | 93 metadata = self.attrs_json5_file.metadata[name].strip('"') |
97 if self.tags_in_file: | 94 if self.tags_json5_file: |
98 assert parameter == self.tags_in_file.parameters[name].strip('"'), '
Both in files must have the same %s.' % name | 95 assert metadata == self.tags_json5_file.metadata[name].strip('"'), '
Both files must have the same %s.' % name |
99 return parameter | 96 return metadata |
100 | 97 |
101 @template_expander.use_jinja('MakeQualifiedNames.h.tmpl', filters=filters) | 98 @template_expander.use_jinja('MakeQualifiedNames.h.tmpl', filters=filters) |
102 def generate_header(self): | 99 def generate_header(self): |
103 return self._template_context | 100 return self._template_context |
104 | 101 |
105 @template_expander.use_jinja('MakeQualifiedNames.cpp.tmpl', filters=filters) | 102 @template_expander.use_jinja('MakeQualifiedNames.cpp.tmpl', filters=filters) |
106 def generate_implementation(self): | 103 def generate_implementation(self): |
107 return self._template_context | 104 return self._template_context |
108 | 105 |
109 | 106 |
110 if __name__ == "__main__": | 107 if __name__ == "__main__": |
111 in_generator.Maker(MakeQualifiedNamesWriter).main(sys.argv) | 108 json5_generator.Maker(MakeQualifiedNamesWriter).main() |
OLD | NEW |