店播爬取Python脚本

extension_dict.py 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 _ExtensionDict class to represent extensions.
  31. """
  32. from google.protobuf.internal import type_checkers
  33. from google.protobuf.descriptor import FieldDescriptor
  34. def _VerifyExtensionHandle(message, extension_handle):
  35. """Verify that the given extension handle is valid."""
  36. if not isinstance(extension_handle, FieldDescriptor):
  37. raise KeyError('HasExtension() expects an extension handle, got: %s' %
  38. extension_handle)
  39. if not extension_handle.is_extension:
  40. raise KeyError('"%s" is not an extension.' % extension_handle.full_name)
  41. if not extension_handle.containing_type:
  42. raise KeyError('"%s" is missing a containing_type.'
  43. % extension_handle.full_name)
  44. if extension_handle.containing_type is not message.DESCRIPTOR:
  45. raise KeyError('Extension "%s" extends message type "%s", but this '
  46. 'message is of type "%s".' %
  47. (extension_handle.full_name,
  48. extension_handle.containing_type.full_name,
  49. message.DESCRIPTOR.full_name))
  50. # TODO(robinson): Unify error handling of "unknown extension" crap.
  51. # TODO(robinson): Support iteritems()-style iteration over all
  52. # extensions with the "has" bits turned on?
  53. class _ExtensionDict(object):
  54. """Dict-like container for Extension fields on proto instances.
  55. Note that in all cases we expect extension handles to be
  56. FieldDescriptors.
  57. """
  58. def __init__(self, extended_message):
  59. """
  60. Args:
  61. extended_message: Message instance for which we are the Extensions dict.
  62. """
  63. self._extended_message = extended_message
  64. def __getitem__(self, extension_handle):
  65. """Returns the current value of the given extension handle."""
  66. _VerifyExtensionHandle(self._extended_message, extension_handle)
  67. result = self._extended_message._fields.get(extension_handle)
  68. if result is not None:
  69. return result
  70. if extension_handle.label == FieldDescriptor.LABEL_REPEATED:
  71. result = extension_handle._default_constructor(self._extended_message)
  72. elif extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE:
  73. message_type = extension_handle.message_type
  74. if not hasattr(message_type, '_concrete_class'):
  75. # pylint: disable=protected-access
  76. self._extended_message._FACTORY.GetPrototype(message_type)
  77. assert getattr(extension_handle.message_type, '_concrete_class', None), (
  78. 'Uninitialized concrete class found for field %r (message type %r)'
  79. % (extension_handle.full_name,
  80. extension_handle.message_type.full_name))
  81. result = extension_handle.message_type._concrete_class()
  82. try:
  83. result._SetListener(self._extended_message._listener_for_children)
  84. except ReferenceError:
  85. pass
  86. else:
  87. # Singular scalar -- just return the default without inserting into the
  88. # dict.
  89. return extension_handle.default_value
  90. # Atomically check if another thread has preempted us and, if not, swap
  91. # in the new object we just created. If someone has preempted us, we
  92. # take that object and discard ours.
  93. # WARNING: We are relying on setdefault() being atomic. This is true
  94. # in CPython but we haven't investigated others. This warning appears
  95. # in several other locations in this file.
  96. result = self._extended_message._fields.setdefault(
  97. extension_handle, result)
  98. return result
  99. def __eq__(self, other):
  100. if not isinstance(other, self.__class__):
  101. return False
  102. my_fields = self._extended_message.ListFields()
  103. other_fields = other._extended_message.ListFields()
  104. # Get rid of non-extension fields.
  105. my_fields = [field for field in my_fields if field.is_extension]
  106. other_fields = [field for field in other_fields if field.is_extension]
  107. return my_fields == other_fields
  108. def __ne__(self, other):
  109. return not self == other
  110. def __len__(self):
  111. fields = self._extended_message.ListFields()
  112. # Get rid of non-extension fields.
  113. extension_fields = [field for field in fields if field[0].is_extension]
  114. return len(extension_fields)
  115. def __hash__(self):
  116. raise TypeError('unhashable object')
  117. # Note that this is only meaningful for non-repeated, scalar extension
  118. # fields. Note also that we may have to call _Modified() when we do
  119. # successfully set a field this way, to set any necessary "has" bits in the
  120. # ancestors of the extended message.
  121. def __setitem__(self, extension_handle, value):
  122. """If extension_handle specifies a non-repeated, scalar extension
  123. field, sets the value of that field.
  124. """
  125. _VerifyExtensionHandle(self._extended_message, extension_handle)
  126. if (extension_handle.label == FieldDescriptor.LABEL_REPEATED or
  127. extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE):
  128. raise TypeError(
  129. 'Cannot assign to extension "%s" because it is a repeated or '
  130. 'composite type.' % extension_handle.full_name)
  131. # It's slightly wasteful to lookup the type checker each time,
  132. # but we expect this to be a vanishingly uncommon case anyway.
  133. type_checker = type_checkers.GetTypeChecker(extension_handle)
  134. # pylint: disable=protected-access
  135. self._extended_message._fields[extension_handle] = (
  136. type_checker.CheckValue(value))
  137. self._extended_message._Modified()
  138. def __delitem__(self, extension_handle):
  139. self._extended_message.ClearExtension(extension_handle)
  140. def _FindExtensionByName(self, name):
  141. """Tries to find a known extension with the specified name.
  142. Args:
  143. name: Extension full name.
  144. Returns:
  145. Extension field descriptor.
  146. """
  147. return self._extended_message._extensions_by_name.get(name, None)
  148. def _FindExtensionByNumber(self, number):
  149. """Tries to find a known extension with the field number.
  150. Args:
  151. number: Extension field number.
  152. Returns:
  153. Extension field descriptor.
  154. """
  155. return self._extended_message._extensions_by_number.get(number, None)
  156. def __iter__(self):
  157. # Return a generator over the populated extension fields
  158. return (f[0] for f in self._extended_message.ListFields()
  159. if f[0].is_extension)
  160. def __contains__(self, extension_handle):
  161. _VerifyExtensionHandle(self._extended_message, extension_handle)
  162. if extension_handle not in self._extended_message._fields:
  163. return False
  164. if extension_handle.label == FieldDescriptor.LABEL_REPEATED:
  165. return bool(self._extended_message._fields.get(extension_handle))
  166. if extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE:
  167. value = self._extended_message._fields.get(extension_handle)
  168. # pylint: disable=protected-access
  169. return value is not None and value._is_present_in_parent
  170. return True