图片解析应用
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.

256 lines
9.4 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. """Provides a factory class for generating dynamic messages.
  31. The easiest way to use this class is if you have access to the FileDescriptor
  32. protos containing the messages you want to create you can just do the following:
  33. message_classes = message_factory.GetMessages(iterable_of_file_descriptors)
  34. my_proto_instance = message_classes['some.proto.package.MessageName']()
  35. """
  36. __author__ = 'matthewtoia@google.com (Matt Toia)'
  37. import warnings
  38. from google.protobuf.internal import api_implementation
  39. from google.protobuf import descriptor_pool
  40. from google.protobuf import message
  41. if api_implementation.Type() == 'python':
  42. from google.protobuf.internal import python_message as message_impl
  43. else:
  44. from google.protobuf.pyext import cpp_message as message_impl # pylint: disable=g-import-not-at-top
  45. # The type of all Message classes.
  46. _GENERATED_PROTOCOL_MESSAGE_TYPE = message_impl.GeneratedProtocolMessageType
  47. def GetMessageClass(descriptor):
  48. """Obtains a proto2 message class based on the passed in descriptor.
  49. Passing a descriptor with a fully qualified name matching a previous
  50. invocation will cause the same class to be returned.
  51. Args:
  52. descriptor: The descriptor to build from.
  53. Returns:
  54. A class describing the passed in descriptor.
  55. """
  56. concrete_class = getattr(descriptor, '_concrete_class', None)
  57. if concrete_class:
  58. return concrete_class
  59. return _InternalCreateMessageClass(descriptor)
  60. def GetMessageClassesForFiles(files, pool):
  61. """Gets all the messages from specified files.
  62. This will find and resolve dependencies, failing if the descriptor
  63. pool cannot satisfy them.
  64. Args:
  65. files: The file names to extract messages from.
  66. pool: The descriptor pool to find the files including the dependent
  67. files.
  68. Returns:
  69. A dictionary mapping proto names to the message classes.
  70. """
  71. result = {}
  72. for file_name in files:
  73. file_desc = pool.FindFileByName(file_name)
  74. for desc in file_desc.message_types_by_name.values():
  75. result[desc.full_name] = GetMessageClass(desc)
  76. # While the extension FieldDescriptors are created by the descriptor pool,
  77. # the python classes created in the factory need them to be registered
  78. # explicitly, which is done below.
  79. #
  80. # The call to RegisterExtension will specifically check if the
  81. # extension was already registered on the object and either
  82. # ignore the registration if the original was the same, or raise
  83. # an error if they were different.
  84. for extension in file_desc.extensions_by_name.values():
  85. extended_class = GetMessageClass(extension.containing_type)
  86. if api_implementation.Type() != 'python':
  87. # TODO(b/286443080): Remove this check here. Duplicate extension
  88. # register check should be in descriptor_pool.
  89. if extension is not pool.FindExtensionByNumber(
  90. extension.containing_type, extension.number
  91. ):
  92. raise ValueError('Double registration of Extensions')
  93. # Recursively load protos for extension field, in order to be able to
  94. # fully represent the extension. This matches the behavior for regular
  95. # fields too.
  96. if extension.message_type:
  97. GetMessageClass(extension.message_type)
  98. return result
  99. def _InternalCreateMessageClass(descriptor):
  100. """Builds a proto2 message class based on the passed in descriptor.
  101. Args:
  102. descriptor: The descriptor to build from.
  103. Returns:
  104. A class describing the passed in descriptor.
  105. """
  106. descriptor_name = descriptor.name
  107. result_class = _GENERATED_PROTOCOL_MESSAGE_TYPE(
  108. descriptor_name,
  109. (message.Message,),
  110. {
  111. 'DESCRIPTOR': descriptor,
  112. # If module not set, it wrongly points to message_factory module.
  113. '__module__': None,
  114. })
  115. for field in descriptor.fields:
  116. if field.message_type:
  117. GetMessageClass(field.message_type)
  118. for extension in result_class.DESCRIPTOR.extensions:
  119. extended_class = GetMessageClass(extension.containing_type)
  120. if api_implementation.Type() != 'python':
  121. # TODO(b/286443080): Remove this check here. Duplicate extension
  122. # register check should be in descriptor_pool.
  123. pool = extension.containing_type.file.pool
  124. if extension is not pool.FindExtensionByNumber(
  125. extension.containing_type, extension.number
  126. ):
  127. raise ValueError('Double registration of Extensions')
  128. if extension.message_type:
  129. GetMessageClass(extension.message_type)
  130. return result_class
  131. # Deprecated. Please use GetMessageClass() or GetMessageClassesForFiles()
  132. # method above instead.
  133. class MessageFactory(object):
  134. """Factory for creating Proto2 messages from descriptors in a pool."""
  135. def __init__(self, pool=None):
  136. """Initializes a new factory."""
  137. self.pool = pool or descriptor_pool.DescriptorPool()
  138. def GetPrototype(self, descriptor):
  139. """Obtains a proto2 message class based on the passed in descriptor.
  140. Passing a descriptor with a fully qualified name matching a previous
  141. invocation will cause the same class to be returned.
  142. Args:
  143. descriptor: The descriptor to build from.
  144. Returns:
  145. A class describing the passed in descriptor.
  146. """
  147. warnings.warn(
  148. 'MessageFactory class is deprecated. Please use '
  149. 'GetMessageClass() instead of MessageFactory.GetPrototype. '
  150. 'MessageFactory class will be removed after 2024.',
  151. stacklevel=2,
  152. )
  153. return GetMessageClass(descriptor)
  154. def CreatePrototype(self, descriptor):
  155. """Builds a proto2 message class based on the passed in descriptor.
  156. Don't call this function directly, it always creates a new class. Call
  157. GetMessageClass() instead.
  158. Args:
  159. descriptor: The descriptor to build from.
  160. Returns:
  161. A class describing the passed in descriptor.
  162. """
  163. warnings.warn(
  164. 'Directly call CreatePrototype is wrong. Please use '
  165. 'GetMessageClass() method instead. Directly use '
  166. 'CreatePrototype will raise error after July 2023.',
  167. stacklevel=2,
  168. )
  169. return _InternalCreateMessageClass(descriptor)
  170. def GetMessages(self, files):
  171. """Gets all the messages from a specified file.
  172. This will find and resolve dependencies, failing if the descriptor
  173. pool cannot satisfy them.
  174. Args:
  175. files: The file names to extract messages from.
  176. Returns:
  177. A dictionary mapping proto names to the message classes. This will include
  178. any dependent messages as well as any messages defined in the same file as
  179. a specified message.
  180. """
  181. warnings.warn(
  182. 'MessageFactory class is deprecated. Please use '
  183. 'GetMessageClassesForFiles() instead of '
  184. 'MessageFactory.GetMessages(). MessageFactory class '
  185. 'will be removed after 2024.',
  186. stacklevel=2,
  187. )
  188. return GetMessageClassesForFiles(files, self.pool)
  189. def GetMessages(file_protos, pool=None):
  190. """Builds a dictionary of all the messages available in a set of files.
  191. Args:
  192. file_protos: Iterable of FileDescriptorProto to build messages out of.
  193. pool: The descriptor pool to add the file protos.
  194. Returns:
  195. A dictionary mapping proto names to the message classes. This will include
  196. any dependent messages as well as any messages defined in the same file as
  197. a specified message.
  198. """
  199. # The cpp implementation of the protocol buffer library requires to add the
  200. # message in topological order of the dependency graph.
  201. des_pool = pool or descriptor_pool.DescriptorPool()
  202. file_by_name = {file_proto.name: file_proto for file_proto in file_protos}
  203. def _AddFile(file_proto):
  204. for dependency in file_proto.dependency:
  205. if dependency in file_by_name:
  206. # Remove from elements to be visited, in order to cut cycles.
  207. _AddFile(file_by_name.pop(dependency))
  208. des_pool.Add(file_proto)
  209. while file_by_name:
  210. _AddFile(file_by_name.popitem()[1])
  211. return GetMessageClassesForFiles(
  212. [file_proto.name for file_proto in file_protos], des_pool)