Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame^] | 1 | # Protocol Buffers - Google's data interchange format |
| 2 | # Copyright 2008 Google Inc. All rights reserved. |
| 3 | # https://developers.google.com/protocol-buffers/ |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 30 | |
| 31 | """Provides a container for DescriptorProtos.""" |
| 32 | |
| 33 | __author__ = 'matthewtoia@google.com (Matt Toia)' |
| 34 | |
| 35 | |
| 36 | class Error(Exception): |
| 37 | pass |
| 38 | |
| 39 | |
| 40 | class DescriptorDatabaseConflictingDefinitionError(Error): |
| 41 | """Raised when a proto is added with the same name & different descriptor.""" |
| 42 | |
| 43 | |
| 44 | class DescriptorDatabase(object): |
| 45 | """A container accepting FileDescriptorProtos and maps DescriptorProtos.""" |
| 46 | |
| 47 | def __init__(self): |
| 48 | self._file_desc_protos_by_file = {} |
| 49 | self._file_desc_protos_by_symbol = {} |
| 50 | |
| 51 | def Add(self, file_desc_proto): |
| 52 | """Adds the FileDescriptorProto and its types to this database. |
| 53 | |
| 54 | Args: |
| 55 | file_desc_proto: The FileDescriptorProto to add. |
| 56 | Raises: |
| 57 | DescriptorDatabaseException: if an attempt is made to add a proto |
| 58 | with the same name but different definition than an exisiting |
| 59 | proto in the database. |
| 60 | """ |
| 61 | proto_name = file_desc_proto.name |
| 62 | if proto_name not in self._file_desc_protos_by_file: |
| 63 | self._file_desc_protos_by_file[proto_name] = file_desc_proto |
| 64 | elif self._file_desc_protos_by_file[proto_name] != file_desc_proto: |
| 65 | raise DescriptorDatabaseConflictingDefinitionError( |
| 66 | '%s already added, but with different descriptor.' % proto_name) |
| 67 | |
| 68 | # Add the top-level Message, Enum and Extension descriptors to the index. |
| 69 | package = file_desc_proto.package |
| 70 | for message in file_desc_proto.message_type: |
| 71 | self._file_desc_protos_by_symbol.update( |
| 72 | (name, file_desc_proto) for name in _ExtractSymbols(message, package)) |
| 73 | for enum in file_desc_proto.enum_type: |
| 74 | self._file_desc_protos_by_symbol[ |
| 75 | '.'.join((package, enum.name))] = file_desc_proto |
| 76 | for extension in file_desc_proto.extension: |
| 77 | self._file_desc_protos_by_symbol[ |
| 78 | '.'.join((package, extension.name))] = file_desc_proto |
| 79 | |
| 80 | def FindFileByName(self, name): |
| 81 | """Finds the file descriptor proto by file name. |
| 82 | |
| 83 | Typically the file name is a relative path ending to a .proto file. The |
| 84 | proto with the given name will have to have been added to this database |
| 85 | using the Add method or else an error will be raised. |
| 86 | |
| 87 | Args: |
| 88 | name: The file name to find. |
| 89 | |
| 90 | Returns: |
| 91 | The file descriptor proto matching the name. |
| 92 | |
| 93 | Raises: |
| 94 | KeyError if no file by the given name was added. |
| 95 | """ |
| 96 | |
| 97 | return self._file_desc_protos_by_file[name] |
| 98 | |
| 99 | def FindFileContainingSymbol(self, symbol): |
| 100 | """Finds the file descriptor proto containing the specified symbol. |
| 101 | |
| 102 | The symbol should be a fully qualified name including the file descriptor's |
| 103 | package and any containing messages. Some examples: |
| 104 | |
| 105 | 'some.package.name.Message' |
| 106 | 'some.package.name.Message.NestedEnum' |
| 107 | |
| 108 | The file descriptor proto containing the specified symbol must be added to |
| 109 | this database using the Add method or else an error will be raised. |
| 110 | |
| 111 | Args: |
| 112 | symbol: The fully qualified symbol name. |
| 113 | |
| 114 | Returns: |
| 115 | The file descriptor proto containing the symbol. |
| 116 | |
| 117 | Raises: |
| 118 | KeyError if no file contains the specified symbol. |
| 119 | """ |
| 120 | |
| 121 | return self._file_desc_protos_by_symbol[symbol] |
| 122 | |
| 123 | |
| 124 | def _ExtractSymbols(desc_proto, package): |
| 125 | """Pulls out all the symbols from a descriptor proto. |
| 126 | |
| 127 | Args: |
| 128 | desc_proto: The proto to extract symbols from. |
| 129 | package: The package containing the descriptor type. |
| 130 | |
| 131 | Yields: |
| 132 | The fully qualified name found in the descriptor. |
| 133 | """ |
| 134 | |
| 135 | message_name = '.'.join((package, desc_proto.name)) |
| 136 | yield message_name |
| 137 | for nested_type in desc_proto.nested_type: |
| 138 | for symbol in _ExtractSymbols(nested_type, message_name): |
| 139 | yield symbol |
| 140 | for enum_type in desc_proto.enum_type: |
| 141 | yield '.'.join((message_name, enum_type.name)) |