Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/env 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 sys
     31 
     32 import hasher
     33 import in_generator
     34 import name_utilities
     35 import template_expander
     36 
     37 from in_file import InFile
     38 
     39 
     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('-', '_')
     45 
     46 
     47 class MakeQualifiedNamesWriter(in_generator.Writer):
     48     defaults = {
     49     }
     50     default_parameters = {
     51         'attrsNullNamespace': None,
     52         'namespace': '',
     53         'namespacePrefix': '',
     54         'namespaceURI': '',
     55     }
     56     filters = {
     57         'hash': hasher.hash,
     58         'enable_conditional': name_utilities.enable_conditional_if_endif,
     59         'symbol': _symbol,
     60         'to_macro_style': name_utilities.to_macro_style,
     61     }
     62 
     63     def __init__(self, in_file_paths):
     64         super(MakeQualifiedNamesWriter, self).__init__(None)
     65         assert len(in_file_paths) <= 2, 'MakeQualifiedNamesWriter requires at most 2 in files, got %d.' % len(in_file_paths)
     66 
     67         if len(in_file_paths) == 2:
     68             self.tags_in_file = InFile.load_from_files([in_file_paths.pop(0)], self.defaults, self.valid_values, self.default_parameters)
     69         else:
     70             self.tags_in_file = None
     71 
     72         self.attrs_in_file = InFile.load_from_files([in_file_paths.pop()], self.defaults, self.valid_values, self.default_parameters)
     73 
     74         self.namespace = self._parameter('namespace')
     75 
     76         namespace_prefix = self._parameter('namespacePrefix') or self.namespace.lower()
     77         namespace_uri = self._parameter('namespaceURI')
     78 
     79         use_namespace_for_attrs = self.attrs_in_file.parameters['attrsNullNamespace'] is None
     80 
     81         self._outputs = {
     82             (self.namespace + "Names.h"): self.generate_header,
     83             (self.namespace + "Names.cpp"): self.generate_implementation,
     84         }
     85         self._template_context = {
     86             'namespace': self.namespace,
     87             'namespace_prefix': namespace_prefix,
     88             'namespace_uri': namespace_uri,
     89             'use_namespace_for_attrs': use_namespace_for_attrs,
     90             'tags': self.tags_in_file.name_dictionaries if self.tags_in_file else [],
     91             'attrs': self.attrs_in_file.name_dictionaries,
     92         }
     93 
     94     def _parameter(self, name):
     95         parameter = self.attrs_in_file.parameters[name].strip('"')
     96         if self.tags_in_file:
     97             assert parameter == self.tags_in_file.parameters[name].strip('"'), 'Both in files must have the same %s.' % name
     98         return parameter
     99 
    100     @template_expander.use_jinja('MakeQualifiedNames.h.tmpl', filters=filters)
    101     def generate_header(self):
    102         return self._template_context
    103 
    104     @template_expander.use_jinja('MakeQualifiedNames.cpp.tmpl', filters=filters)
    105     def generate_implementation(self):
    106         return self._template_context
    107 
    108 
    109 if __name__ == "__main__":
    110     in_generator.Maker(MakeQualifiedNamesWriter).main(sys.argv)
    111