/third_party/blink/renderer/build/scripts/core/css/properties/make_css_property_instances.py

https://github.com/chromium/chromium · Python · 93 lines · 61 code · 16 blank · 16 comment · 3 complexity · 90b74babbfb19f5516d345eaa05996bf MD5 · raw file

  1. #!/usr/bin/env python
  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
  4. # found in the LICENSE file.
  5. import json5_generator
  6. import template_expander
  7. from collections import namedtuple
  8. from core.css import css_properties
  9. class PropertyClassData(
  10. namedtuple(
  11. 'PropertyClassData',
  12. 'enum_key,enum_value,property_id,classname,namespace_group,filename'
  13. )):
  14. pass
  15. class CSSPropertyInstancesWriter(json5_generator.Writer):
  16. def __init__(self, json5_file_paths, output_dir):
  17. super(CSSPropertyInstancesWriter, self).__init__([], output_dir)
  18. self._input_files = json5_file_paths
  19. self._outputs = {
  20. 'css_property_instances.h':
  21. self.generate_property_instances_header,
  22. 'css_property_instances.cc':
  23. self.generate_property_instances_implementation
  24. }
  25. # These files are no longer generated. If the files are present from
  26. # a previous build, we remove them. This avoids accidentally #including
  27. # a stale generated header.
  28. self._cleanup = set([
  29. 'css_property.cc', 'css_property.h', 'css_unresolved_property.cc',
  30. 'css_unresolved_property.h'
  31. ])
  32. self._css_properties = css_properties.CSSProperties(json5_file_paths)
  33. properties = self._css_properties.longhands + self._css_properties.shorthands
  34. aliases = self._css_properties.aliases
  35. # Lists of PropertyClassData.
  36. self._property_classes_by_id = list(map(self.get_class, properties))
  37. self._alias_classes_by_id = list(map(self.get_class, aliases))
  38. # Sort by enum value.
  39. self._property_classes_by_id.sort(key=lambda t: t.enum_value)
  40. self._alias_classes_by_id.sort(key=lambda t: t.enum_value)
  41. def get_class(self, property_):
  42. """Gets the automatically
  43. generated class name for a property.
  44. Args:
  45. property_: A single property from CSSProperties.properties()
  46. Returns:
  47. The name to use for the property class.
  48. """
  49. namespace_group = 'Shorthand' if property_['longhands'] else 'Longhand'
  50. return PropertyClassData(
  51. enum_key=property_['enum_key'],
  52. enum_value=property_['enum_value'],
  53. property_id=property_['property_id'],
  54. classname=property_['name'].to_upper_camel_case(),
  55. namespace_group=namespace_group,
  56. filename=property_['name'].to_snake_case())
  57. @property
  58. def css_properties(self):
  59. return self._css_properties
  60. @template_expander.use_jinja(
  61. 'core/css/properties/templates/css_property_instances.h.tmpl')
  62. def generate_property_instances_header(self):
  63. return {
  64. 'input_files': self._input_files,
  65. 'property_classes_by_property_id': self._property_classes_by_id,
  66. 'alias_classes_by_property_id': self._alias_classes_by_id,
  67. }
  68. @template_expander.use_jinja(
  69. 'core/css/properties/templates/css_property_instances.cc.tmpl')
  70. def generate_property_instances_implementation(self):
  71. return {
  72. 'input_files': self._input_files,
  73. 'property_classes_by_property_id': self._property_classes_by_id,
  74. 'alias_classes_by_property_id': self._alias_classes_by_id,
  75. }
  76. if __name__ == '__main__':
  77. json5_generator.Maker(CSSPropertyInstancesWriter).main()