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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 files: The file names to extract messages from. | 96 files: The file names to extract messages from. |
97 | 97 |
98 Returns: | 98 Returns: |
99 A dictionary mapping proto names to the message classes. This will include | 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 | 100 any dependent messages as well as any messages defined in the same file as |
101 a specified message. | 101 a specified message. |
102 """ | 102 """ |
103 result = {} | 103 result = {} |
104 for file_name in files: | 104 for file_name in files: |
105 file_desc = self.pool.FindFileByName(file_name) | 105 file_desc = self.pool.FindFileByName(file_name) |
106 for desc in file_desc.message_types_by_name.values(): | 106 for name, msg in file_desc.message_types_by_name.items(): |
107 result[desc.full_name] = self.GetPrototype(desc) | 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)) |
108 | 113 |
109 # While the extension FieldDescriptors are created by the descriptor pool, | 114 # While the extension FieldDescriptors are created by the descriptor pool, |
110 # the python classes created in the factory need them to be registered | 115 # the python classes created in the factory need them to be registered |
111 # explicitly, which is done below. | 116 # explicitly, which is done below. |
112 # | 117 # |
113 # The call to RegisterExtension will specifically check if the | 118 # The call to RegisterExtension will specifically check if the |
114 # extension was already registered on the object and either | 119 # extension was already registered on the object and either |
115 # ignore the registration if the original was the same, or raise | 120 # ignore the registration if the original was the same, or raise |
116 # an error if they were different. | 121 # an error if they were different. |
117 | 122 |
118 for extension in file_desc.extensions_by_name.values(): | 123 for name, extension in file_desc.extensions_by_name.items(): |
119 if extension.containing_type.full_name not in self._classes: | 124 if extension.containing_type.full_name not in self._classes: |
120 self.GetPrototype(extension.containing_type) | 125 self.GetPrototype(extension.containing_type) |
121 extended_class = self._classes[extension.containing_type.full_name] | 126 extended_class = self._classes[extension.containing_type.full_name] |
122 extended_class.RegisterExtension(extension) | 127 extended_class.RegisterExtension(extension) |
123 return result | 128 return result |
124 | 129 |
125 | 130 |
126 _FACTORY = MessageFactory() | 131 _FACTORY = MessageFactory() |
127 | 132 |
128 | 133 |
129 def GetMessages(file_protos): | 134 def GetMessages(file_protos): |
130 """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. |
131 | 136 |
132 Args: | 137 Args: |
133 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. |
134 | 139 |
135 Returns: | 140 Returns: |
136 A dictionary mapping proto names to the message classes. This will include | 141 A dictionary mapping proto names to the message classes. This will include |
137 any dependent messages as well as any messages defined in the same file as | 142 any dependent messages as well as any messages defined in the same file as |
138 a specified message. | 143 a specified message. |
139 """ | 144 """ |
140 for file_proto in file_protos: | 145 for file_proto in file_protos: |
141 _FACTORY.pool.Add(file_proto) | 146 _FACTORY.pool.Add(file_proto) |
142 return _FACTORY.GetMessages([file_proto.name for file_proto in file_protos]) | 147 return _FACTORY.GetMessages([file_proto.name for file_proto in file_protos]) |
OLD | NEW |