codegen.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python
  2. # Copyright (c) 2022 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import click
  16. import logging
  17. import coloredlogs
  18. import enum
  19. try:
  20. from idl.matter_idl_parser import CreateParser
  21. except:
  22. import os
  23. import sys
  24. sys.path.append(os.path.abspath(os.path.dirname(__file__)))
  25. from idl.matter_idl_parser import CreateParser
  26. from idl.generators import FileSystemGeneratorStorage, GeneratorStorage
  27. from idl.generators.java import JavaGenerator
  28. class CodeGeneratorTypes(enum.Enum):
  29. """
  30. Represents every generator type supported by codegen and maps
  31. the simple enum value (user friendly and can be a command line input)
  32. into underlying generators.
  33. """
  34. JAVA = enum.auto()
  35. def CreateGenerator(self, *args, **kargs):
  36. if self == CodeGeneratorTypes.JAVA:
  37. return JavaGenerator(*args, **kargs)
  38. else:
  39. raise Error("Unknown code generator type")
  40. class ListGeneratedFilesStorage(GeneratorStorage):
  41. """
  42. A storage that prints out file names that would have content in them.
  43. """
  44. def get_existing_data(self, relative_path: str):
  45. return None # stdout has no pre-existing data
  46. def write_new_data(self, relative_path: str, content: str):
  47. print(relative_path)
  48. # Supported log levels, mapping string values required for argument
  49. # parsing into logging constants
  50. __LOG_LEVELS__ = {
  51. 'debug': logging.DEBUG,
  52. 'info': logging.INFO,
  53. 'warn': logging.WARN,
  54. 'fatal': logging.FATAL,
  55. }
  56. __GENERATORS__ = {
  57. 'java': CodeGeneratorTypes.JAVA,
  58. }
  59. @click.command()
  60. @click.option(
  61. '--log-level',
  62. default='INFO',
  63. type=click.Choice(__LOG_LEVELS__.keys(), case_sensitive=False),
  64. help='Determines the verbosity of script output')
  65. @click.option(
  66. '--generator',
  67. default='JAVA',
  68. type=click.Choice(__GENERATORS__.keys(), case_sensitive=False),
  69. help='What code generator to run')
  70. @click.option(
  71. '--output-dir',
  72. type=click.Path(exists=False),
  73. default=".",
  74. help='Where to generate the code')
  75. @click.option(
  76. '--dry-run',
  77. default=False,
  78. is_flag=True,
  79. help='If to actually generate')
  80. @click.option(
  81. '--name-only',
  82. default=False,
  83. is_flag=True,
  84. help='Output just a list of file names that would be generated')
  85. @click.argument(
  86. 'idl_path',
  87. type=click.Path(exists=True))
  88. def main(log_level, generator, output_dir, dry_run, name_only, idl_path):
  89. """
  90. Parses MATTER IDL files (.matter) and performs SDK code generation
  91. as set up by the program arguments.
  92. """
  93. coloredlogs.install(level=__LOG_LEVELS__[
  94. log_level], fmt='%(asctime)s %(levelname)-7s %(message)s')
  95. logging.info("Parsing idl from %s" % idl_path)
  96. idl_tree = CreateParser().parse(open(idl_path, "rt").read())
  97. if name_only:
  98. storage = ListGeneratedFilesStorage()
  99. else:
  100. storage = FileSystemGeneratorStorage(output_dir)
  101. logging.info("Running code generator %s" % generator)
  102. generator = __GENERATORS__[
  103. generator].CreateGenerator(storage, idl=idl_tree)
  104. generator.render(dry_run)
  105. logging.info("Done")
  106. if __name__ == '__main__':
  107. main()