main.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python3
  2. # Copyright 2022 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 os
  7. import sys
  8. sys.path += [os.path.dirname(os.path.dirname(__file__))]
  9. from json_data_generator.generator import JSONDataGenerator
  10. from json_data_generator.util import (GetFileNameWithoutExtensionFromPath,
  11. JoinPath)
  12. def main():
  13. parser = argparse.ArgumentParser(
  14. description='Generate data from JSON5 file.')
  15. parser.add_argument('--templates',
  16. nargs='+',
  17. help="Jinja template files (*.jinja)")
  18. parser.add_argument(
  19. '--template-helper',
  20. help='additional python file to provide custom Jinja globals/filters')
  21. parser.add_argument('--out-dir', help='directory to write output to')
  22. parser.add_argument('--sources', nargs='+', help='source json5 data files')
  23. args = parser.parse_args()
  24. generator = JSONDataGenerator(args.out_dir)
  25. generator.AddJSONFilesToModel(args.sources)
  26. generator.out_dir = args.out_dir
  27. os.makedirs(args.out_dir, exist_ok=True)
  28. for template_path in args.templates:
  29. out_file_path = JoinPath(
  30. args.out_dir, GetFileNameWithoutExtensionFromPath(template_path))
  31. with open(out_file_path, 'w') as f:
  32. f.write(
  33. generator.RenderTemplate(template_path, args.template_helper))
  34. return 0
  35. if __name__ == '__main__':
  36. sys.exit(main())