3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """A database of Python protocol buffer generated symbols.
19 SymbolDatabase makes it easy to create new instances of a registered type, given
20 only the type's protocol buffer symbol name. Once all symbols are registered,
21 they can be accessed using either the MessageFactory interface which
22 SymbolDatabase exposes, or the BasicDescriptorPool interface of the underlying
27 db = symbol_database.SymbolDatabase()
29 # Register symbols of interest, from one or multiple files.
30 db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR)
31 db.RegisterMessage(my_proto_pb2.MyMessage)
32 db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR)
34 # The database can be used as a MessageFactory, to generate types based on
36 types = db.GetMessages(['my_proto.proto'])
37 my_message_instance = types['MyMessage']()
39 # The database's underlying descriptor pool can be queried, so it's not
40 # necessary to know a type's filename to be able to generate it:
41 filename = db.pool.FindFileContainingSymbol('MyMessage')
42 my_message_instance = db.GetMessages([filename])['MyMessage']()
44 # This functionality is also provided directly via a convenience method:
45 my_message_instance = db.GetSymbol('MyMessage')()
49 from google
.net
.proto2
.python
.public
import basic_descriptor_pool
52 class SymbolDatabase(object):
53 """A database of Python generated symbols.
55 SymbolDatabase also models message_factory.MessageFactory.
57 The symbol database can be used to keep a global registry of all protocol
58 buffer types used within a program.
65 self
._symbols
_by
_file
= {}
66 self
.pool
= basic_descriptor_pool
.BasicDescriptorPool()
68 def RegisterMessage(self
, message
):
69 """Registers the given message type in the local database.
72 message: a message.Message, to be registered.
78 desc
= message
.DESCRIPTOR
79 self
._symbols
[desc
.full_name
] = message
80 if desc
.file.name
not in self
._symbols
_by
_file
:
81 self
._symbols
_by
_file
[desc
.file.name
] = {}
82 self
._symbols
_by
_file
[desc
.file.name
][desc
.full_name
] = message
83 self
.pool
.AddMessage(desc
)
86 def RegisterEnumDescriptor(self
, enum_descriptor
):
87 """Registers the given enum descriptor in the local database.
90 enum_descriptor: a descriptor.EnumDescriptor.
93 The provided descriptor.
95 self
.pool
.AddEnum(enum_descriptor
)
96 return enum_descriptor
98 def RegisterFileDescriptor(self
, file_descriptor
):
99 """Registers the given file descriptor in the local database.
102 file_descriptor: a descriptor.FileDescriptor.
105 The provided descriptor.
107 self
.pool
.AddFile(file_descriptor
)
109 def GetSymbol(self
, symbol
):
110 """Tries to find a symbol in the local database.
112 Currently, this method only returns message.Message instances, however, if
113 may be extended in future to support other symbol types.
116 symbol: A str, a protocol buffer symbol.
119 A Python class corresponding to the symbol.
122 KeyError: if the symbol could not be found.
125 return self
._symbols
[symbol
]
127 def GetPrototype(self
, descriptor
):
128 """Builds a proto2 message class based on the passed in descriptor.
130 Passing a descriptor with a fully qualified name matching a previous
131 invocation will cause the same class to be returned.
134 descriptor: The descriptor to build from.
137 A class describing the passed in descriptor.
140 return self
.GetSymbol(descriptor
.full_name
)
142 def GetMessages(self
, files
):
143 """Gets all the messages from a specified file.
145 This will find and resolve dependencies, failing if they are not registered
146 in the symbol database.
150 files: The file names to extract messages from.
153 A dictionary mapping proto names to the message classes. This will include
154 any dependent messages as well as any messages defined in the same file as
158 KeyError: if a file could not be found.
163 result
.update(self
._symbols
_by
_file
[f
])
166 _DEFAULT
= SymbolDatabase()
170 """Returns the default SymbolDatabase."""