Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 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 re |
| 31 | import sys |
| 32 | |
| 33 | import in_generator |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 34 | import template_expander |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 35 | |
| 36 | |
| 37 | class StyleBuilderWriter(in_generator.Writer): |
| 38 | class_name = 'StyleBuilder' |
| 39 | |
| 40 | valid_values = { |
| 41 | 'apply_type': ['default', 'length'], |
| 42 | 'use_none': [True, False], |
| 43 | 'use_intrinsic': [True, False], |
| 44 | 'use_auto': [True, False], |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 45 | 'custom_all': [True, False], |
| 46 | 'custom_initial': [True, False], |
| 47 | 'custom_inherit': [True, False], |
| 48 | 'custom_value': [True, False], |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 49 | } |
| 50 | defaults = { |
| 51 | 'condition': None, |
| 52 | 'apply_type': 'default', |
| 53 | 'name_for_methods': None, |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 54 | 'use_handlers_for': None, |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 55 | # These depend on property name by default |
| 56 | 'type_name': None, |
| 57 | 'getter': None, |
| 58 | 'setter': None, |
| 59 | 'initial': None, |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 60 | # Setting these stops default handlers being generated |
| 61 | # Setting custom_all is the same as setting the other three |
| 62 | 'custom_all': False, |
| 63 | 'custom_initial': False, |
| 64 | 'custom_inherit': False, |
| 65 | 'custom_value': False, |
| 66 | # For the length apply type. Will get moved out to StyleBuilderFunctions.cpp.tmpl |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 67 | 'use_none': False, |
| 68 | 'use_intrinsic': False, |
| 69 | 'use_auto': False, |
| 70 | } |
| 71 | |
| 72 | def __init__(self, in_files, enabled_conditions): |
| 73 | super(StyleBuilderWriter, self).__init__(in_files, enabled_conditions) |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 74 | self._outputs = {("StyleBuilderFunctions.h"): self.generate_style_builder_functions_h, |
| 75 | ("StyleBuilderFunctions.cpp"): self.generate_style_builder_functions_cpp, |
| 76 | ("StyleBuilder.cpp"): self.generate_style_builder, |
| 77 | } |
| 78 | |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 79 | self._properties = self.in_file.name_dictionaries |
| 80 | |
| 81 | def set_if_none(property, key, value): |
| 82 | if property[key] is None: |
| 83 | property[key] = value |
| 84 | |
| 85 | for property in self._properties: |
| 86 | cc = self._camelcase_property_name(property["name"]) |
| 87 | property["property_id"] = "CSSProperty" + cc |
| 88 | cc = property["name_for_methods"] or cc.replace("Webkit", "") |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 89 | property["camel_case_name"] = cc |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 90 | set_if_none(property, "type_name", "E" + cc) |
| 91 | set_if_none(property, "getter", self._lower_first(cc)) |
| 92 | set_if_none(property, "setter", "set" + cc) |
| 93 | set_if_none(property, "initial", "initial" + cc) |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 94 | if property["custom_all"]: |
| 95 | property["custom_initial"] = True |
| 96 | property["custom_inherit"] = True |
| 97 | property["custom_value"] = True |
| 98 | |
| 99 | self._properties = dict((property["property_id"], property) for property in self._properties) |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 100 | |
| 101 | # FIXME: some of these might be better in a utils file |
| 102 | @staticmethod |
| 103 | def _camelcase_property_name(property_name): |
| 104 | return re.sub(r'(^[^-])|-(.)', lambda match: (match.group(1) or match.group(2)).upper(), property_name) |
| 105 | |
| 106 | @staticmethod |
| 107 | def _lower_first(s): |
| 108 | return s[0].lower() + s[1:] |
| 109 | |
| 110 | @staticmethod |
| 111 | def _upper_first(s): |
| 112 | return s[0].upper() + s[1:] |
| 113 | |
Torne (Richard Coles) | 5267f70 | 2013-06-11 10:57:24 +0100 | [diff] [blame] | 114 | @template_expander.use_jinja("StyleBuilderFunctions.h.tmpl") |
| 115 | def generate_style_builder_functions_h(self): |
| 116 | return { |
| 117 | "properties": self._properties, |
| 118 | } |
| 119 | |
| 120 | @template_expander.use_jinja("StyleBuilderFunctions.cpp.tmpl") |
| 121 | def generate_style_builder_functions_cpp(self): |
| 122 | return { |
| 123 | "properties": self._properties, |
| 124 | } |
| 125 | |
| 126 | @template_expander.use_jinja("StyleBuilder.cpp.tmpl") |
| 127 | def generate_style_builder(self): |
Torne (Richard Coles) | 93ac45c | 2013-05-29 14:40:20 +0100 | [diff] [blame] | 128 | return { |
| 129 | "properties": self._properties, |
| 130 | } |
| 131 | |
| 132 | |
| 133 | if __name__ == "__main__": |
| 134 | in_generator.Maker(StyleBuilderWriter).main(sys.argv) |