message_factory.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. """Provides a factory class for generating dynamic messages.
  8. The easiest way to use this class is if you have access to the FileDescriptor
  9. protos containing the messages you want to create you can just do the following:
  10. message_classes = message_factory.GetMessages(iterable_of_file_descriptors)
  11. my_proto_instance = message_classes['some.proto.package.MessageName']()
  12. """
  13. __author__ = 'matthewtoia@google.com (Matt Toia)'
  14. import warnings
  15. from google.protobuf import descriptor_pool
  16. from google.protobuf import message
  17. from google.protobuf.internal import api_implementation
  18. if api_implementation.Type() == 'python':
  19. from google.protobuf.internal import python_message as message_impl
  20. else:
  21. from google.protobuf.pyext import cpp_message as message_impl # pylint: disable=g-import-not-at-top
  22. # The type of all Message classes.
  23. _GENERATED_PROTOCOL_MESSAGE_TYPE = message_impl.GeneratedProtocolMessageType
  24. def GetMessageClass(descriptor):
  25. """Obtains a proto2 message class based on the passed in descriptor.
  26. Passing a descriptor with a fully qualified name matching a previous
  27. invocation will cause the same class to be returned.
  28. Args:
  29. descriptor: The descriptor to build from.
  30. Returns:
  31. A class describing the passed in descriptor.
  32. """
  33. concrete_class = getattr(descriptor, '_concrete_class', None)
  34. if concrete_class:
  35. return concrete_class
  36. return _InternalCreateMessageClass(descriptor)
  37. def GetMessageClassesForFiles(files, pool):
  38. """Gets all the messages from specified files.
  39. This will find and resolve dependencies, failing if the descriptor
  40. pool cannot satisfy them.
  41. This will not return the classes for nested types within those classes, for
  42. those, use GetMessageClass() on the nested types within their containing
  43. messages.
  44. For example, for the message:
  45. message NestedTypeMessage {
  46. message NestedType {
  47. string data = 1;
  48. }
  49. NestedType nested = 1;
  50. }
  51. NestedTypeMessage will be in the result, but not
  52. NestedTypeMessage.NestedType.
  53. Args:
  54. files: The file names to extract messages from.
  55. pool: The descriptor pool to find the files including the dependent files.
  56. Returns:
  57. A dictionary mapping proto names to the message classes.
  58. """
  59. result = {}
  60. for file_name in files:
  61. file_desc = pool.FindFileByName(file_name)
  62. for desc in file_desc.message_types_by_name.values():
  63. result[desc.full_name] = GetMessageClass(desc)
  64. # While the extension FieldDescriptors are created by the descriptor pool,
  65. # the python classes created in the factory need them to be registered
  66. # explicitly, which is done below.
  67. #
  68. # The call to RegisterExtension will specifically check if the
  69. # extension was already registered on the object and either
  70. # ignore the registration if the original was the same, or raise
  71. # an error if they were different.
  72. for extension in file_desc.extensions_by_name.values():
  73. _ = GetMessageClass(extension.containing_type)
  74. if api_implementation.Type() != 'python':
  75. # TODO: Remove this check here. Duplicate extension
  76. # register check should be in descriptor_pool.
  77. if extension is not pool.FindExtensionByNumber(
  78. extension.containing_type, extension.number
  79. ):
  80. raise ValueError('Double registration of Extensions')
  81. # Recursively load protos for extension field, in order to be able to
  82. # fully represent the extension. This matches the behavior for regular
  83. # fields too.
  84. if extension.message_type:
  85. GetMessageClass(extension.message_type)
  86. return result
  87. def _InternalCreateMessageClass(descriptor):
  88. """Builds a proto2 message class based on the passed in descriptor.
  89. Args:
  90. descriptor: The descriptor to build from.
  91. Returns:
  92. A class describing the passed in descriptor.
  93. """
  94. descriptor_name = descriptor.name
  95. result_class = _GENERATED_PROTOCOL_MESSAGE_TYPE(
  96. descriptor_name,
  97. (message.Message,),
  98. {
  99. 'DESCRIPTOR': descriptor,
  100. # If module not set, it wrongly points to message_factory module.
  101. '__module__': None,
  102. },
  103. )
  104. for field in descriptor.fields:
  105. if field.message_type:
  106. GetMessageClass(field.message_type)
  107. for extension in result_class.DESCRIPTOR.extensions:
  108. extended_class = GetMessageClass(extension.containing_type)
  109. if api_implementation.Type() != 'python':
  110. # TODO: Remove this check here. Duplicate extension
  111. # register check should be in descriptor_pool.
  112. pool = extension.containing_type.file.pool
  113. if extension is not pool.FindExtensionByNumber(
  114. extension.containing_type, extension.number
  115. ):
  116. raise ValueError('Double registration of Extensions')
  117. if extension.message_type:
  118. GetMessageClass(extension.message_type)
  119. return result_class
  120. # Deprecated. Please use GetMessageClass() or GetMessageClassesForFiles()
  121. # method above instead.
  122. class MessageFactory(object):
  123. """Factory for creating Proto2 messages from descriptors in a pool."""
  124. def __init__(self, pool=None):
  125. """Initializes a new factory."""
  126. self.pool = pool or descriptor_pool.DescriptorPool()
  127. def GetMessages(file_protos, pool=None):
  128. """Builds a dictionary of all the messages available in a set of files.
  129. Args:
  130. file_protos: Iterable of FileDescriptorProto to build messages out of.
  131. pool: The descriptor pool to add the file protos.
  132. Returns:
  133. A dictionary mapping proto names to the message classes. This will include
  134. any dependent messages as well as any messages defined in the same file as
  135. a specified message.
  136. """
  137. # The cpp implementation of the protocol buffer library requires to add the
  138. # message in topological order of the dependency graph.
  139. des_pool = pool or descriptor_pool.DescriptorPool()
  140. file_by_name = {file_proto.name: file_proto for file_proto in file_protos}
  141. def _AddFile(file_proto):
  142. for dependency in file_proto.dependency:
  143. if dependency in file_by_name:
  144. # Remove from elements to be visited, in order to cut cycles.
  145. _AddFile(file_by_name.pop(dependency))
  146. des_pool.Add(file_proto)
  147. while file_by_name:
  148. _AddFile(file_by_name.popitem()[1])
  149. return GetMessageClassesForFiles(
  150. [file_proto.name for file_proto in file_protos], des_pool
  151. )