App Engine Python SDK version 1.9.2
[gae.git] / python / google / net / proto2 / python / public / symbol_database.py
blob6de0afa8de3482c315a9fec36a17270f08636653
1 #!/usr/bin/env python
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
23 pool.
25 Example usage:
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
35 # their name:
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')()
46 """
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.
59 """
61 def __init__(self):
62 """Constructor."""
64 self._symbols = {}
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.
71 Args:
72 message: a message.Message, to be registered.
74 Returns:
75 The provided message.
76 """
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)
84 return message
86 def RegisterEnumDescriptor(self, enum_descriptor):
87 """Registers the given enum descriptor in the local database.
89 Args:
90 enum_descriptor: a descriptor.EnumDescriptor.
92 Returns:
93 The provided descriptor.
94 """
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.
101 Args:
102 file_descriptor: a descriptor.FileDescriptor.
104 Returns:
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.
115 Args:
116 symbol: A str, a protocol buffer symbol.
118 Returns:
119 A Python class corresponding to the symbol.
121 Raises:
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.
133 Args:
134 descriptor: The descriptor to build from.
136 Returns:
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.
149 Args:
150 files: The file names to extract messages from.
152 Returns:
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
155 a specified message.
157 Raises:
158 KeyError: if a file could not be found.
161 result = {}
162 for f in files:
163 result.update(self._symbols_by_file[f])
164 return result
166 _DEFAULT = SymbolDatabase()
169 def Default():
170 """Returns the default SymbolDatabase."""
171 return _DEFAULT