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
30import sys
31
32import hasher
33import in_generator
34import template_expander
35import name_utilities
36
37def _symbol(entry):
38    # FIXME: Remove this special case for the ugly x-webkit-foo attributes.
39    if entry['name'].startswith('-webkit-'):
40        return entry['name'].replace('-', '_')[1:]
41    return name_utilities.cpp_name(entry).replace('-', '_')
42
43
44class MakeNamesWriter(in_generator.Writer):
45    defaults = {
46        'Conditional': None,  # FIXME: Add support for Conditional.
47        'RuntimeEnabled': None,  # What should we do for runtime-enabled features?
48        'ImplementedAs': None,
49    }
50    default_parameters = {
51        'namespace': '',
52        'export': '',
53    }
54    filters = {
55        'cpp_name': name_utilities.cpp_name,
56        'hash': hasher.hash,
57        'script_name': name_utilities.script_name,
58        'symbol': _symbol,
59        'to_macro_style': name_utilities.to_macro_style,
60    }
61
62    def __init__(self, in_file_path):
63        super(MakeNamesWriter, self).__init__(in_file_path)
64
65        namespace = self.in_file.parameters['namespace'].strip('"')
66        export = self.in_file.parameters['export'].strip('"')
67
68        assert namespace, 'A namespace is required.'
69
70        self._outputs = {
71            (namespace + 'Names.h'): self.generate_header,
72            (namespace + 'Names.cpp'): self.generate_implementation,
73        }
74        self._template_context = {
75            'namespace': namespace,
76            'export': export,
77            'entries': self.in_file.name_dictionaries,
78        }
79
80    @template_expander.use_jinja("MakeNames.h.tmpl", filters=filters)
81    def generate_header(self):
82        return self._template_context
83
84    @template_expander.use_jinja("MakeNames.cpp.tmpl", filters=filters)
85    def generate_implementation(self):
86        return self._template_context
87
88
89if __name__ == "__main__":
90    in_generator.Maker(MakeNamesWriter).main(sys.argv)
91