Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/message_factory.py: 29%

62 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

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 

31"""Provides a factory class for generating dynamic messages. 

32 

33The easiest way to use this class is if you have access to the FileDescriptor 

34protos containing the messages you want to create you can just do the following: 

35 

36message_classes = message_factory.GetMessages(iterable_of_file_descriptors) 

37my_proto_instance = message_classes['some.proto.package.MessageName']() 

38""" 

39 

40__author__ = 'matthewtoia@google.com (Matt Toia)' 

41 

42import warnings 

43 

44from google.protobuf.internal import api_implementation 

45from google.protobuf import descriptor_pool 

46from google.protobuf import message 

47 

48if api_implementation.Type() == 'python': 

49 from google.protobuf.internal import python_message as message_impl 

50else: 

51 from google.protobuf.pyext import cpp_message as message_impl # pylint: disable=g-import-not-at-top 

52 

53 

54# The type of all Message classes. 

55_GENERATED_PROTOCOL_MESSAGE_TYPE = message_impl.GeneratedProtocolMessageType 

56 

57 

58def GetMessageClass(descriptor): 

59 """Obtains a proto2 message class based on the passed in descriptor. 

60 

61 Passing a descriptor with a fully qualified name matching a previous 

62 invocation will cause the same class to be returned. 

63 

64 Args: 

65 descriptor: The descriptor to build from. 

66 

67 Returns: 

68 A class describing the passed in descriptor. 

69 """ 

70 concrete_class = getattr(descriptor, '_concrete_class', None) 

71 if concrete_class: 

72 return concrete_class 

73 return _InternalCreateMessageClass(descriptor) 

74 

75 

76def GetMessageClassesForFiles(files, pool): 

77 """Gets all the messages from specified files. 

78 

79 This will find and resolve dependencies, failing if the descriptor 

80 pool cannot satisfy them. 

81 

82 Args: 

83 files: The file names to extract messages from. 

84 pool: The descriptor pool to find the files including the dependent 

85 files. 

86 

87 Returns: 

88 A dictionary mapping proto names to the message classes. 

89 """ 

90 result = {} 

91 for file_name in files: 

92 file_desc = pool.FindFileByName(file_name) 

93 for desc in file_desc.message_types_by_name.values(): 

94 result[desc.full_name] = GetMessageClass(desc) 

95 

96 # While the extension FieldDescriptors are created by the descriptor pool, 

97 # the python classes created in the factory need them to be registered 

98 # explicitly, which is done below. 

99 # 

100 # The call to RegisterExtension will specifically check if the 

101 # extension was already registered on the object and either 

102 # ignore the registration if the original was the same, or raise 

103 # an error if they were different. 

104 

105 for extension in file_desc.extensions_by_name.values(): 

106 extended_class = GetMessageClass(extension.containing_type) 

107 extended_class.RegisterExtension(extension) 

108 # Recursively load protos for extension field, in order to be able to 

109 # fully represent the extension. This matches the behavior for regular 

110 # fields too. 

111 if extension.message_type: 

112 GetMessageClass(extension.message_type) 

113 return result 

114 

115 

116def _InternalCreateMessageClass(descriptor): 

117 """Builds a proto2 message class based on the passed in descriptor. 

118 

119 Args: 

120 descriptor: The descriptor to build from. 

121 

122 Returns: 

123 A class describing the passed in descriptor. 

124 """ 

125 descriptor_name = descriptor.name 

126 result_class = _GENERATED_PROTOCOL_MESSAGE_TYPE( 

127 descriptor_name, 

128 (message.Message,), 

129 { 

130 'DESCRIPTOR': descriptor, 

131 # If module not set, it wrongly points to message_factory module. 

132 '__module__': None, 

133 }) 

134 for field in descriptor.fields: 

135 if field.message_type: 

136 GetMessageClass(field.message_type) 

137 for extension in result_class.DESCRIPTOR.extensions: 

