protoc-gen-eams.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #
  2. # Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
  3. #
  4. # This file is part of Embedded Proto.
  5. #
  6. # Embedded Proto is open source software: you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as published
  8. # by the Free Software Foundation, version 3 of the license.
  9. #
  10. # Embedded Proto is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
  17. #
  18. # For commercial and closed source application please visit:
  19. # <https://EmbeddedProto.com/license/>.
  20. #
  21. # Embedded AMS B.V.
  22. # Info:
  23. # info at EmbeddedProto dot com
  24. #
  25. # Postal address:
  26. # Johan Huizingalaan 763a
  27. # 1066 VH, Amsterdam
  28. # the Netherlands
  29. #
  30. import io
  31. import sys
  32. import os
  33. from support.ProtoFile import ProtoFile
  34. from google.protobuf.compiler import plugin_pb2 as plugin
  35. import jinja2
  36. # -----------------------------------------------------------------------------
  37. def generate_code(request, respones):
  38. # Create definitions for al proto files in the request.
  39. file_definitions = [ProtoFile(proto_file) for proto_file in request.proto_file]
  40. # Obtain all definitions made in all the files to properly link definitions with fields using them. This to properly
  41. # create template parameters.
  42. all_types_definitions = {"enums": [], "messages": []}
  43. for fd in file_definitions:
  44. nt = fd.get_all_nested_types()
  45. all_types_definitions["enums"].extend(nt["enums"])
  46. all_types_definitions["messages"].extend(nt["messages"])
  47. # Match all fields with their respective type definition.
  48. for fd in file_definitions:
  49. fd.match_fields_with_definitions(all_types_definitions)
  50. # Add template parameters to the fields that need them.
  51. all_parameters_registered = True
  52. for _ in range(3):
  53. for fd in file_definitions:
  54. all_parameters_registered = fd.register_template_parameters() and all_parameters_registered
  55. if all_parameters_registered:
  56. break
  57. if not all_parameters_registered:
  58. raise Exception("Messages with repeated, string or byte fields use template parameters to define their length."
  59. "For some reason it was not to add all required template parameters.")
  60. curr_location = os.path.dirname(os.path.abspath(__file__))
  61. filepath = os.path.join(curr_location, "templates")
  62. template_loader = jinja2.FileSystemLoader(searchpath=filepath)
  63. template_env = jinja2.Environment(loader=template_loader, trim_blocks=True, lstrip_blocks=True)
  64. for fd in file_definitions:
  65. file_str = fd.render(template_env)
  66. if file_str:
  67. f = respones.file.add()
  68. f.name = fd.filename_with_folder + ".h"
  69. f.content = file_str
  70. else:
  71. break
  72. # -----------------------------------------------------------------------------
  73. def main_plugin():
  74. # The main function when running the scrip as a protoc plugin. It will read in the protoc data from the stdin and
  75. # write back the output to stdout.
  76. # Read request message from stdin
  77. data = io.open(sys.stdin.fileno(), "rb").read()
  78. request = plugin.CodeGeneratorRequest.FromString(data)
  79. if '--debug' in sys.argv:
  80. # Write the requests to a file for easy debugging.
  81. with open("./debug_embbeded_proto.bin", 'wb') as file:
  82. file.write(request.SerializeToString())
  83. # Create response
  84. response = plugin.CodeGeneratorResponse()
  85. # Generate code
  86. try:
  87. generate_code(request, response)
  88. except jinja2.UndefinedError as e:
  89. response.error = "Embedded Proto error - Template Undefined Error exception: " + str(e)
  90. except jinja2.TemplateRuntimeError as e:
  91. response.error = "Embedded Proto error - Template Runtime Error exception: " + str(e)
  92. except jinja2.TemplateAssertionError as e:
  93. response.error = "Embedded Proto error - TemplateAssertionError exception: " + str(e)
  94. except jinja2.TemplateSyntaxError as e:
  95. response.error = "Embedded Proto error - TemplateSyntaxError exception: " + str(e)
  96. except jinja2.TemplateError as e:
  97. response.error = "Embedded Proto error - TemplateError exception: " + str(e)
  98. except Exception as e:
  99. response.error = "Embedded Proto error - " + str(e)
  100. # Serialize response message
  101. output = response.SerializeToString()
  102. # Write to stdout
  103. io.open(sys.stdout.fileno(), "wb").write(output)
  104. # -----------------------------------------------------------------------------
  105. def main_cli():
  106. # The main function when running from the command line and debugging. Instead of receiving data from protoc this
  107. # will read in a binary file stored the previous time main_plugin() is ran.
  108. with open("debug_embbeded_proto.bin", 'rb') as file:
  109. data = file.read()
  110. request = plugin.CodeGeneratorRequest.FromString(data)
  111. # Create response
  112. response = plugin.CodeGeneratorResponse()
  113. # Generate code
  114. try:
  115. generate_code(request, response)
  116. except jinja2.UndefinedError as e:
  117. response.error = "Embedded Proto error - Template Undefined Error exception: " + str(e)
  118. except jinja2.TemplateRuntimeError as e:
  119. response.error = "Embedded Proto error - Template Runtime Error exception: " + str(e)
  120. except jinja2.TemplateAssertionError as e:
  121. response.error = "Embedded Proto error - TemplateAssertionError exception: " + str(e)
  122. except jinja2.TemplateSyntaxError as e:
  123. response.error = "Embedded Proto error - TemplateSyntaxError exception: " + str(e)
  124. except jinja2.TemplateError as e:
  125. response.error = "Embedded Proto error - TemplateError exception: " + str(e)
  126. except Exception as e:
  127. response.error = "Embedded Proto error - " + str(e)
  128. # For debugging purposes print the result to the console.
  129. for response_file in response.file:
  130. print(response_file.name)
  131. print(response_file.content)
  132. # -----------------------------------------------------------------------------
  133. if __name__ == '__main__':
  134. # Check if we are running as a plugin under protoc
  135. if '--protoc-plugin' in sys.argv:
  136. main_plugin()
  137. else:
  138. main_cli()