| OLD | NEW |
| (Empty) |
| 1 # Protocol Buffers - Google's data interchange format | |
| 2 # Copyright 2008 Google Inc. All rights reserved. | |
| 3 # http://code.google.com/p/protobuf/ | |
| 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 package = file_desc_proto.package | |
| 69 for message in file_desc_proto.message_type: | |
| 70 self._file_desc_protos_by_symbol.update( | |
| 71 (name, file_desc_proto) for name in _ExtractSymbols(message, package)) | |
| 72 for enum in file_desc_proto.enum_type: | |
| 73 self._file_desc_protos_by_symbol[ | |
| 74 '.'.join((package, enum.name))] = file_desc_proto | |
| 75 | |
| 76 def FindFileByName(self, name): | |
| 77 """Finds the file descriptor proto by file name. | |
| 78 | |
| 79 Typically the file name is a relative path ending to a .proto file. The | |
| 80 proto with the given name will have to have been added to this database | |
| 81 using the Add method or else an error will be raised. | |
| 82 | |
| 83 Args: | |
| 84 name: The file name to find. | |
| 85 | |
| 86 Returns: | |
| 87 The file descriptor proto matching the name. | |
| 88 | |
| 89 Raises: | |
| 90 KeyError if no file by the given name was added. | |
| 91 """ | |
| 92 | |
| 93 return self._file_desc_protos_by_file[name] | |
| 94 | |
| 95 def FindFileContainingSymbol(self, symbol): | |
| 96 """Finds the file descriptor proto containing the specified symbol. | |
| 97 | |
| 98 The symbol should be a fully qualified name including the file descriptor's | |
| 99 package and any containing messages. Some examples: | |
| 100 | |
| 101 'some.package.name.Message' | |
| 102 'some.package.name.Message.NestedEnum' | |
| 103 | |
| 104 The file descriptor proto containing the specified symbol must be added to | |
| 105 this database using the Add method or else an error will be raised. | |
| 106 | |
| 107 Args: | |
| 108 symbol: The fully qualified symbol name. | |
| 109 | |
| 110 Returns: | |
| 111 The file descriptor proto containing the symbol. | |
| 112 | |
| 113 Raises: | |
| 114 KeyError if no file contains the specified symbol. | |
| 115 """ | |
| 116 | |
| 117 return self._file_desc_protos_by_symbol[symbol] | |
| 118 | |
| 119 | |
| 120 def _ExtractSymbols(desc_proto, package): | |
| 121 """Pulls out all the symbols from a descriptor proto. | |
| 122 | |
| 123 Args: | |
| 124 desc_proto: The proto to extract symbols from. | |
| 125 package: The package containing the descriptor type. | |
| 126 | |
| 127 Yields: | |
| 128 The fully qualified name found in the descriptor. | |
| 129 """ | |
| 130 | |
| 131 message_name = '.'.join((package, desc_proto.name)) | |
| 132 yield message_name | |
| 133 for nested_type in desc_proto.nested_type: | |
| 134 for symbol in _ExtractSymbols(nested_type, message_name): | |
| 135 yield symbol | |
| 136 for enum_type in desc_proto.enum_type: | |
| 137 yield '.'.join((message_name, enum_type.name)) | |
| OLD | NEW |