| 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(make_style_builder.StyleBuilderWriter): |
| 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__(json5_file_paths[0]) |
| 30 # TODO(aazzam): Move the logic for loading CSSPropertyAPIMethods.json5 i
nto a new class APIMethodsWriter(). |
| 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', ('return_type', 'parameters')) |
| 46 for api_method in self.css_property_api_methods.name_dictionaries: |
| 47 self.ordered_api_method_names.append(api_method['name']) |
| 48 self.all_api_methods[api_method['name']] = ApiMethod( |
| 49 return_type=api_method['return_type'], |
| 50 parameters=api_method['parameters'], |
| 51 ) |
| 52 |
| 32 # Temporary map of API classname to list of propertyIDs that the API cla
ss is for. | 53 # Temporary map of API classname to list of propertyIDs that the API cla
ss is for. |
| 33 properties_for_class = defaultdict(list) | 54 properties_for_class = defaultdict(list) |
| 34 # Temporary map of API classname to set of method names this API class i
mplements | 55 # Map of API classname to list of methods implemented by that class. |
| 35 api_methods_for_class = defaultdict(set) | 56 self.methods_for_classes = defaultdict(list) |
| 36 for property in self._properties.values(): | 57 for property in self.properties().values(): |
| 37 if property['api_class'] is None: | 58 if property['api_class'] is None: |
| 38 continue | 59 continue |
| 39 classname = get_classname(property) | 60 classname = get_classname(property) |
| 40 api_methods_for_class[classname] = property['api_methods'] | |
| 41 properties_for_class[classname].append(property['property_id']) | 61 properties_for_class[classname].append(property['property_id']) |
| 42 self._outputs[classname + '.h'] = self.generate_property_api_h_build
er(classname, property['api_methods']) | 62 # For api_classes that contain multiple properties, combine all impl
emented properties. |
| 63 # This list contains duplicate entries, but is only used to check if
a method is |
| 64 # implemented for an api_class. |
| 65 self.methods_for_classes[classname] += property['api_methods'] |
| 66 self._outputs[classname + '.h'] = self.generate_property_api_h_build
er(classname) |
| 43 | 67 |
| 44 # Stores a list of classes with elements (index, classname, [propertyIDs
, ..], [api_methods, ...]). | 68 # Stores a list of classes with elements (index, classname, [propertyIDs
, ..], [api_methods, ...]). |
| 45 self._api_classes = [] | 69 self._api_classes = [] |
| 46 | 70 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()): | 71 for i, classname in enumerate(properties_for_class.keys()): |
| 49 self._api_classes.append(ApiClass( | 72 self._api_classes.append(ApiClass( |
| 50 index=i + 1, | 73 index=i + 1, |
| 51 classname=classname, | 74 classname=classname, |
| 52 property_ids=properties_for_class[classname], | 75 property_ids=properties_for_class[classname], |
| 53 api_methods=api_methods_for_class[classname], | 76 methods_for_class=self.methods_for_classes[classname] |
| 54 )) | 77 )) |
| 55 | 78 |
| 56 @template_expander.use_jinja('CSSPropertyDescriptor.cpp.tmpl') | 79 @template_expander.use_jinja('CSSPropertyDescriptor.cpp.tmpl') |
| 57 def generate_property_descriptor_cpp(self): | 80 def generate_property_descriptor_cpp(self): |
| 58 return { | 81 return { |
| 59 'api_classes': self._api_classes, | 82 'api_classes': self._api_classes, |
| 60 'api_methods': self.json5_file.parameters['api_methods']['valid_valu
es'], | 83 'ordered_api_method_names': self.ordered_api_method_names, |
| 61 } | 84 } |
| 62 | 85 |
| 63 # Provides a function object given the classname of the property. | 86 # Provides a function object given the classname of the property. |
| 64 def generate_property_api_h_builder(self, api_classname, api_methods): | 87 def generate_property_api_h_builder(self, api_classname): |
| 65 @template_expander.use_jinja('CSSPropertyAPIFiles.h.tmpl') | 88 @template_expander.use_jinja('CSSPropertyAPIFiles.h.tmpl') |
| 66 def generate_property_api_h(): | 89 def generate_property_api_h(): |
| 67 return { | 90 return { |
| 68 'api_classname': api_classname, | 91 'api_classname': api_classname, |
| 69 'api_methods': api_methods, | 92 'methods_for_class': self.methods_for_classes[api_classname], |
| 93 'all_api_methods': self.all_api_methods, |
| 70 } | 94 } |
| 71 return generate_property_api_h | 95 return generate_property_api_h |
| 72 | 96 |
| 73 if __name__ == '__main__': | 97 if __name__ == '__main__': |
| 74 json5_generator.Maker(CSSPropertyAPIWriter).main() | 98 json5_generator.Maker(CSSPropertyAPIWriter).main() |
| OLD | NEW |