Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import sys | 6 import sys |
| 7 | 7 |
| 8 import json5_generator | 8 import json5_generator |
| 9 import template_expander | 9 import template_expander |
| 10 import make_style_builder | 10 import make_style_builder |
| 11 | 11 |
| 12 from collections import namedtuple, defaultdict | 12 from collections import namedtuple, defaultdict |
| 13 from json5_generator import Json5File | |
| 14 from make_style_builder import StyleBuilderWriter | |
| 13 | 15 |
| 14 # Gets the classname for a given property. | 16 # Gets the classname for a given property. |
| 15 def get_classname(property): | 17 def get_classname(property): |
| 16 if property['api_class'] is True: | 18 if property['api_class'] is True: |
| 17 # This property had the generated_api_class flag set in CSSProperties.js on5. | 19 # This property had the generated_api_class flag set in CSSProperties.js on5. |
| 18 return 'CSSPropertyAPI' + property['upper_camel_name'] | 20 return 'CSSPropertyAPI' + property['upper_camel_name'] |
| 19 # This property has a specified class name. | 21 # This property has a specified class name. |
| 20 assert isinstance(property['api_class'], str), \ | 22 assert isinstance(property['api_class'], str), \ |
| 21 ("api_class value for " + property['api_class'] + " should be None, True or a string") | 23 ("api_class value for " + property['api_class'] + " should be None, True or a string") |
| 22 return property['api_class'] | 24 return property['api_class'] |
| 23 | 25 |
| 24 | 26 |
| 25 class CSSPropertyAPIWriter(make_style_builder.StyleBuilderWriter): | 27 class CSSPropertyAPIWriter(json5_generator.Writer): |
| 26 def __init__(self, json5_file_path): | 28 def __init__(self, json5_file_paths): |
| 27 super(CSSPropertyAPIWriter, self).__init__(json5_file_path) | 29 super(CSSPropertyAPIWriter, self).__init__(None) |
| 30 assert len(json5_file_paths) == 2, 'CSSPropertyAPIWriter requires 2 inpu t json5 files files, got %d.' % len(json5_file_paths) | |
| 31 | |
| 32 self.css_properties = StyleBuilderWriter([json5_file_paths[0]]) | |
| 33 self.css_property_api_methods = Json5File.load_from_files([json5_file_pa ths[1]], {}, {}) | |
| 34 | |
| 28 self._outputs = { | 35 self._outputs = { |
| 29 'CSSPropertyDescriptor.cpp': self.generate_property_descriptor_cpp, | 36 'CSSPropertyDescriptor.cpp': self.generate_property_descriptor_cpp, |
| 30 } | 37 } |
| 31 | 38 |
| 39 # Stores a map of API method name -> (signature, function_ptr) | |
| 40 self.all_api_methods = {} | |
| 41 ApiMethod = namedtuple('ApiMethod', ('signature', 'function_ptr')) | |
| 42 for api_method in self.css_property_api_methods.name_dictionaries: | |
| 43 self.all_api_methods[api_method['name']] = ApiMethod( | |
| 44 signature=api_method['signature'], | |
| 45 function_ptr=api_method['function_ptr'], | |
| 46 ) | |
| 47 | |
| 48 # Stores an ORDERED list of all API method names. | |
|
sashab
2017/02/09 04:36:28
This must match the order they appear in the Descr
| |
| 49 self.all_api_method_names = [api_method['name'] for api_method in self.c ss_property_api_methods.name_dictionaries] | |
| 50 | |
| 32 # Temporary map of API classname to list of propertyIDs that the API cla ss is for. | 51 # Temporary map of API classname to list of propertyIDs that the API cla ss is for. |
| 33 properties_for_class = defaultdict(list) | 52 properties_for_class = defaultdict(list) |
| 34 # Temporary map of API classname to set of method names this API class i mplements | 53 # Map of API classname to list of methods implemented in the API. |
| 35 api_methods_for_class = defaultdict(set) | 54 self.methods_for_class = defaultdict(list) |
| 36 for property in self._properties.values(): | 55 for property in self.css_properties.properties().values(): |
| 37 if property['api_class'] is None: | 56 if property['api_class'] is None: |
| 38 continue | 57 continue |
| 39 classname = get_classname(property) | 58 classname = get_classname(property) |
| 40 api_methods_for_class[classname] = property['api_methods'] | |
| 41 properties_for_class[classname].append(property['property_id']) | 59 properties_for_class[classname].append(property['property_id']) |
| 42 self._outputs[classname + '.h'] = self.generate_property_api_h_build er(classname, property['api_methods']) | 60 self.methods_for_class[classname].extend(property['api_methods']) |
|
sashab
2017/02/09 04:36:28
+=
| |
| 61 self._outputs[classname + '.h'] = self.generate_property_api_h_build er(classname) | |
| 43 | 62 |
| 44 # Stores a list of classes with elements (index, classname, [propertyIDs , ..], [api_methods, ...]). | 63 # Stores a list of classes with elements (index, classname, [propertyIDs , ..], [api_methods, ...]). |
| 45 self._api_classes = [] | 64 self._api_classes = [] |
| 46 | 65 ApiClass = namedtuple('ApiClass', ('index', 'classname', 'property_ids', 'methods_for_class')) |
| 47 ApiClass = namedtuple('ApiClass', ('index', 'classname', 'property_ids', 'api_methods')) | |
| 48 for i, classname in enumerate(properties_for_class.keys()): | 66 for i, classname in enumerate(properties_for_class.keys()): |
| 49 self._api_classes.append(ApiClass( | 67 self._api_classes.append(ApiClass( |
| 50 index=i + 1, | 68 index=i + 1, |
| 51 classname=classname, | 69 classname=classname, |
| 52 property_ids=properties_for_class[classname], | 70 property_ids=properties_for_class[classname], |
| 53 api_methods=api_methods_for_class[classname], | 71 methods_for_class=self.methods_for_class[classname] |
| 54 )) | 72 )) |
| 55 | 73 |
| 56 @template_expander.use_jinja('CSSPropertyDescriptor.cpp.tmpl') | 74 @template_expander.use_jinja('CSSPropertyDescriptor.cpp.tmpl') |
| 57 def generate_property_descriptor_cpp(self): | 75 def generate_property_descriptor_cpp(self): |
| 58 return { | 76 return { |
| 59 'api_classes': self._api_classes, | 77 'api_classes': self._api_classes, |
| 60 'api_methods': self.json5_file.parameters['api_methods']['valid_valu es'], | 78 'all_api_method_names': self.all_api_method_names, |
| 79 'methods_for_class': self.methods_for_class, | |
|
sashab
2017/02/09 04:36:28
Delete methods_for_class
| |
| 61 } | 80 } |
| 62 | 81 |
| 63 # Provides a function object given the classname of the property. | 82 # Provides a function object given the classname of the property. |
| 64 def generate_property_api_h_builder(self, api_classname, api_methods): | 83 def generate_property_api_h_builder(self, api_classname): |
| 65 @template_expander.use_jinja('CSSPropertyAPIFiles.h.tmpl') | 84 @template_expander.use_jinja('CSSPropertyAPIFiles.h.tmpl') |
| 66 def generate_property_api_h(): | 85 def generate_property_api_h(): |
| 67 return { | 86 return { |
| 68 'api_classname': api_classname, | 87 'api_classname': api_classname, |
| 69 'api_methods': api_methods, | 88 'methods_for_class': self.methods_for_class[api_classname], |
|
sashab
2017/02/09 04:36:28
Change dictionary from methods_for_class -> method
| |
| 89 'all_api_methods': self.all_api_methods, | |
| 70 } | 90 } |
| 71 return generate_property_api_h | 91 return generate_property_api_h |
| 72 | 92 |
| 73 if __name__ == '__main__': | 93 if __name__ == '__main__': |
| 74 json5_generator.Maker(CSSPropertyAPIWriter).main() | 94 json5_generator.Maker(CSSPropertyAPIWriter).main() |
| OLD | NEW |