main.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. # Copyright 2019 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 argparse
  6. import sys
  7. import os
  8. sys.path += [os.path.dirname(os.path.dirname(__file__))]
  9. from style_variable_generator.css_generator import CSSStyleGenerator
  10. from style_variable_generator.ts_generator import TSStyleGenerator
  11. from style_variable_generator.proto_generator import ProtoStyleGenerator, ProtoJSONStyleGenerator
  12. from style_variable_generator.json_generator import JSONStyleGenerator
  13. from style_variable_generator.views_generator import ViewsCCStyleGenerator, ViewsHStyleGenerator
  14. from style_variable_generator.base_generator import Modes
  15. from style_variable_generator.color_mappings_generator import ColorMappingsCCStyleGenerator, ColorMappingsHStyleGenerator
  16. def parseGeneratorOptionList(options):
  17. result = {}
  18. if options is None:
  19. return result
  20. for key_value_pair in options:
  21. key, value = key_value_pair.split('=', 1)
  22. key = key.strip()
  23. result[key] = value
  24. return result
  25. def main():
  26. parser = argparse.ArgumentParser(
  27. description='Generate style variables from JSON5 color file.')
  28. generators = [
  29. CSSStyleGenerator, ViewsCCStyleGenerator, ViewsHStyleGenerator,
  30. ProtoStyleGenerator, ProtoJSONStyleGenerator, TSStyleGenerator,
  31. JSONStyleGenerator, ColorMappingsCCStyleGenerator,
  32. ColorMappingsHStyleGenerator
  33. ]
  34. parser.add_argument(
  35. '--generator',
  36. choices=[g.GetName() for g in generators],
  37. required=True,
  38. help='type of file to generate, provide this option multiple '
  39. 'times to use multiple generators',
  40. action='append')
  41. parser.add_argument('--generate-single-mode',
  42. choices=Modes.ALL,
  43. help='generates output for a single mode')
  44. parser.add_argument(
  45. '--out-file',
  46. help='file to write output to, the number of out-files '
  47. 'must match the number of generators if specified',
  48. action='append')
  49. parser.add_argument('--generator-option',
  50. metavar='KEY=VALUE',
  51. action='append',
  52. help='Set a option specific to the selected generator '
  53. 'via a key value pair. See the README.md for a '
  54. 'full list of generator specific options.')
  55. parser.add_argument('targets', nargs='+', help='source json5 color files')
  56. args = parser.parse_args()
  57. if args.out_file and len(args.generator) != len(args.out_file):
  58. raise ValueError(
  59. 'number of --out-files must match number of --generators')
  60. for i, g in enumerate(args.generator):
  61. gen_constructor = next(x for x in generators if g == x.GetName())
  62. style_generator = gen_constructor()
  63. style_generator.AddJSONFilesToModel(args.targets)
  64. style_generator.generate_single_mode = args.generate_single_mode
  65. style_generator.generator_options = parseGeneratorOptionList(
  66. args.generator_option)
  67. if args.out_file:
  68. style_generator.out_file_path = args.out_file[i]
  69. with open(args.out_file[i], 'w') as f:
  70. f.write(style_generator.Render())
  71. else:
  72. print('=========', style_generator.GetName(), '=========')
  73. print(style_generator.Render())
  74. return 0
  75. if __name__ == '__main__':
  76. sys.exit(main())