1
# Protocol Buffers - Google's data interchange format
2
# Copyright 2008 Google Inc. All rights reserved.
3
# http://code.google.com/p/protobuf/
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are
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
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.
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.
31
"""A database of Python protocol buffer generated symbols.
33
SymbolDatabase makes it easy to create new instances of a registered type, given
34
only the type's protocol buffer symbol name. Once all symbols are registered,
35
they can be accessed using either the MessageFactory interface which
36
SymbolDatabase exposes, or the DescriptorPool interface of the underlying
41
db = symbol_database.SymbolDatabase()
43
# Register symbols of interest, from one or multiple files.
44
db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR)
45
db.RegisterMessage(my_proto_pb2.MyMessage)
46
db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR)
48
# The database can be used as a MessageFactory, to generate types based on
50
types = db.GetMessages(['my_proto.proto'])
51
my_message_instance = types['MyMessage']()
53
# The database's underlying descriptor pool can be queried, so it's not
54
# necessary to know a type's filename to be able to generate it:
55
filename = db.pool.FindFileContainingSymbol('MyMessage')
56
my_message_instance = db.GetMessages([filename])['MyMessage']()
58
# This functionality is also provided directly via a convenience method:
59
my_message_instance = db.GetSymbol('MyMessage')()
63
from google.protobuf import descriptor_pool
66
class SymbolDatabase(object):
67
"""A database of Python generated symbols.
69
SymbolDatabase also models message_factory.MessageFactory.
71
The symbol database can be used to keep a global registry of all protocol
72
buffer types used within a program.
79
self._symbols_by_file = {}
80
self.pool = descriptor_pool.DescriptorPool()
82
def RegisterMessage(self, message):
83
"""Registers the given message type in the local database.
86
message: a message.Message, to be registered.
92
desc = message.DESCRIPTOR
93
self._symbols[desc.full_name] = message
94
if desc.file.name not in self._symbols_by_file:
95
self._symbols_by_file[desc.file.name] = {}
96
self._symbols_by_file[desc.file.name][desc.full_name] = message
97
self.pool.AddDescriptor(desc)
100
def RegisterEnumDescriptor(self, enum_descriptor):
101
"""Registers the given enum descriptor in the local database.
104
enum_descriptor: a descriptor.EnumDescriptor.
107
The provided descriptor.
109
self.pool.AddEnumDescriptor(enum_descriptor)
110
return enum_descriptor
112
def RegisterFileDescriptor(self, file_descriptor):
113
"""Registers the given file descriptor in the local database.
116
file_descriptor: a descriptor.FileDescriptor.
119
The provided descriptor.
121
self.pool.AddFileDescriptor(file_descriptor)
123
def GetSymbol(self, symbol):
124
"""Tries to find a symbol in the local database.
126
Currently, this method only returns message.Message instances, however, if
127
may be extended in future to support other symbol types.
130
symbol: A str, a protocol buffer symbol.
133
A Python class corresponding to the symbol.
136
KeyError: if the symbol could not be found.
139
return self._symbols[symbol]
141
def GetPrototype(self, descriptor):
142
"""Builds a proto2 message class based on the passed in descriptor.
144
Passing a descriptor with a fully qualified name matching a previous
145
invocation will cause the same class to be returned.
148
descriptor: The descriptor to build from.
151
A class describing the passed in descriptor.
154
return self.GetSymbol(descriptor.full_name)
156
def GetMessages(self, files):
157
"""Gets all the messages from a specified file.
159
This will find and resolve dependencies, failing if they are not registered
160
in the symbol database.
164
files: The file names to extract messages from.
167
A dictionary mapping proto names to the message classes. This will include
168
any dependent messages as well as any messages defined in the same file as
172
KeyError: if a file could not be found.
177
result.update(self._symbols_by_file[f])
180
_DEFAULT = SymbolDatabase()
184
"""Returns the default SymbolDatabase."""