| OLD | NEW |
| 1 # Protocol Buffers - Google's data interchange format | 1 # Protocol Buffers - Google's data interchange format |
| 2 # Copyright 2008 Google Inc. All rights reserved. | 2 # Copyright 2008 Google Inc. All rights reserved. |
| 3 # https://developers.google.com/protocol-buffers/ | 3 # https://developers.google.com/protocol-buffers/ |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
| 7 # met: | 7 # met: |
| 8 # | 8 # |
| 9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 """A database of Python protocol buffer generated symbols. | 31 """A database of Python protocol buffer generated symbols. |
| 32 | 32 |
| 33 SymbolDatabase is the MessageFactory for messages generated at compile time, | 33 SymbolDatabase makes it easy to create new instances of a registered type, given |
| 34 and makes it easy to create new instances of a registered type, given only the | 34 only the type's protocol buffer symbol name. Once all symbols are registered, |
| 35 type's protocol buffer symbol name. | 35 they can be accessed using either the MessageFactory interface which |
| 36 SymbolDatabase exposes, or the DescriptorPool interface of the underlying |
| 37 pool. |
| 36 | 38 |
| 37 Example usage: | 39 Example usage: |
| 38 | 40 |
| 39 db = symbol_database.SymbolDatabase() | 41 db = symbol_database.SymbolDatabase() |
| 40 | 42 |
| 41 # Register symbols of interest, from one or multiple files. | 43 # Register symbols of interest, from one or multiple files. |
| 42 db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR) | 44 db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR) |
| 43 db.RegisterMessage(my_proto_pb2.MyMessage) | 45 db.RegisterMessage(my_proto_pb2.MyMessage) |
| 44 db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR) | 46 db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR) |
| 45 | 47 |
| 46 # The database can be used as a MessageFactory, to generate types based on | 48 # The database can be used as a MessageFactory, to generate types based on |
| 47 # their name: | 49 # their name: |
| 48 types = db.GetMessages(['my_proto.proto']) | 50 types = db.GetMessages(['my_proto.proto']) |
| 49 my_message_instance = types['MyMessage']() | 51 my_message_instance = types['MyMessage']() |
| 50 | 52 |
| 51 # The database's underlying descriptor pool can be queried, so it's not | 53 # The database's underlying descriptor pool can be queried, so it's not |
| 52 # necessary to know a type's filename to be able to generate it: | 54 # necessary to know a type's filename to be able to generate it: |
| 53 filename = db.pool.FindFileContainingSymbol('MyMessage') | 55 filename = db.pool.FindFileContainingSymbol('MyMessage') |
| 54 my_message_instance = db.GetMessages([filename])['MyMessage']() | 56 my_message_instance = db.GetMessages([filename])['MyMessage']() |
| 55 | 57 |
| 56 # This functionality is also provided directly via a convenience method: | 58 # This functionality is also provided directly via a convenience method: |
| 57 my_message_instance = db.GetSymbol('MyMessage')() | 59 my_message_instance = db.GetSymbol('MyMessage')() |
| 58 """ | 60 """ |
| 59 | 61 |
| 60 | 62 |
| 61 from google.protobuf import descriptor_pool | 63 from google.protobuf import descriptor_pool |
| 62 from google.protobuf import message_factory | |
| 63 | 64 |
| 64 | 65 |
| 65 class SymbolDatabase(message_factory.MessageFactory): | 66 class SymbolDatabase(object): |
| 66 """A database of Python generated symbols.""" | 67 """A database of Python generated symbols. |
| 68 |
| 69 SymbolDatabase also models message_factory.MessageFactory. |
| 70 |
| 71 The symbol database can be used to keep a global registry of all protocol |
| 72 buffer types used within a program. |
| 73 """ |
| 74 |
| 75 def __init__(self, pool=None): |
| 76 """Constructor.""" |
| 77 |
| 78 self._symbols = {} |
| 79 self._symbols_by_file = {} |
| 80 self.pool = pool or descriptor_pool.Default() |
| 67 | 81 |
| 68 def RegisterMessage(self, message): | 82 def RegisterMessage(self, message): |
| 69 """Registers the given message type in the local database. | 83 """Registers the given message type in the local database. |
| 70 | 84 |
| 71 Calls to GetSymbol() and GetMessages() will return messages registered here. | |
| 72 | |
| 73 Args: | 85 Args: |
| 74 message: a message.Message, to be registered. | 86 message: a message.Message, to be registered. |
| 75 | 87 |
| 76 Returns: | 88 Returns: |
| 77 The provided message. | 89 The provided message. |
| 78 """ | 90 """ |
| 79 | 91 |
| 80 desc = message.DESCRIPTOR | 92 desc = message.DESCRIPTOR |
| 81 self._classes[desc.full_name] = message | 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 |
| 82 self.pool.AddDescriptor(desc) | 97 self.pool.AddDescriptor(desc) |
| 83 return message | 98 return message |
| 84 | 99 |
| 85 def RegisterEnumDescriptor(self, enum_descriptor): | 100 def RegisterEnumDescriptor(self, enum_descriptor): |
| 86 """Registers the given enum descriptor in the local database. | 101 """Registers the given enum descriptor in the local database. |
| 87 | 102 |
| 88 Args: | 103 Args: |
| 89 enum_descriptor: a descriptor.EnumDescriptor. | 104 enum_descriptor: a descriptor.EnumDescriptor. |
| 90 | 105 |
| 91 Returns: | 106 Returns: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 114 Args: | 129 Args: |
| 115 symbol: A str, a protocol buffer symbol. | 130 symbol: A str, a protocol buffer symbol. |
| 116 | 131 |
| 117 Returns: | 132 Returns: |
| 118 A Python class corresponding to the symbol. | 133 A Python class corresponding to the symbol. |
| 119 | 134 |
| 120 Raises: | 135 Raises: |
| 121 KeyError: if the symbol could not be found. | 136 KeyError: if the symbol could not be found. |
| 122 """ | 137 """ |
| 123 | 138 |
| 124 return self._classes[symbol] | 139 return self._symbols[symbol] |
| 140 |
| 141 def GetPrototype(self, descriptor): |
| 142 """Builds a proto2 message class based on the passed in descriptor. |
| 143 |
| 144 Passing a descriptor with a fully qualified name matching a previous |
| 145 invocation will cause the same class to be returned. |
| 146 |
| 147 Args: |
| 148 descriptor: The descriptor to build from. |
| 149 |
| 150 Returns: |
| 151 A class describing the passed in descriptor. |
| 152 """ |
| 153 |
| 154 return self.GetSymbol(descriptor.full_name) |
| 125 | 155 |
| 126 def GetMessages(self, files): | 156 def GetMessages(self, files): |
| 127 # TODO(amauryfa): Fix the differences with MessageFactory. | 157 """Gets all the messages from a specified file. |
| 128 """Gets all registered messages from a specified file. | |
| 129 | 158 |
| 130 Only messages already created and registered will be returned; (this is the | 159 This will find and resolve dependencies, failing if they are not registered |
| 131 case for imported _pb2 modules) | 160 in the symbol database. |
| 132 But unlike MessageFactory, this version also returns already defined nested | 161 |
| 133 messages, but does not register any message extensions. | |
| 134 | 162 |
| 135 Args: | 163 Args: |
| 136 files: The file names to extract messages from. | 164 files: The file names to extract messages from. |
| 137 | 165 |
| 138 Returns: | 166 Returns: |
| 139 A dictionary mapping proto names to the message classes. | 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 |
| 169 a specified message. |
| 140 | 170 |
| 141 Raises: | 171 Raises: |
| 142 KeyError: if a file could not be found. | 172 KeyError: if a file could not be found. |
| 143 """ | 173 """ |
| 144 | 174 |
| 145 def _GetAllMessageNames(desc): | |
| 146 """Walk a message Descriptor and recursively yields all message names.""" | |
| 147 yield desc.full_name | |
| 148 for msg_desc in desc.nested_types: | |
| 149 for full_name in _GetAllMessageNames(msg_desc): | |
| 150 yield full_name | |
| 151 | |
| 152 result = {} | 175 result = {} |
| 153 for file_name in files: | 176 for f in files: |
| 154 file_desc = self.pool.FindFileByName(file_name) | 177 result.update(self._symbols_by_file[f]) |
| 155 for msg_desc in file_desc.message_types_by_name.values(): | |
| 156 for full_name in _GetAllMessageNames(msg_desc): | |
| 157 try: | |
| 158 result[full_name] = self._classes[full_name] | |
| 159 except KeyError: | |
| 160 # This descriptor has no registered class, skip it. | |
| 161 pass | |
| 162 return result | 178 return result |
| 163 | 179 |
| 164 | |
| 165 _DEFAULT = SymbolDatabase(pool=descriptor_pool.Default()) | 180 _DEFAULT = SymbolDatabase(pool=descriptor_pool.Default()) |
| 166 | 181 |
| 167 | 182 |
| 168 def Default(): | 183 def Default(): |
| 169 """Returns the default SymbolDatabase.""" | 184 """Returns the default SymbolDatabase.""" |
| 170 return _DEFAULT | 185 return _DEFAULT |
| OLD | NEW |