|
|
@ -39,6 +39,8 @@ my_proto_instance = message_classes['some.proto.package.MessageName']() |
|
|
|
|
|
|
|
|
|
|
|
__author__ = 'matthewtoia@google.com (Matt Toia)' |
|
|
|
__author__ = 'matthewtoia@google.com (Matt Toia)' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import warnings |
|
|
|
|
|
|
|
|
|
|
|
from google.protobuf.internal import api_implementation |
|
|
|
from google.protobuf.internal import api_implementation |
|
|
|
from google.protobuf import descriptor_pool |
|
|
|
from google.protobuf import descriptor_pool |
|
|
|
from google.protobuf import message |
|
|
|
from google.protobuf import message |
|
|
@ -53,6 +55,95 @@ else: |
|
|
|
_GENERATED_PROTOCOL_MESSAGE_TYPE = message_impl.GeneratedProtocolMessageType |
|
|
|
_GENERATED_PROTOCOL_MESSAGE_TYPE = message_impl.GeneratedProtocolMessageType |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def GetMessageClass(descriptor): |
|
|
|
|
|
|
|
"""Obtains a proto2 message class based on the passed in descriptor. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Passing a descriptor with a fully qualified name matching a previous |
|
|
|
|
|
|
|
invocation will cause the same class to be returned. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
|
|
|
descriptor: The descriptor to build from. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
A class describing the passed in descriptor. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
concrete_class = getattr(descriptor, '_concrete_class', None) |
|
|
|
|
|
|
|
if concrete_class: |
|
|
|
|
|
|
|
return concrete_class |
|
|
|
|
|
|
|
return _InternalCreateMessageClass(descriptor) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def GetMessageClassesForFiles(files, pool): |
|
|
|
|
|
|
|
"""Gets all the messages from specified files. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This will find and resolve dependencies, failing if the descriptor |
|
|
|
|
|
|
|
pool cannot satisfy them. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
|
|
|
files: The file names to extract messages from. |
|
|
|
|
|
|
|
pool: The descriptor pool to find the files including the dependent |
|
|
|
|
|
|
|
files. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
A dictionary mapping proto names to the message classes. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
result = {} |
|
|
|
|
|
|
|
for file_name in files: |
|
|
|
|
|
|
|
file_desc = pool.FindFileByName(file_name) |
|
|
|
|
|
|
|
for desc in file_desc.message_types_by_name.values(): |
|
|
|
|
|
|
|
result[desc.full_name] = GetMessageClass(desc) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# While the extension FieldDescriptors are created by the descriptor pool, |
|
|
|
|
|
|
|
# the python classes created in the factory need them to be registered |
|
|
|
|
|
|
|
# explicitly, which is done below. |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
# The call to RegisterExtension will specifically check if the |
|
|
|
|
|
|
|
# extension was already registered on the object and either |
|
|
|
|
|
|
|
# ignore the registration if the original was the same, or raise |
|
|
|
|
|
|
|
# an error if they were different. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for extension in file_desc.extensions_by_name.values(): |
|
|
|
|
|
|
|
extended_class = GetMessageClass(extension.containing_type) |
|
|
|
|
|
|
|
extended_class.RegisterExtension(extension) |
|
|
|
|
|
|
|
# Recursively load protos for extension field, in order to be able to |
|
|
|
|
|
|
|
# fully represent the extension. This matches the behavior for regular |
|
|
|
|
|
|
|
# fields too. |
|
|
|
|
|
|
|
if extension.message_type: |
|
|
|
|
|
|
|
GetMessageClass(extension.message_type) |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _InternalCreateMessageClass(descriptor): |
|
|
|
|
|
|
|
"""Builds a proto2 message class based on the passed in descriptor. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
|
|
|
descriptor: The descriptor to build from. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
|
|
|
|
A class describing the passed in descriptor. |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
descriptor_name = descriptor.name |
|
|
|
|
|
|
|
result_class = _GENERATED_PROTOCOL_MESSAGE_TYPE( |
|
|
|
|
|
|
|
descriptor_name, |
|
|
|
|
|
|
|
(message.Message,), |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
'DESCRIPTOR': descriptor, |
|
|
|
|
|
|
|
# If module not set, it wrongly points to message_factory module. |
|
|
|
|
|
|
|
'__module__': None, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
for field in descriptor.fields: |
|
|
|
|
|
|
|
if field.message_type: |
|
|
|
|
|
|
|
GetMessageClass(field.message_type) |
|
|
|
|
|
|
|
for extension in result_class.DESCRIPTOR.extensions: |
|
|
|
|
|
|
|
extended_class = GetMessageClass(extension.containing_type) |
|
|
|
|
|
|
|
extended_class.RegisterExtension(extension) |
|
|
|
|
|
|
|
if extension.message_type: |
|
|
|
|
|
|
|
GetMessageClass(extension.message_type) |
|
|
|
|
|
|
|
return result_class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Deprecated. Please use GetMessageClass() or GetMessageClassesForFiles() |
|
|
|
|
|
|
|
# method above instead. |
|
|
|
class MessageFactory(object): |
|
|
|
class MessageFactory(object): |
|
|
|
"""Factory for creating Proto2 messages from descriptors in a pool.""" |
|
|
|
"""Factory for creating Proto2 messages from descriptors in a pool.""" |
|
|
|
|
|
|
|
|
|
|
@ -72,18 +163,17 @@ class MessageFactory(object): |
|
|
|
Returns: |
|
|
|
Returns: |
|
|
|
A class describing the passed in descriptor. |
|
|
|
A class describing the passed in descriptor. |
|
|
|
""" |
|
|
|
""" |
|
|
|
concrete_class = getattr(descriptor, '_concrete_class', None) |
|
|
|
# TODO(b/258832141): add this warning |
|
|
|
if concrete_class: |
|
|
|
# warnings.warn('MessageFactory class is deprecated. Please use ' |
|
|
|
return concrete_class |
|
|
|
# 'GetMessageClass() instead of MessageFactory.GetPrototype. ' |
|
|
|
result_class = self.CreatePrototype(descriptor) |
|
|
|
# 'MessageFactory class will be removed after 2024.') |
|
|
|
return result_class |
|
|
|
return GetMessageClass(descriptor) |
|
|
|
|
|
|
|
|
|
|
|
def CreatePrototype(self, descriptor): |
|
|
|
def CreatePrototype(self, descriptor): |
|
|
|
"""Builds a proto2 message class based on the passed in descriptor. |
|
|
|
"""Builds a proto2 message class based on the passed in descriptor. |
|
|
|
|
|
|
|
|
|
|
|
Don't call this function directly, it always creates a new class. Call |
|
|
|
Don't call this function directly, it always creates a new class. Call |
|
|
|
GetPrototype() instead. This method is meant to be overridden in subblasses |
|
|
|
GetMessageClass() instead. |
|
|
|
to perform additional operations on the newly constructed class. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
Args: |
|
|
|
descriptor: The descriptor to build from. |
|
|
|
descriptor: The descriptor to build from. |
|
|
@ -91,25 +181,11 @@ class MessageFactory(object): |
|
|
|
Returns: |
|
|
|
Returns: |
|
|
|
A class describing the passed in descriptor. |
|
|
|
A class describing the passed in descriptor. |
|
|
|
""" |
|
|
|
""" |
|
|
|
descriptor_name = descriptor.name |
|
|
|
# TODO(b/258832141): add this warning |
|
|
|
result_class = _GENERATED_PROTOCOL_MESSAGE_TYPE( |
|
|
|
# warnings.warn('Directly call CreatePrototype is wrong. Please use ' |
|
|
|
descriptor_name, |
|
|
|
# 'GetMessageClass() method instead. Directly use ' |
|
|
|
(message.Message,), |
|
|
|
# 'CreatePrototype will raise error after July 2023.') |
|
|
|
{ |
|
|
|
return _InternalCreateMessageClass(descriptor) |
|
|
|
'DESCRIPTOR': descriptor, |
|
|
|
|
|
|
|
# If module not set, it wrongly points to message_factory module. |
|
|
|
|
|
|
|
'__module__': None, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
result_class._FACTORY = self # pylint: disable=protected-access |
|
|
|
|
|
|
|
for field in descriptor.fields: |
|
|
|
|
|
|
|
if field.message_type: |
|
|
|
|
|
|
|
self.GetPrototype(field.message_type) |
|
|
|
|
|
|
|
for extension in result_class.DESCRIPTOR.extensions: |
|
|
|
|
|
|
|
extended_class = self.GetPrototype(extension.containing_type) |
|
|
|
|
|
|
|
extended_class.RegisterExtension(extension) |
|
|
|
|
|
|
|
if extension.message_type: |
|
|
|
|
|
|
|
self.GetPrototype(extension.message_type) |
|
|
|
|
|
|
|
return result_class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def GetMessages(self, files): |
|
|
|
def GetMessages(self, files): |
|
|
|
"""Gets all the messages from a specified file. |
|
|
|
"""Gets all the messages from a specified file. |
|
|
@ -125,37 +201,20 @@ class MessageFactory(object): |
|
|
|
any dependent messages as well as any messages defined in the same file as |
|
|
|
any dependent messages as well as any messages defined in the same file as |
|
|
|
a specified message. |
|
|
|
a specified message. |
|
|
|
""" |
|
|
|
""" |
|
|
|
result = {} |
|
|
|
# TODO(b/258832141): add this warning |
|
|
|
for file_name in files: |
|
|
|
# warnings.warn('MessageFactory class is deprecated. Please use ' |
|
|
|
file_desc = self.pool.FindFileByName(file_name) |
|
|
|
# 'GetMessageClassesForFiles() instead of ' |
|
|
|
for desc in file_desc.message_types_by_name.values(): |
|
|
|
# 'MessageFactory.GetMessages(). MessageFactory class ' |
|
|
|
result[desc.full_name] = self.GetPrototype(desc) |
|
|
|
# 'will be removed after 2024.') |
|
|
|
|
|
|
|
return GetMessageClassesForFiles(files, self.pool) |
|
|
|
# While the extension FieldDescriptors are created by the descriptor pool, |
|
|
|
|
|
|
|
# the python classes created in the factory need them to be registered |
|
|
|
|
|
|
|
# explicitly, which is done below. |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
# The call to RegisterExtension will specifically check if the |
|
|
|
|
|
|
|
# extension was already registered on the object and either |
|
|
|
|
|
|
|
# ignore the registration if the original was the same, or raise |
|
|
|
|
|
|
|
# an error if they were different. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for extension in file_desc.extensions_by_name.values(): |
|
|
|
|
|
|
|
extended_class = self.GetPrototype(extension.containing_type) |
|
|
|
|
|
|
|
extended_class.RegisterExtension(extension) |
|
|
|
|
|
|
|
if extension.message_type: |
|
|
|
|
|
|
|
self.GetPrototype(extension.message_type) |
|
|
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_FACTORY = MessageFactory() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def GetMessages(file_protos): |
|
|
|
def GetMessages(file_protos, pool=None): |
|
|
|
"""Builds a dictionary of all the messages available in a set of files. |
|
|
|
"""Builds a dictionary of all the messages available in a set of files. |
|
|
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
Args: |
|
|
|
file_protos: Iterable of FileDescriptorProto to build messages out of. |
|
|
|
file_protos: Iterable of FileDescriptorProto to build messages out of. |
|
|
|
|
|
|
|
pool: The descriptor pool to add the file protos. |
|
|
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
Returns: |
|
|
|
A dictionary mapping proto names to the message classes. This will include |
|
|
|
A dictionary mapping proto names to the message classes. This will include |
|
|
@ -164,13 +223,15 @@ def GetMessages(file_protos): |
|
|
|
""" |
|
|
|
""" |
|
|
|
# The cpp implementation of the protocol buffer library requires to add the |
|
|
|
# The cpp implementation of the protocol buffer library requires to add the |
|
|
|
# message in topological order of the dependency graph. |
|
|
|
# message in topological order of the dependency graph. |
|
|
|
|
|
|
|
des_pool = pool or descriptor_pool.DescriptorPool() |
|
|
|
file_by_name = {file_proto.name: file_proto for file_proto in file_protos} |
|
|
|
file_by_name = {file_proto.name: file_proto for file_proto in file_protos} |
|
|
|
def _AddFile(file_proto): |
|
|
|
def _AddFile(file_proto): |
|
|
|
for dependency in file_proto.dependency: |
|
|
|
for dependency in file_proto.dependency: |
|
|
|
if dependency in file_by_name: |
|
|
|
if dependency in file_by_name: |
|
|
|
# Remove from elements to be visited, in order to cut cycles. |
|
|
|
# Remove from elements to be visited, in order to cut cycles. |
|
|
|
_AddFile(file_by_name.pop(dependency)) |
|
|
|
_AddFile(file_by_name.pop(dependency)) |
|
|
|
_FACTORY.pool.Add(file_proto) |
|
|
|
des_pool.Add(file_proto) |
|
|
|
while file_by_name: |
|
|
|
while file_by_name: |
|
|
|
_AddFile(file_by_name.popitem()[1]) |
|
|
|
_AddFile(file_by_name.popitem()[1]) |
|
|
|
return _FACTORY.GetMessages([file_proto.name for file_proto in file_protos]) |
|
|
|
return GetMessageClassesForFiles( |
|
|
|
|
|
|
|
[file_proto.name for file_proto in file_protos], des_pool) |
|
|
|