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 # http://code.google.com/p/protobuf/ | 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. |
11 # * Redistributions in binary form must reproduce the above | 11 # * Redistributions in binary form must reproduce the above |
12 # copyright notice, this list of conditions and the following disclaimer | 12 # copyright notice, this list of conditions and the following disclaimer |
13 # in the documentation and/or other materials provided with the | 13 # in the documentation and/or other materials provided with the |
14 # distribution. | 14 # distribution. |
15 # * Neither the name of Google Inc. nor the names of its | 15 # * Neither the name of Google Inc. nor the names of its |
16 # contributors may be used to endorse or promote products derived from | 16 # contributors may be used to endorse or promote products derived from |
17 # this software without specific prior written permission. | 17 # this software without specific prior written permission. |
18 # | 18 # |
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
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 """Provides a factory class for generating dynamic messages.""" | 31 """Provides a factory class for generating dynamic messages. |
| 32 |
| 33 The easiest way to use this class is if you have access to the FileDescriptor |
| 34 protos containing the messages you want to create you can just do the following: |
| 35 |
| 36 message_classes = message_factory.GetMessages(iterable_of_file_descriptors) |
| 37 my_proto_instance = message_classes['some.proto.package.MessageName']() |
| 38 """ |
32 | 39 |
33 __author__ = 'matthewtoia@google.com (Matt Toia)' | 40 __author__ = 'matthewtoia@google.com (Matt Toia)' |
34 | 41 |
35 from google.protobuf import descriptor_database | |
36 from google.protobuf import descriptor_pool | 42 from google.protobuf import descriptor_pool |
37 from google.protobuf import message | 43 from google.protobuf import message |
38 from google.protobuf import reflection | 44 from google.protobuf import reflection |
39 | 45 |
40 | 46 |
41 class MessageFactory(object): | 47 class MessageFactory(object): |
42 """Factory for creating Proto2 messages from descriptors in a pool.""" | 48 """Factory for creating Proto2 messages from descriptors in a pool.""" |
43 | 49 |
44 def __init__(self): | 50 def __init__(self, pool=None): |
45 """Initializes a new factory.""" | 51 """Initializes a new factory.""" |
| 52 self.pool = pool or descriptor_pool.DescriptorPool() |
| 53 |
| 54 # local cache of all classes built from protobuf descriptors |
46 self._classes = {} | 55 self._classes = {} |
47 | 56 |
48 def GetPrototype(self, descriptor): | 57 def GetPrototype(self, descriptor): |
49 """Builds a proto2 message class based on the passed in descriptor. | 58 """Builds a proto2 message class based on the passed in descriptor. |
50 | 59 |
51 Passing a descriptor with a fully qualified name matching a previous | 60 Passing a descriptor with a fully qualified name matching a previous |
52 invocation will cause the same class to be returned. | 61 invocation will cause the same class to be returned. |
53 | 62 |
54 Args: | 63 Args: |
55 descriptor: The descriptor to build from. | 64 descriptor: The descriptor to build from. |
56 | 65 |
57 Returns: | 66 Returns: |
58 A class describing the passed in descriptor. | 67 A class describing the passed in descriptor. |
59 """ | 68 """ |
60 | |
61 if descriptor.full_name not in self._classes: | 69 if descriptor.full_name not in self._classes: |
| 70 descriptor_name = descriptor.name |
| 71 if str is bytes: # PY2 |
| 72 descriptor_name = descriptor.name.encode('ascii', 'ignore') |
62 result_class = reflection.GeneratedProtocolMessageType( | 73 result_class = reflection.GeneratedProtocolMessageType( |
63 descriptor.name.encode('ascii', 'ignore'), | 74 descriptor_name, |
64 (message.Message,), | 75 (message.Message,), |
65 {'DESCRIPTOR': descriptor}) | 76 {'DESCRIPTOR': descriptor, '__module__': None}) |
| 77 # If module not set, it wrongly points to the reflection.py module. |
66 self._classes[descriptor.full_name] = result_class | 78 self._classes[descriptor.full_name] = result_class |
67 for field in descriptor.fields: | 79 for field in descriptor.fields: |
68 if field.message_type: | 80 if field.message_type: |
69 self.GetPrototype(field.message_type) | 81 self.GetPrototype(field.message_type) |
| 82 for extension in result_class.DESCRIPTOR.extensions: |
| 83 if extension.containing_type.full_name not in self._classes: |
| 84 self.GetPrototype(extension.containing_type) |
| 85 extended_class = self._classes[extension.containing_type.full_name] |
| 86 extended_class.RegisterExtension(extension) |
70 return self._classes[descriptor.full_name] | 87 return self._classes[descriptor.full_name] |
71 | 88 |
| 89 def GetMessages(self, files): |
| 90 """Gets all the messages from a specified file. |
72 | 91 |
73 _DB = descriptor_database.DescriptorDatabase() | 92 This will find and resolve dependencies, failing if the descriptor |
74 _POOL = descriptor_pool.DescriptorPool(_DB) | 93 pool cannot satisfy them. |
| 94 |
| 95 Args: |
| 96 files: The file names to extract messages from. |
| 97 |
| 98 Returns: |
| 99 A dictionary mapping proto names to the message classes. This will include |
| 100 any dependent messages as well as any messages defined in the same file as |
| 101 a specified message. |
| 102 """ |
| 103 result = {} |
| 104 for file_name in files: |
| 105 file_desc = self.pool.FindFileByName(file_name) |
| 106 for name, msg in file_desc.message_types_by_name.items(): |
| 107 if file_desc.package: |
| 108 full_name = '.'.join([file_desc.package, name]) |
| 109 else: |
| 110 full_name = msg.name |
| 111 result[full_name] = self.GetPrototype( |
| 112 self.pool.FindMessageTypeByName(full_name)) |
| 113 |
| 114 # While the extension FieldDescriptors are created by the descriptor pool, |
| 115 # the python classes created in the factory need them to be registered |
| 116 # explicitly, which is done below. |
| 117 # |
| 118 # The call to RegisterExtension will specifically check if the |
| 119 # extension was already registered on the object and either |
| 120 # ignore the registration if the original was the same, or raise |
| 121 # an error if they were different. |
| 122 |
| 123 for name, extension in file_desc.extensions_by_name.items(): |
| 124 if extension.containing_type.full_name not in self._classes: |
| 125 self.GetPrototype(extension.containing_type) |
| 126 extended_class = self._classes[extension.containing_type.full_name] |
| 127 extended_class.RegisterExtension(extension) |
| 128 return result |
| 129 |
| 130 |
75 _FACTORY = MessageFactory() | 131 _FACTORY = MessageFactory() |
76 | 132 |
77 | 133 |
78 def GetMessages(file_protos): | 134 def GetMessages(file_protos): |
79 """Builds a dictionary of all the messages available in a set of files. | 135 """Builds a dictionary of all the messages available in a set of files. |
80 | 136 |
81 Args: | 137 Args: |
82 file_protos: A sequence of file protos to build messages out of. | 138 file_protos: A sequence of file protos to build messages out of. |
83 | 139 |
84 Returns: | 140 Returns: |
85 A dictionary containing all the message types in the files mapping the | 141 A dictionary mapping proto names to the message classes. This will include |
86 fully qualified name to a Message subclass for the descriptor. | 142 any dependent messages as well as any messages defined in the same file as |
| 143 a specified message. |
87 """ | 144 """ |
88 | |
89 result = {} | |
90 for file_proto in file_protos: | 145 for file_proto in file_protos: |
91 _DB.Add(file_proto) | 146 _FACTORY.pool.Add(file_proto) |
92 for file_proto in file_protos: | 147 return _FACTORY.GetMessages([file_proto.name for file_proto in file_protos]) |
93 for desc in _GetAllDescriptors(file_proto.message_type, file_proto.package): | |
94 result[desc.full_name] = _FACTORY.GetPrototype(desc) | |
95 return result | |
96 | |
97 | |
98 def _GetAllDescriptors(desc_protos, package): | |
99 """Gets all levels of nested message types as a flattened list of descriptors. | |
100 | |
101 Args: | |
102 desc_protos: The descriptor protos to process. | |
103 package: The package where the protos are defined. | |
104 | |
105 Yields: | |
106 Each message descriptor for each nested type. | |
107 """ | |
108 | |
109 for desc_proto in desc_protos: | |
110 name = '.'.join((package, desc_proto.name)) | |
111 yield _POOL.FindMessageTypeByName(name) | |
112 for nested_desc in _GetAllDescriptors(desc_proto.nested_type, name): | |
113 yield nested_desc | |
OLD | NEW |