m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.4 KiB

7 months ago
  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. """Contains Unknown Fields APIs.
  31. Simple usage example:
  32. unknown_field_set = UnknownFieldSet(message)
  33. for unknown_field in unknown_field_set:
  34. wire_type = unknown_field.wire_type
  35. field_number = unknown_field.field_number
  36. data = unknown_field.data
  37. """
  38. from google.protobuf.internal import api_implementation
  39. if api_implementation._c_module is not None: # pylint: disable=protected-access
  40. UnknownFieldSet = api_implementation._c_module.UnknownFieldSet # pylint: disable=protected-access
  41. else:
  42. from google.protobuf.internal import decoder # pylint: disable=g-import-not-at-top
  43. from google.protobuf.internal import wire_format # pylint: disable=g-import-not-at-top
  44. class UnknownField:
  45. """A parsed unknown field."""
  46. # Disallows assignment to other attributes.
  47. __slots__ = ['_field_number', '_wire_type', '_data']
  48. def __init__(self, field_number, wire_type, data):
  49. self._field_number = field_number
  50. self._wire_type = wire_type
  51. self._data = data
  52. return
  53. @property
  54. def field_number(self):
  55. return self._field_number
  56. @property
  57. def wire_type(self):
  58. return self._wire_type
  59. @property
  60. def data(self):
  61. return self._data
  62. class UnknownFieldSet:
  63. """UnknownField container."""
  64. # Disallows assignment to other attributes.
  65. __slots__ = ['_values']
  66. def __init__(self, msg):
  67. def InternalAdd(field_number, wire_type, data):
  68. unknown_field = UnknownField(field_number, wire_type, data)
  69. self._values.append(unknown_field)
  70. self._values = []
  71. msg_des = msg.DESCRIPTOR
  72. # pylint: disable=protected-access
  73. unknown_fields = msg._unknown_fields
  74. if (msg_des.has_options and
  75. msg_des.GetOptions().message_set_wire_format):
  76. local_decoder = decoder.UnknownMessageSetItemDecoder()
  77. for _, buffer in unknown_fields:
  78. (field_number, data) = local_decoder(memoryview(buffer))
  79. InternalAdd(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED, data)
  80. else:
  81. for tag_bytes, buffer in unknown_fields:
  82. # pylint: disable=protected-access
  83. (tag, _) = decoder._DecodeVarint(tag_bytes, 0)
  84. field_number, wire_type = wire_format.UnpackTag(tag)
  85. if field_number == 0:
  86. raise RuntimeError('Field number 0 is illegal.')
  87. (data, _) = decoder._DecodeUnknownField(
  88. memoryview(buffer), 0, wire_type)
  89. InternalAdd(field_number, wire_type, data)
  90. def __getitem__(self, index):
  91. size = len(self._values)
  92. if index < 0:
  93. index += size
  94. if index < 0 or index >= size:
  95. raise IndexError('index %d out of range'.index)
  96. return self._values[index]
  97. def __len__(self):
  98. return len(self._values)
  99. def __iter__(self):
  100. return iter(self._values)