descriptor_database.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. """Provides a container for DescriptorProtos."""
  31. __author__ = 'matthewtoia@google.com (Matt Toia)'
  32. class Error(Exception):
  33. pass
  34. class DescriptorDatabaseConflictingDefinitionError(Error):
  35. """Raised when a proto is added with the same name & different descriptor."""
  36. class DescriptorDatabase(object):
  37. """A container accepting FileDescriptorProtos and maps DescriptorProtos."""
  38. def __init__(self):
  39. self._file_desc_protos_by_file = {}
  40. self._file_desc_protos_by_symbol = {}
  41. def Add(self, file_desc_proto):
  42. """Adds the FileDescriptorProto and its types to this database.
  43. Args:
  44. file_desc_proto: The FileDescriptorProto to add.
  45. Raises:
  46. DescriptorDatabaseConflictingDefinitionError: if an attempt is made to
  47. add a proto with the same name but different definition than an
  48. exisiting proto in the database.
  49. """
  50. proto_name = file_desc_proto.name
  51. if proto_name not in self._file_desc_protos_by_file:
  52. self._file_desc_protos_by_file[proto_name] = file_desc_proto
  53. elif self._file_desc_protos_by_file[proto_name] != file_desc_proto:
  54. raise DescriptorDatabaseConflictingDefinitionError(
  55. '%s already added, but with different descriptor.' % proto_name)
  56. # Add all the top-level descriptors to the index.
  57. package = file_desc_proto.package
  58. for message in file_desc_proto.message_type:
  59. self._file_desc_protos_by_symbol.update(
  60. (name, file_desc_proto) for name in _ExtractSymbols(message, package))
  61. for enum in file_desc_proto.enum_type:
  62. self._file_desc_protos_by_symbol[
  63. '.'.join((package, enum.name))] = file_desc_proto
  64. for extension in file_desc_proto.extension:
  65. self._file_desc_protos_by_symbol[
  66. '.'.join((package, extension.name))] = file_desc_proto
  67. for service in file_desc_proto.service:
  68. self._file_desc_protos_by_symbol[
  69. '.'.join((package, service.name))] = file_desc_proto
  70. def FindFileByName(self, name):
  71. """Finds the file descriptor proto by file name.
  72. Typically the file name is a relative path ending to a .proto file. The
  73. proto with the given name will have to have been added to this database
  74. using the Add method or else an error will be raised.
  75. Args:
  76. name: The file name to find.
  77. Returns:
  78. The file descriptor proto matching the name.
  79. Raises:
  80. KeyError if no file by the given name was added.
  81. """
  82. return self._file_desc_protos_by_file[name]
  83. def FindFileContainingSymbol(self, symbol):
  84. """Finds the file descriptor proto containing the specified symbol.
  85. The symbol should be a fully qualified name including the file descriptor's
  86. package and any containing messages. Some examples:
  87. 'some.package.name.Message'
  88. 'some.package.name.Message.NestedEnum'
  89. The file descriptor proto containing the specified symbol must be added to
  90. this database using the Add method or else an error will be raised.
  91. Args:
  92. symbol: The fully qualified symbol name.
  93. Returns:
  94. The file descriptor proto containing the symbol.
  95. Raises:
  96. KeyError if no file contains the specified symbol.
  97. """
  98. return self._file_desc_protos_by_symbol[symbol]
  99. def _ExtractSymbols(desc_proto, package):
  100. """Pulls out all the symbols from a descriptor proto.
  101. Args:
  102. desc_proto: The proto to extract symbols from.
  103. package: The package containing the descriptor type.
  104. Yields:
  105. The fully qualified name found in the descriptor.
  106. """
  107. message_name = '.'.join((package, desc_proto.name))
  108. yield message_name
  109. for nested_type in desc_proto.nested_type:
  110. for symbol in _ExtractSymbols(nested_type, message_name):
  111. yield symbol
  112. for enum_type in desc_proto.enum_type:
  113. yield '.'.join((message_name, enum_type.name))