| 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 makes it easy to create new instances of a registered type, given | 33 SymbolDatabase is the MessageFactory for messages generated at compile time, |
| 34 only the type's protocol buffer symbol name. Once all symbols are registered, | 34 and makes it easy to create new instances of a registered type, given only the |
| 35 they can be accessed using either the MessageFactory interface which | 35 type's protocol buffer symbol name. |
| 36 SymbolDatabase exposes, or the DescriptorPool interface of the underlying | |
| 37 pool. | |
| 38 | 36 |
| 39 Example usage: | 37 Example usage: |
| 40 | 38 |
| 41 db = symbol_database.SymbolDatabase() | 39 db = symbol_database.SymbolDatabase() |
| 42 | 40 |
| 43 # Register symbols of interest, from one or multiple files. | 41 # Register symbols of interest, from one or multiple files. |
| 44 db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR) | 42 db.RegisterFileDescriptor(my_proto_pb2.DESCRIPTOR) |
| 45 db.RegisterMessage(my_proto_pb2.MyMessage) | 43 db.RegisterMessage(my_proto_pb2.MyMessage) |
| 46 db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR) | 44 db.RegisterEnumDescriptor(my_proto_pb2.MyEnum.DESCRIPTOR) |
| 47 | 45 |
| 48 # The database can be used as a MessageFactory, to generate types based on | 46 # The database can be used as a MessageFactory, to generate types based on |
| 49 # their name: | 47 # their name: |
| 50 types = db.GetMessages(['my_proto.proto']) | 48 types = db.GetMessages(['my_proto.proto']) |
| 51 my_message_instance = types['MyMessage']() | 49 my_message_instance = types['MyMessage']() |
| 52 | 50 |
| 53 # The database's underlying descriptor pool can be queried, so it's not | 51 # 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: | 52 # necessary to know a type's filename to be able to generate it: |
| 55 filename = db.pool.FindFileContainingSymbol('MyMessage') | 53 filename = db.pool.FindFileContainingSymbol('MyMessage') |
| 56 my_message_instance = db.GetMessages([filename])['MyMessage']() | 54 my_message_instance = db.GetMessages([filename])['MyMessage']() |
| 57 | 55 |
| 58 # This functionality is also provided directly via a convenience method: | 56 # This functionality is also provided directly via a convenience method: |
| 59 my_message_instance = db.GetSymbol('MyMessage')() | 57 my_message_instance = db.GetSymbol('MyMessage')() |
| 60 """ | 58 """ |
| 61 | 59 |
| 62 | 60 |
| 63 from google.protobuf import descriptor_pool | 61 from google.protobuf import descriptor_pool |
| 62 from google.protobuf import message_factory |
| 64 | 63 |
| 65 | 64 |
| 66 class SymbolDatabase(object): | 65 class SymbolDatabase(message_factory.MessageFactory): |
| 67 """A database of Python generated symbols. | 66 """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() | |
| 81 | 67 |
| 82 def RegisterMessage(self, message): | 68 def RegisterMessage(self, message): |
| 83 """Registers the given message type in the local database. | 69 """Registers the given message type in the local database. |
| 84 | 70 |
| 71 Calls to GetSymbol() and GetMessages() will return messages registered here. |
| 72 |
| 85 Args: | 73 Args: |
| 86 message: a message.Message, to be registered. | 74 message: a message.Message, to be registered. |
| 87 | 75 |
| 88 Returns: | 76 Returns: |
| 89 The provided message. | 77 The provided message. |
| 90 """ | 78 """ |
| 91 | 79 |
| 92 desc = message.DESCRIPTOR | 80 desc = message.DESCRIPTOR |
| 93 self._symbols[desc.full_name] = message | 81 self._classes[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) | 82 self.pool.AddDescriptor(desc) |
| 98 return message | 83 return message |
| 99 | 84 |
| 100 def RegisterEnumDescriptor(self, enum_descriptor): | 85 def RegisterEnumDescriptor(self, enum_descriptor): |
| 101 """Registers the given enum descriptor in the local database. | 86 """Registers the given enum descriptor in the local database. |
| 102 | 87 |
| 103 Args: | 88 Args: |
| 104 enum_descriptor: a descriptor.EnumDescriptor. | 89 enum_descriptor: a descriptor.EnumDescriptor. |
| 105 | 90 |
| 106 Returns: | 91 Returns: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 129 Args: | 114 Args: |
| 130 symbol: A str, a protocol buffer symbol. | 115 symbol: A str, a protocol buffer symbol. |
| 131 | 116 |
| 132 Returns: | 117 Returns: |
| 133 A Python class corresponding to the symbol. | 118 A Python class corresponding to the symbol. |
| 134 | 119 |
| 135 Raises: | 120 Raises: |
| 136 KeyError: if the symbol could not be found. | 121 KeyError: if the symbol could not be found. |
| 137 """ | 122 """ |
| 138 | 123 |
| 139 return self._symbols[symbol] | 124 return self._classes[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) | |
| 155 | 125 |
| 156 def GetMessages(self, files): | 126 def GetMessages(self, files): |
| 157 """Gets all the messages from a specified file. | 127 # TODO(amauryfa): Fix the differences with MessageFactory. |
| 128 """Gets all registered messages from a specified file. |
| 158 | 129 |
| 159 This will find and resolve dependencies, failing if they are not registered | 130 Only messages already created and registered will be returned; (this is the |
| 160 in the symbol database. | 131 case for imported _pb2 modules) |
| 161 | 132 But unlike MessageFactory, this version also returns nested messages. |
| 162 | 133 |
| 163 Args: | 134 Args: |
| 164 files: The file names to extract messages from. | 135 files: The file names to extract messages from. |
| 165 | 136 |
| 166 Returns: | 137 Returns: |
| 167 A dictionary mapping proto names to the message classes. This will include | 138 A dictionary mapping proto names to the message classes. |
| 168 any dependent messages as well as any messages defined in the same file as | |
| 169 a specified message. | |
| 170 | 139 |
| 171 Raises: | 140 Raises: |
| 172 KeyError: if a file could not be found. | 141 KeyError: if a file could not be found. |
| 173 """ | 142 """ |
| 174 | 143 |
| 144 def _GetAllMessageNames(desc): |
| 145 """Walk a message Descriptor and recursively yields all message names.""" |
| 146 yield desc.full_name |
| 147 for msg_desc in desc.nested_types: |
| 148 for full_name in _GetAllMessageNames(msg_desc): |
| 149 yield full_name |
| 150 |
| 175 result = {} | 151 result = {} |
| 176 for f in files: | 152 for file_name in files: |
| 177 result.update(self._symbols_by_file[f]) | 153 file_desc = self.pool.FindFileByName(file_name) |
| 154 for msg_desc in file_desc.message_types_by_name.values(): |
| 155 for full_name in _GetAllMessageNames(msg_desc): |
| 156 try: |
| 157 result[full_name] = self._classes[full_name] |
| 158 except KeyError: |
| 159 # This descriptor has no registered class, skip it. |
| 160 pass |
| 178 return result | 161 return result |
| 179 | 162 |
| 163 |
| 180 _DEFAULT = SymbolDatabase(pool=descriptor_pool.Default()) | 164 _DEFAULT = SymbolDatabase(pool=descriptor_pool.Default()) |
| 181 | 165 |
| 182 | 166 |
| 183 def Default(): | 167 def Default(): |
| 184 """Returns the default SymbolDatabase.""" | 168 """Returns the default SymbolDatabase.""" |
| 185 return _DEFAULT | 169 return _DEFAULT |
| OLD | NEW |