图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.3 KiB

  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. # https://developers.google.com/protocol-buffers/
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """Builds descriptors, message classes and services for generated _pb2.py.
  31. This file is only called in python generated _pb2.py files. It builds
  32. descriptors, message classes and services that users can directly use
  33. in generated code.
  34. """
  35. __author__ = 'jieluo@google.com (Jie Luo)'
  36. from google.protobuf.internal import enum_type_wrapper
  37. from google.protobuf.internal import python_message
  38. from google.protobuf import message as _message
  39. from google.protobuf import reflection as _reflection
  40. from google.protobuf import symbol_database as _symbol_database
  41. _sym_db = _symbol_database.Default()
  42. def BuildMessageAndEnumDescriptors(file_des, module):
  43. """Builds message and enum descriptors.
  44. Args:
  45. file_des: FileDescriptor of the .proto file
  46. module: Generated _pb2 module
  47. """
  48. def BuildNestedDescriptors(msg_des, prefix):
  49. for (name, nested_msg) in msg_des.nested_types_by_name.items():
  50. module_name = prefix + name.upper()
  51. module[module_name] = nested_msg
  52. BuildNestedDescriptors(nested_msg, module_name + '_')
  53. for enum_des in msg_des.enum_types:
  54. module[prefix + enum_des.name.upper()] = enum_des
  55. for (name, msg_des) in file_des.message_types_by_name.items():
  56. module_name = '_' + name.upper()
  57. module[module_name] = msg_des
  58. BuildNestedDescriptors(msg_des, module_name + '_')
  59. def BuildTopDescriptorsAndMessages(file_des, module_name, module):
  60. """Builds top level descriptors and message classes.
  61. Args:
  62. file_des: FileDescriptor of the .proto file
  63. module_name: str, the name of generated _pb2 module
  64. module: Generated _pb2 module
  65. """
  66. def BuildMessage(msg_des):
  67. create_dict = {}
  68. for (name, nested_msg) in msg_des.nested_types_by_name.items():
  69. create_dict[name] = BuildMessage(nested_msg)
  70. create_dict['DESCRIPTOR'] = msg_des
  71. create_dict['__module__'] = module_name
  72. message_class = _reflection.GeneratedProtocolMessageType(
  73. msg_des.name, (_message.Message,), create_dict)
  74. _sym_db.RegisterMessage(message_class)
  75. return message_class
  76. # top level enums
  77. for (name, enum_des) in file_des.enum_types_by_name.items():
  78. module['_' + name.upper()] = enum_des
  79. module[name] = enum_type_wrapper.EnumTypeWrapper(enum_des)
  80. for enum_value in enum_des.values:
  81. module[enum_value.name] = enum_value.number
  82. # top level extensions
  83. for (name, extension_des) in file_des.extensions_by_name.items():
  84. module[name.upper() + '_FIELD_NUMBER'] = extension_des.number
  85. module[name] = extension_des
  86. # services
  87. for (name, service) in file_des.services_by_name.items():
  88. module['_' + name.upper()] = service
  89. # Build messages.
  90. for (name, msg_des) in file_des.message_types_by_name.items():
  91. module[name] = BuildMessage(msg_des)
  92. def AddHelpersToExtensions(file_des):
  93. """no-op to keep old generated code work with new runtime.
  94. Args:
  95. file_des: FileDescriptor of the .proto file
  96. """
  97. # TODO(b/279930766): Remove this on-op
  98. return
  99. def BuildServices(file_des, module_name, module):
  100. """Builds services classes and services stub class.
  101. Args:
  102. file_des: FileDescriptor of the .proto file
  103. module_name: str, the name of generated _pb2 module
  104. module: Generated _pb2 module
  105. """
  106. # pylint: disable=g-import-not-at-top
  107. from google.protobuf import service as _service
  108. from google.protobuf import service_reflection
  109. # pylint: enable=g-import-not-at-top
  110. for (name, service) in file_des.services_by_name.items():
  111. module[name] = service_reflection.GeneratedServiceType(
  112. name, (_service.Service,),
  113. dict(DESCRIPTOR=service, __module__=module_name))
  114. stub_name = name + '_Stub'
  115. module[stub_name] = service_reflection.GeneratedServiceStubType(
  116. stub_name, (module[name],),
  117. dict(DESCRIPTOR=service, __module__=module_name))