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         'to_macro_style': name_utilities.to_macro_style,
     59         'symbol': _symbol,
     60     }
     61 
     62     def __init__(self, in_file_paths):
     63         super(MakeQualifiedNamesWriter, self).__init__(None)
     64         assert len(in_file_paths) <= 2, 'MakeQualifiedNamesWriter requires at most 2 in files, got %d.' % len(in_file_paths)
     65 
     66         if len(in_file_paths) == 2:
     67             self.tags_in_file = InFile.load_from_files([in_file_paths.pop(0)], self.defaults, self.valid_values, self.default_parameters)
     68         else:
     69             self.tags_in_file = None
     70 
     71         self.attrs_in_file = InFile.load_from_files([in_file_paths.pop()], self.defaults, self.valid_values, self.default_parameters)
     72 
     73         self.namespace = self._parameter('namespace')
     74 
     75         namespace_prefix = self._parameter('namespacePrefix') or self.namespace.lower()
     76         namespace_uri = self._parameter('namespaceURI')
     77 
     78         use_namespace_for_attrs = self.attrs_in_file.parameters['attrsNullNamespace'] is None
     79 
     80         self._outputs = {
     81             (self.namespace + "Names.h"): self.generate_header,
     82             (self.namespace + "Names.cpp"): self.generate_implementation,
     83         }
     84         self._template_context = {
     85             'namespace': self.namespace,
     86             'namespace_prefix': namespace_prefix,
     87             'namespace_uri': namespace_uri,
     88             'use_namespace_for_attrs': use_namespace_for_attrs,
     89             'tags': self.tags_in_file.name_dictionaries if self.tags_in_file else [],
     90             'attrs': self.attrs_in_file.name_dictionaries,
     91         }
     92 
     93     def _parameter(self, name):
     94         parameter = self.attrs_in_file.parameters[name].strip('"')
     95         if self.tags_in_file:
     96             assert parameter == self.tags_in_file.parameters[name].strip('"'), 'Both in files must have the same %s.' % name
     97         return parameter
     98 
     99     @template_expander.use_jinja('MakeQualifiedNames.h.tmpl', filters=filters)
    100     def generate_header(self):
    101         return self._template_context
    102 
    103     @template_expander.use_jinja('MakeQualifiedNames.cpp.tmpl', filters=filters)
    104     def generate_implementation(self):
    105         return self._template_context
    106 
    107 
    108 if __name__ == "__main__":
    109     in_generator.Maker(MakeQualifiedNamesWriter).main(sys.argv)
    110