31 """Provides a container for DescriptorProtos."""
33 __author__ =
'matthewtoia@google.com (Matt Toia)'
38 class Error(Exception):
42 class DescriptorDatabaseConflictingDefinitionError(Error):
43 """Raised when a proto is added with the same name & different descriptor."""
47 """A container accepting FileDescriptorProtos and maps DescriptorProtos."""
53 def Add(self, file_desc_proto):
54 """Adds the FileDescriptorProto and its types to this database.
57 file_desc_proto: The FileDescriptorProto to add.
59 DescriptorDatabaseConflictingDefinitionError: if an attempt is made to
60 add a proto with the same name but different definition than an
61 existing proto in the database.
63 proto_name = file_desc_proto.name
68 '%s already added, but with different descriptor.' % proto_name)
73 package = file_desc_proto.package
74 for message
in file_desc_proto.message_type:
77 for enum
in file_desc_proto.enum_type:
78 self.
_AddSymbol((
'.'.join((package, enum.name))), file_desc_proto)
79 for enum_value
in enum.value:
81 '.'.join((package, enum_value.name))] = file_desc_proto
82 for extension
in file_desc_proto.extension:
83 self.
_AddSymbol((
'.'.join((package, extension.name))), file_desc_proto)
84 for service
in file_desc_proto.service:
85 self.
_AddSymbol((
'.'.join((package, service.name))), file_desc_proto)
88 """Finds the file descriptor proto by file name.
90 Typically the file name is a relative path ending to a .proto file. The
91 proto with the given name will have to have been added to this database
92 using the Add method or else an error will be raised.
95 name: The file name to find.
98 The file descriptor proto matching the name.
101 KeyError if no file by the given name was added.
107 """Finds the file descriptor proto containing the specified symbol.
109 The symbol should be a fully qualified name including the file descriptor's
110 package and any containing messages. Some examples:
112 'some.package.name.Message'
113 'some.package.name.Message.NestedEnum'
114 'some.package.name.Message.some_field'
116 The file descriptor proto containing the specified symbol must be added to
117 this database using the Add method or else an error will be raised.
120 symbol: The fully qualified symbol name.
123 The file descriptor proto containing the symbol.
126 KeyError if no file contains the specified symbol.
136 top_level, _, _ = symbol.rpartition(
'.')
141 raise KeyError(symbol)
153 warn_msg = (
'Conflict register for file "' + file_desc_proto.name +
155 ' is already defined in file "' +
157 warnings.warn(warn_msg, RuntimeWarning)
162 """Pulls out all the symbols from a descriptor proto.
165 desc_proto: The proto to extract symbols from.
166 package: The package containing the descriptor type.
169 The fully qualified name found in the descriptor.
171 message_name = package +
'.' + desc_proto.name
if package
else desc_proto.name
173 for nested_type
in desc_proto.nested_type:
176 for enum_type
in desc_proto.enum_type:
177 yield '.'.join((message_name, enum_type.name))