ProtoFile.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. from .TypeDefinitions import *
  31. import os
  32. import jinja2
  33. class ProtoFile:
  34. def __init__(self, proto_descriptor):
  35. self.descriptor = proto_descriptor
  36. if "proto2" == proto_descriptor.syntax:
  37. raise Exception(proto_descriptor.name + ": Sorry, proto2 is not supported, please use proto3.")
  38. # These file names are the ones used for creating the C++ files.
  39. self.filename_with_folder = os.path.splitext(proto_descriptor.name)[0]
  40. self.filename_without_folder = os.path.basename(self.filename_with_folder)
  41. # Construct the base scope used in this file.
  42. self.scope = None
  43. if self.descriptor.package:
  44. package_list = self.descriptor.package.split(".")
  45. # The first element is the base scope.
  46. self.scope = Scope(package_list[0], None)
  47. # Next add additional scopes nesting the previous one.
  48. for package in package_list[1:]:
  49. self.scope = Scope(package, self.scope)
  50. self.enum_definitions = [EnumDefinition(enum, self.scope) for enum in self.descriptor.enum_type]
  51. self.msg_definitions = [MessageDefinition(msg, self.scope) for msg in self.descriptor.message_type]
  52. self.all_parameters_registered = False
  53. def get_dependencies(self):
  54. imported_dependencies = []
  55. if self.descriptor.dependency:
  56. imported_dependencies = [os.path.splitext(dependency)[0] + ".h" for dependency in
  57. self.descriptor.dependency]
  58. return imported_dependencies
  59. def get_namespaces(self):
  60. if self.scope:
  61. result = self.scope.get_list_of_scope_str()
  62. else:
  63. result = []
  64. return result
  65. def get_header_guard(self):
  66. return self.filename_with_folder.replace("/", "_").upper()
  67. # Obtain a dictionary with references to all nested enums and messages
  68. def get_all_nested_types(self):
  69. nested_types = {"enums": self.enum_definitions, "messages": []}
  70. for msg in self.msg_definitions:
  71. nt = msg.get_all_nested_types()
  72. nested_types["enums"].extend(nt["enums"])
  73. nested_types["messages"].extend(nt["messages"])
  74. nested_types["messages"].append(msg)
  75. return nested_types
  76. def match_fields_with_definitions(self, all_types_definitions):
  77. for msg in self.msg_definitions:
  78. msg.match_fields_with_definitions(all_types_definitions)
  79. def register_template_parameters(self):
  80. all_parameters_registered = True
  81. for msg in self.msg_definitions:
  82. all_parameters_registered = msg.register_template_parameters() and all_parameters_registered
  83. return all_parameters_registered
  84. def render(self, jinja_environment):
  85. template_file = "Header.h"
  86. template = jinja_environment.get_template(template_file)
  87. file_str = template.render(proto_file=self, environment=jinja_environment)
  88. return file_str