138 extended_class = GetMessageClass(extension.containing_type) 

139 extended_class.RegisterExtension(extension) 

140 if extension.message_type: 

141 GetMessageClass(extension.message_type) 

142 return result_class 

143 

144 

145# Deprecated. Please use GetMessageClass() or GetMessageClassesForFiles() 

146# method above instead. 

147class MessageFactory(object): 

148 """Factory for creating Proto2 messages from descriptors in a pool.""" 

149 

150 def __init__(self, pool=None): 

151 """Initializes a new factory.""" 

152 self.pool = pool or descriptor_pool.DescriptorPool() 

153 

154 def GetPrototype(self, descriptor): 

155 """Obtains a proto2 message class based on the passed in descriptor. 

156 

157 Passing a descriptor with a fully qualified name matching a previous 

158 invocation will cause the same class to be returned. 

159 

160 Args: 

161 descriptor: The descriptor to build from. 

162 

163 Returns: 

164 A class describing the passed in descriptor. 

165 """ 

166 warnings.warn('MessageFactory class is deprecated. Please use ' 

167 'GetMessageClass() instead of MessageFactory.GetPrototype. ' 

168 'MessageFactory class will be removed after 2024.') 

169 return GetMessageClass(descriptor) 

170 

171 def CreatePrototype(self, descriptor): 

172 """Builds a proto2 message class based on the passed in descriptor. 

173 

174 Don't call this function directly, it always creates a new class. Call 

175 GetMessageClass() instead. 

176 

177 Args: 

178 descriptor: The descriptor to build from. 

179 

180 Returns: 

181 A class describing the passed in descriptor. 

182 """ 

183 warnings.warn('Directly call CreatePrototype is wrong. Please use ' 

184 'GetMessageClass() method instead. Directly use ' 

185 'CreatePrototype will raise error after July 2023.') 

186 return _InternalCreateMessageClass(descriptor) 

187 

188 def GetMessages(self, files): 

189 """Gets all the messages from a specified file. 

190 

191 This will find and resolve dependencies, failing if the descriptor 

192 pool cannot satisfy them. 

193 

194 Args: 

195 files: The file names to extract messages from. 

196 

197 Returns: 

198 A dictionary mapping proto names to the message classes. This will include 

199 any dependent messages as well as any messages defined in the same file as 

200 a specified message. 

201 """ 

202 warnings.warn('MessageFactory class is deprecated. Please use ' 

203 'GetMessageClassesForFiles() instead of ' 

204 'MessageFactory.GetMessages(). MessageFactory class ' 

205 'will be removed after 2024.') 

206 return GetMessageClassesForFiles(files, self.pool) 

207 

208 

209def GetMessages(file_protos, pool=None): 

210 """Builds a dictionary of all the messages available in a set of files. 

211 

212 Args: 

213 file_protos: Iterable of FileDescriptorProto to build messages out of. 

214 pool: The descriptor pool to add the file protos. 

215 

216 Returns: 

217 A dictionary mapping proto names to the message classes. This will include 

218 any dependent messages as well as any messages defined in the same file as 

219 a specified message. 

220 """ 

221 # The cpp implementation of the protocol buffer library requires to add the 

222 # message in topological order of the dependency graph. 

223 des_pool = pool or descriptor_pool.DescriptorPool() 

224 file_by_name = {file_proto.name: file_proto for file_proto in file_protos} 

225 def _AddFile(file_proto): 

226 for dependency in file_proto.dependency: 

227 if dependency in file_by_name: 

228 # Remove from elements to be visited, in order to cut cycles. 

229 _AddFile(file_by_name.pop(dependency)) 

230 des_pool.Add(file_proto) 

231 while file_by_name: 

232 _AddFile(file_by_name.popitem()[1]) 

233 return GetMessageClassesForFiles( 

234 [file_proto.name for file_proto in file_protos], des_pool)