Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 import os.path | |
| 4 import re | |
| 5 import subprocess | 3 import subprocess |
| 6 import sys | 4 import sys |
| 7 | 5 |
| 8 from in_file import InFile | 6 import css_properties |
| 7 import license | |
|
Nils Barth (inactive)
2014/07/07 16:43:28
alpha
| |
| 9 import in_generator | 8 import in_generator |
| 10 import license | 9 import template_expander |
| 11 | 10 |
| 12 | 11 |
| 13 HEADER_TEMPLATE = """ | 12 HEADER_TEMPLATE = """ |
| 14 %(license)s | 13 %(license)s |
| 15 | 14 |
| 16 #ifndef %(class_name)s_h | 15 #ifndef %(class_name)s_h |
| 17 #define %(class_name)s_h | 16 #define %(class_name)s_h |
| 18 | 17 |
| 19 #include "core/css/CSSParserMode.h" | 18 #include "core/css/CSSParserMode.h" |
| 20 #include "wtf/HashFunctions.h" | 19 #include "wtf/HashFunctions.h" |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 return true; | 170 return true; |
| 172 default: | 171 default: |
| 173 return false; | 172 return false; |
| 174 } | 173 } |
| 175 } | 174 } |
| 176 | 175 |
| 177 } // namespace WebCore | 176 } // namespace WebCore |
| 178 """ | 177 """ |
| 179 | 178 |
| 180 | 179 |
| 181 class CSSPropertiesWriter(in_generator.Writer): | 180 class CSSPropertyNamesWriter(css_properties.CSSProperties): |
| 182 class_name = "CSSPropertyNames" | 181 class_name = "CSSPropertyNames" |
|
Nils Barth (inactive)
2014/07/07 16:43:27
Could you single-quotify, while you're here?
| |
| 183 defaults = { | |
| 184 'alias_for': None, | |
| 185 'is_internal': False, | |
| 186 } | |
| 187 | 182 |
| 188 def __init__(self, file_paths): | 183 def __init__(self, in_file_path): |
| 189 in_generator.Writer.__init__(self, file_paths) | 184 super(CSSPropertyNamesWriter, self).__init__(in_file_path) |
| 190 self._outputs = {(self.class_name + ".h"): self.generate_header, | 185 self._outputs = {(self.class_name + ".h"): self.generate_header, |
|
Nils Barth (inactive)
2014/07/07 16:43:28
ditto
| |
| 191 (self.class_name + ".cpp"): self.generate_implementatio n, | 186 (self.class_name + ".cpp"): self.generate_implementatio n, |
| 192 } | 187 } |
| 193 | 188 |
| 194 self._aliases = filter(lambda property: property['alias_for'], self.in_f ile.name_dictionaries) | |
| 195 for offset, property in enumerate(self._aliases): | |
| 196 # Aliases use the enum_name that they are an alias for. | |
| 197 property['enum_name'] = self._enum_name_from_property_name(property[ 'alias_for']) | |
| 198 # Aliases do not get an enum_value. | |
| 199 | |
| 200 self._properties = filter(lambda property: not property['alias_for'], se lf.in_file.name_dictionaries) | |
| 201 if len(self._properties) > 1024: | |
| 202 print "ERROR : There is more than 1024 CSS Properties, you need to u pdate CSSProperty.h/StylePropertyMetadata m_propertyID accordingly." | |
| 203 exit(1) | |
| 204 self._first_property_id = 1 # We start after CSSPropertyInvalid. | |
| 205 property_id = self._first_property_id | |
| 206 for offset, property in enumerate(self._properties): | |
| 207 property['enum_name'] = self._enum_name_from_property_name(property[ 'name']) | |
| 208 property['enum_value'] = self._first_property_id + offset | |
| 209 if property['name'].startswith('-internal-'): | |
| 210 property['is_internal'] = True | |
| 211 | |
| 212 def _enum_name_from_property_name(self, property_name): | |
| 213 return "CSSProperty" + re.sub(r'(^[^-])|-(.)', lambda match: (match.grou p(1) or match.group(2)).upper(), property_name) | |
| 214 | |
| 215 def _enum_declaration(self, property): | 189 def _enum_declaration(self, property): |
| 216 return " %(enum_name)s = %(enum_value)s," % property | 190 return " %(property_id)s = %(enum_value)s," % property |
|
Nils Barth (inactive)
2014/07/07 16:43:28
ditto
| |
| 217 | 191 |
| 218 def generate_header(self): | 192 def generate_header(self): |
| 219 return HEADER_TEMPLATE % { | 193 return HEADER_TEMPLATE % { |
| 220 'license': license.license_for_generated_cpp(), | 194 'license': license.license_for_generated_cpp(), |
|
Nils Barth (inactive)
2014/07/07 16:43:28
alpha while here
| |
| 221 'class_name': self.class_name, | 195 'class_name': self.class_name, |
| 222 'property_enums': "\n".join(map(self._enum_declaration, self._proper ties)), | 196 'property_enums': "\n".join(map(self._enum_declaration, self._proper ties_list)), |
|
Nils Barth (inactive)
2014/07/07 16:43:28
quote
| |
| 223 'first_property_id': self._first_property_id, | 197 'first_property_id': self._first_enum_value, |
| 224 'properties_count': len(self._properties), | 198 'properties_count': len(self._properties), |
| 225 'last_property_id': self._first_property_id + len(self._properties) - 1, | 199 'last_property_id': self._first_enum_value + len(self._properties) - 1, |
| 226 'max_name_length': reduce(max, map(len, map(lambda property: propert y['name'], self._properties))), | 200 'max_name_length': max(map(len, self._properties)), |
| 227 } | 201 } |
| 228 | 202 |
| 229 def _case_properties(self, property): | |
| 230 return "case %(enum_name)s:" % property | |
| 231 | |
| 232 def generate_implementation(self): | 203 def generate_implementation(self): |
| 233 property_offsets = [] | 204 property_offsets = [] |
| 234 current_offset = 0 | 205 current_offset = 0 |
| 235 for property in self._properties: | 206 for property in self._properties_list: |
| 236 property_offsets.append(current_offset) | 207 property_offsets.append(current_offset) |
| 237 current_offset += len(property["name"]) + 1 | 208 current_offset += len(property["name"]) + 1 |
|
Nils Barth (inactive)
2014/07/07 16:43:27
quote
| |
| 238 | 209 |
| 210 css_name_and_enum_pairs = [(property['name'], property_id) for property_ id, property in self._properties.items()] | |
|
Nils Barth (inactive)
2014/07/07 16:43:27
line breaks?
| |
| 211 for name, aliased_name in self._aliases.items(): | |
| 212 css_name_and_enum_pairs.append((name, self._css_name_to_enum(aliased _name))) | |
| 213 | |
| 239 gperf_input = GPERF_TEMPLATE % { | 214 gperf_input = GPERF_TEMPLATE % { |
| 240 'license': license.license_for_generated_cpp(), | 215 'license': license.license_for_generated_cpp(), |
| 241 'class_name': self.class_name, | 216 'class_name': self.class_name, |
| 242 'property_name_strings': '\n'.join(map(lambda property: ' "%(name )s\\0"' % property, self._properties)), | 217 'property_name_strings': '\n'.join(map(lambda property: ' "%(name )s\\0"' % property, self._properties_list)), |
|
Nils Barth (inactive)
2014/07/07 16:43:27
generator expressions instead of map+lamba
| |
| 243 'property_name_offsets': '\n'.join(map(lambda offset: ' %d,' % of fset, property_offsets)), | 218 'property_name_offsets': '\n'.join(map(lambda offset: ' %d,' % of fset, property_offsets)), |
| 244 'property_to_enum_map': '\n'.join(map(lambda property: '%(name)s, %( enum_name)s' % property, self._properties + self._aliases)), | 219 'property_to_enum_map': '\n'.join(map(lambda property: '%s, %s' % pr operty, css_name_and_enum_pairs)), |
| 245 'internal_properties': '\n'.join(map(self._case_properties, filter(l ambda property: property['is_internal'], self._properties))), | 220 'internal_properties': '\n'.join("case %s:" % property_id for proper ty_id, property in self._properties.items() if property['is_internal']), |
| 246 } | 221 } |
| 247 # FIXME: If we could depend on Python 2.7, we would use subprocess.check _output | 222 # FIXME: If we could depend on Python 2.7, we would use subprocess.check _output |
| 248 gperf_args = [self.gperf_path, '--key-positions=*', '-P', '-n'] | 223 gperf_args = [self.gperf_path, '--key-positions=*', '-P', '-n'] |
| 249 gperf_args.extend(['-m', '50']) # Pick best of 50 attempts. | 224 gperf_args.extend(['-m', '50']) # Pick best of 50 attempts. |
| 250 gperf_args.append('-D') # Allow duplicate hashes -> More compact code. | 225 gperf_args.append('-D') # Allow duplicate hashes -> More compact code. |
| 251 gperf = subprocess.Popen(gperf_args, stdin=subprocess.PIPE, stdout=subpr ocess.PIPE, universal_newlines=True) | 226 gperf = subprocess.Popen(gperf_args, stdin=subprocess.PIPE, stdout=subpr ocess.PIPE, universal_newlines=True) |
| 252 return gperf.communicate(gperf_input)[0] | 227 return gperf.communicate(gperf_input)[0] |
| 253 | 228 |
| 254 | 229 |
| 255 if __name__ == "__main__": | 230 if __name__ == "__main__": |
|
Nils Barth (inactive)
2014/07/07 16:43:27
quote
| |
| 256 in_generator.Maker(CSSPropertiesWriter).main(sys.argv) | 231 in_generator.Maker(CSSPropertyNamesWriter).main(sys.argv) |
| OLD | NEW |