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 #TODO(aazzam): Move the logic for loading CSSPropertyAPIMethods.json5 in to a new class APIMethodsWriter(). | |
|
sashab
2017/02/10 00:58:33
# space after hash
| |
| 31 assert len(json5_file_paths) == 2, 'CSSPropertyAPIWriter requires 2 inpu t json5 files files, got %d.' % len(json5_file_paths) | |
| 32 | |
| 33 self.css_properties = StyleBuilderWriter([json5_file_paths[0]]) | |
| 34 self.css_property_api_methods = Json5File.load_from_files([json5_file_pa ths[1]], {}, {}) | |
| 35 | |
| 28 self._outputs = { | 36 self._outputs = { |
| 29 'CSSPropertyDescriptor.cpp': self.generate_property_descriptor_cpp, | 37 'CSSPropertyDescriptor.cpp': self.generate_property_descriptor_cpp, |
| 30 } | 38 } |
| 31 | 39 |
| 40 # Stores a map of API method name -> (return_type, parameters) | |
| 41 self.all_api_methods = {} | |
| 42 # Stores an ordered list of all API method names. This must match the | |
| 43 # order they appear in the Descriptor object. | |
| 44 self.ordered_api_method_names = [] | |
| 45 ApiMethod = namedtuple('ApiMethod', ('signature', 'function_ptr')) | |
| 46 for api_method in self.css_property_api_methods.name_dictionaries: | |
| 47 signature = "static " + api_method['return_type'] + " " + api_method ['name'] + api_method['parameters'] | |
| 48 function_ptr = api_method['return_type'] + " (*" + api_method['name' ] + ")" + api_method['parameters'] | |
|
sashab
2017/02/10 00:58:33
Move this to tmpl
| |
| 49 self.ordered_api_method_names.append(api_method['name']) | |
| 50 self.all_api_methods[api_method['name']] = ApiMethod( | |
| 51 signature=signature, | |
| 52 function_ptr=function_ptr, | |
| 53 ) | |
| 54 | |
| 32 # Temporary map of API classname to list of propertyIDs that the API cla ss is for. | 55 # Temporary map of API classname to list of propertyIDs that the API cla ss is for. |
| 33 properties_for_class = defaultdict(list) | 56 properties_for_class = defaultdict(list) |
| 34 # Temporary map of API classname to set of method names this API class i mplements | 57 # Map of API classname to list of methods implemented by that class. |
| 35 api_methods_for_class = defaultdict(set) | 58 self.methods_for_classes = defaultdict(list) |
| 36 for property in self._properties.values(): | 59 for property in self.css_properties.properties().values(): |
| 37 if property['api_class'] is None: | 60 if property['api_class'] is None: |
| 38 continue | 61 continue |
| 39 classname = get_classname(property) | 62 classname = get_classname(property) |
| 40 api_methods_for_class[classname] = property['api_methods'] | |
| 41 properties_for_class[classname].append(property['property_id']) | 63 properties_for_class[classname].append(property['property_id']) |
| 42 self._outputs[classname + '.h'] = self.generate_property_api_h_build er(classname, property['api_methods']) | 64 self.methods_for_classes[classname] += property['api_methods'] |
| 65 self._outputs[classname + '.h'] = self.generate_property_api_h_build er(classname) | |
| 43 | 66 |
| 44 # Stores a list of classes with elements (index, classname, [propertyIDs , ..], [api_methods, ...]). | 67 # Stores a list of classes with elements (index, classname, [propertyIDs , ..], [api_methods, ...]). |
| 45 self._api_classes = [] | 68 self._api_classes = [] |
| 46 | 69 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()): | 70 for i, classname in enumerate(properties_for_class.keys()): |
| 49 self._api_classes.append(ApiClass( | 71 self._api_classes.append(ApiClass( |
| 50 index=i + 1, | 72 index=i + 1, |
| 51 classname=classname, | 73 classname=classname, |
| 52 property_ids=properties_for_class[classname], | 74 property_ids=properties_for_class[classname], |
| 53 api_methods=api_methods_for_class[classname], | 75 methods_for_class=self.methods_for_classes[classname] |
| 54 )) | 76 )) |
| 55 | 77 |
| 56 @template_expander.use_jinja('CSSPropertyDescriptor.cpp.tmpl') | 78 @template_expander.use_jinja('CSSPropertyDescriptor.cpp.tmpl') |
| 57 def generate_property_descriptor_cpp(self): | 79 def generate_property_descriptor_cpp(self): |
| 58 return { | 80 return { |
| 59 'api_classes': self._api_classes, | 81 'api_classes': self._api_classes, |
| 60 'api_methods': self.json5_file.parameters['api_methods']['valid_valu es'], | 82 'ordered_api_method_names': self.ordered_api_method_names, |
| 61 } | 83 } |
| 62 | 84 |
| 63 # Provides a function object given the classname of the property. | 85 # Provides a function object given the classname of the property. |
| 64 def generate_property_api_h_builder(self, api_classname, api_methods): | 86 def generate_property_api_h_builder(self, api_classname): |
| 65 @template_expander.use_jinja('CSSPropertyAPIFiles.h.tmpl') | 87 @template_expander.use_jinja('CSSPropertyAPIFiles.h.tmpl') |
| 66 def generate_property_api_h(): | 88 def generate_property_api_h(): |
| 67 return { | 89 return { |
| 68 'api_classname': api_classname, | 90 'api_classname': api_classname, |
| 69 'api_methods': api_methods, | 91 'methods_for_class': self.methods_for_classes[api_classname], |
| 92 'all_api_methods': self.all_api_methods, | |
| 70 } | 93 } |
| 71 return generate_property_api_h | 94 return generate_property_api_h |
| 72 | 95 |
| 73 if __name__ == '__main__': | 96 if __name__ == '__main__': |
| 74 json5_generator.Maker(CSSPropertyAPIWriter).main() | 97 json5_generator.Maker(CSSPropertyAPIWriter).main() |
| OLD | NEW |