| 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 json5_generator | 33 import in_generator |
| 34 import name_utilities | 34 import name_utilities |
| 35 import template_expander | 35 import template_expander |
| 36 | 36 |
| 37 from json5_generator import Json5File | 37 from in_file import InFile |
| 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:] |
| 41 return entry['name'].replace('-', '_') | 44 return entry['name'].replace('-', '_') |
| 42 | 45 |
| 43 | 46 |
| 44 class MakeQualifiedNamesWriter(json5_generator.Writer): | 47 class MakeQualifiedNamesWriter(in_generator.Writer): |
| 45 default_parameters = {} | 48 defaults = { |
| 46 default_metadata = { | 49 } |
| 50 default_parameters = { |
| 47 'attrsNullNamespace': None, | 51 'attrsNullNamespace': None, |
| 48 'export': '', | 52 'export': '', |
| 49 'namespace': '', | 53 'namespace': '', |
| 50 'namespacePrefix': '', | 54 'namespacePrefix': '', |
| 51 'namespaceURI': '', | 55 'namespaceURI': '', |
| 52 } | 56 } |
| 53 filters = { | 57 filters = { |
| 54 'hash': hasher.hash, | 58 'hash': hasher.hash, |
| 55 'symbol': _symbol, | 59 'symbol': _symbol, |
| 56 'to_macro_style': name_utilities.to_macro_style, | 60 'to_macro_style': name_utilities.to_macro_style, |
| 57 } | 61 } |
| 58 | 62 |
| 59 def __init__(self, json5_file_paths): | 63 def __init__(self, in_file_paths): |
| 60 super(MakeQualifiedNamesWriter, self).__init__(None) | 64 super(MakeQualifiedNamesWriter, self).__init__(None) |
| 61 assert len(json5_file_paths) <= 2, 'MakeQualifiedNamesWriter requires at
most 2 in files, got %d.' % len(json5_file_paths) | 65 assert len(in_file_paths) <= 2, 'MakeQualifiedNamesWriter requires at mo
st 2 in files, got %d.' % len(in_file_paths) |
| 62 | 66 |
| 63 if len(json5_file_paths) == 2: | 67 if len(in_file_paths) == 2: |
| 64 self.tags_json5_file = Json5File.load_from_files( | 68 self.tags_in_file = InFile.load_from_files([in_file_paths.pop(0)], s
elf.defaults, self.valid_values, self.default_parameters) |
| 65 [json5_file_paths.pop(0)], self.default_metadata, self.default_p
arameters) | |
| 66 else: | 69 else: |
| 67 self.tags_json5_file = None | 70 self.tags_in_file = None |
| 68 | 71 |
| 69 self.attrs_json5_file = Json5File.load_from_files([json5_file_paths.pop(
)], self.default_metadata, self.default_parameters) | 72 self.attrs_in_file = InFile.load_from_files([in_file_paths.pop()], self.
defaults, self.valid_values, self.default_parameters) |
| 70 | 73 |
| 71 self.namespace = self._metadata('namespace') | 74 self.namespace = self._parameter('namespace') |
| 72 | 75 |
| 73 namespace_prefix = self._metadata('namespacePrefix') or self.namespace.l
ower() | 76 namespace_prefix = self._parameter('namespacePrefix') or self.namespace.
lower() |
| 74 namespace_uri = self._metadata('namespaceURI') | 77 namespace_uri = self._parameter('namespaceURI') |
| 75 | 78 |
| 76 use_namespace_for_attrs = self.attrs_json5_file.metadata['attrsNullNames
pace'] is None | 79 use_namespace_for_attrs = self.attrs_in_file.parameters['attrsNullNamesp
ace'] is None |
| 77 | 80 |
| 78 self._outputs = { | 81 self._outputs = { |
| 79 (self.namespace + "Names.h"): self.generate_header, | 82 (self.namespace + "Names.h"): self.generate_header, |
| 80 (self.namespace + "Names.cpp"): self.generate_implementation, | 83 (self.namespace + "Names.cpp"): self.generate_implementation, |
| 81 } | 84 } |
| 82 self._template_context = { | 85 self._template_context = { |
| 83 'attrs': self.attrs_json5_file.name_dictionaries, | 86 'attrs': self.attrs_in_file.name_dictionaries, |
| 84 'export': self._metadata('export'), | 87 'export': self._parameter('export'), |
| 85 'namespace': self.namespace, | 88 'namespace': self.namespace, |
| 86 'namespace_prefix': namespace_prefix, | 89 'namespace_prefix': namespace_prefix, |
| 87 'namespace_uri': namespace_uri, | 90 'namespace_uri': namespace_uri, |
| 88 'tags': self.tags_json5_file.name_dictionaries if self.tags_json5_fi
le else [], | 91 'tags': self.tags_in_file.name_dictionaries if self.tags_in_file els
e [], |
| 89 'use_namespace_for_attrs': use_namespace_for_attrs, | 92 'use_namespace_for_attrs': use_namespace_for_attrs, |
| 90 } | 93 } |
| 91 | 94 |
| 92 def _metadata(self, name): | 95 def _parameter(self, name): |
| 93 metadata = self.attrs_json5_file.metadata[name].strip('"') | 96 parameter = self.attrs_in_file.parameters[name].strip('"') |
| 94 if self.tags_json5_file: | 97 if self.tags_in_file: |
| 95 assert metadata == self.tags_json5_file.metadata[name].strip('"'), '
Both files must have the same %s.' % name | 98 assert parameter == self.tags_in_file.parameters[name].strip('"'), '
Both in files must have the same %s.' % name |
| 96 return metadata | 99 return parameter |
| 97 | 100 |
| 98 @template_expander.use_jinja('MakeQualifiedNames.h.tmpl', filters=filters) | 101 @template_expander.use_jinja('MakeQualifiedNames.h.tmpl', filters=filters) |
| 99 def generate_header(self): | 102 def generate_header(self): |
| 100 return self._template_context | 103 return self._template_context |
| 101 | 104 |
| 102 @template_expander.use_jinja('MakeQualifiedNames.cpp.tmpl', filters=filters) | 105 @template_expander.use_jinja('MakeQualifiedNames.cpp.tmpl', filters=filters) |
| 103 def generate_implementation(self): | 106 def generate_implementation(self): |
| 104 return self._template_context | 107 return self._template_context |
| 105 | 108 |
| 106 | 109 |
| 107 if __name__ == "__majson5__": | 110 if __name__ == "__main__": |
| 108 json5_generator.Maker(MakeQualifiedNamesWriter).main() | 111 in_generator.Maker(MakeQualifiedNamesWriter).main(sys.argv) |
| OLD | NEW |