店播爬取Python脚本

message.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. # TODO(robinson): We should just make these methods all "pure-virtual" and move
  31. # all implementation out, into reflection.py for now.
  32. """Contains an abstract base class for protocol messages."""
  33. __author__ = 'robinson@google.com (Will Robinson)'
  34. class Error(Exception):
  35. """Base error type for this module."""
  36. pass
  37. class DecodeError(Error):
  38. """Exception raised when deserializing messages."""
  39. pass
  40. class EncodeError(Error):
  41. """Exception raised when serializing messages."""
  42. pass
  43. class Message(object):
  44. """Abstract base class for protocol messages.
  45. Protocol message classes are almost always generated by the protocol
  46. compiler. These generated types subclass Message and implement the methods
  47. shown below.
  48. """
  49. # TODO(robinson): Link to an HTML document here.
  50. # TODO(robinson): Document that instances of this class will also
  51. # have an Extensions attribute with __getitem__ and __setitem__.
  52. # Again, not sure how to best convey this.
  53. # TODO(robinson): Document that the class must also have a static
  54. # RegisterExtension(extension_field) method.
  55. # Not sure how to best express at this point.
  56. # TODO(robinson): Document these fields and methods.
  57. __slots__ = []
  58. #: The :class:`google.protobuf.descriptor.Descriptor` for this message type.
  59. DESCRIPTOR = None
  60. def __deepcopy__(self, memo=None):
  61. clone = type(self)()
  62. clone.MergeFrom(self)
  63. return clone
  64. def __eq__(self, other_msg):
  65. """Recursively compares two messages by value and structure."""
  66. raise NotImplementedError
  67. def __ne__(self, other_msg):
  68. # Can't just say self != other_msg, since that would infinitely recurse. :)
  69. return not self == other_msg
  70. def __hash__(self):
  71. raise TypeError('unhashable object')
  72. def __str__(self):
  73. """Outputs a human-readable representation of the message."""
  74. raise NotImplementedError
  75. def __unicode__(self):
  76. """Outputs a human-readable representation of the message."""
  77. raise NotImplementedError
  78. def MergeFrom(self, other_msg):
  79. """Merges the contents of the specified message into current message.
  80. This method merges the contents of the specified message into the current
  81. message. Singular fields that are set in the specified message overwrite
  82. the corresponding fields in the current message. Repeated fields are
  83. appended. Singular sub-messages and groups are recursively merged.
  84. Args:
  85. other_msg (Message): A message to merge into the current message.
  86. """
  87. raise NotImplementedError
  88. def CopyFrom(self, other_msg):
  89. """Copies the content of the specified message into the current message.
  90. The method clears the current message and then merges the specified
  91. message using MergeFrom.
  92. Args:
  93. other_msg (Message): A message to copy into the current one.
  94. """
  95. if self is other_msg:
  96. return
  97. self.Clear()
  98. self.MergeFrom(other_msg)
  99. def Clear(self):
  100. """Clears all data that was set in the message."""
  101. raise NotImplementedError
  102. def SetInParent(self):
  103. """Mark this as present in the parent.
  104. This normally happens automatically when you assign a field of a
  105. sub-message, but sometimes you want to make the sub-message
  106. present while keeping it empty. If you find yourself using this,
  107. you may want to reconsider your design.
  108. """
  109. raise NotImplementedError
  110. def IsInitialized(self):
  111. """Checks if the message is initialized.
  112. Returns:
  113. bool: The method returns True if the message is initialized (i.e. all of
  114. its required fields are set).
  115. """
  116. raise NotImplementedError
  117. # TODO(robinson): MergeFromString() should probably return None and be
  118. # implemented in terms of a helper that returns the # of bytes read. Our
  119. # deserialization routines would use the helper when recursively
  120. # deserializing, but the end user would almost always just want the no-return
  121. # MergeFromString().
  122. def MergeFromString(self, serialized):
  123. """Merges serialized protocol buffer data into this message.
  124. When we find a field in `serialized` that is already present
  125. in this message:
  126. - If it's a "repeated" field, we append to the end of our list.
  127. - Else, if it's a scalar, we overwrite our field.
  128. - Else, (it's a nonrepeated composite), we recursively merge
  129. into the existing composite.
  130. Args:
  131. serialized (bytes): Any object that allows us to call
  132. ``memoryview(serialized)`` to access a string of bytes using the
  133. buffer interface.
  134. Returns:
  135. int: The number of bytes read from `serialized`.
  136. For non-group messages, this will always be `len(serialized)`,
  137. but for messages which are actually groups, this will
  138. generally be less than `len(serialized)`, since we must
  139. stop when we reach an ``END_GROUP`` tag. Note that if
  140. we *do* stop because of an ``END_GROUP`` tag, the number
  141. of bytes returned does not include the bytes
  142. for the ``END_GROUP`` tag information.
  143. Raises:
  144. DecodeError: if the input cannot be parsed.
  145. """
  146. # TODO(robinson): Document handling of unknown fields.
  147. # TODO(robinson): When we switch to a helper, this will return None.
  148. raise NotImplementedError
  149. def ParseFromString(self, serialized):
  150. """Parse serialized protocol buffer data into this message.
  151. Like :func:`MergeFromString()`, except we clear the object first.
  152. """
  153. self.Clear()
  154. return self.MergeFromString(serialized)
  155. def SerializeToString(self, **kwargs):
  156. """Serializes the protocol message to a binary string.
  157. Keyword Args:
  158. deterministic (bool): If true, requests deterministic serialization
  159. of the protobuf, with predictable ordering of map keys.
  160. Returns:
  161. A binary string representation of the message if all of the required
  162. fields in the message are set (i.e. the message is initialized).
  163. Raises:
  164. EncodeError: if the message isn't initialized (see :func:`IsInitialized`).
  165. """
  166. raise NotImplementedError
  167. def SerializePartialToString(self, **kwargs):
  168. """Serializes the protocol message to a binary string.
  169. This method is similar to SerializeToString but doesn't check if the
  170. message is initialized.
  171. Keyword Args:
  172. deterministic (bool): If true, requests deterministic serialization
  173. of the protobuf, with predictable ordering of map keys.
  174. Returns:
  175. bytes: A serialized representation of the partial message.
  176. """
  177. raise NotImplementedError
  178. # TODO(robinson): Decide whether we like these better
  179. # than auto-generated has_foo() and clear_foo() methods
  180. # on the instances themselves. This way is less consistent
  181. # with C++, but it makes reflection-type access easier and
  182. # reduces the number of magically autogenerated things.
  183. #
  184. # TODO(robinson): Be sure to document (and test) exactly
  185. # which field names are accepted here. Are we case-sensitive?
  186. # What do we do with fields that share names with Python keywords
  187. # like 'lambda' and 'yield'?
  188. #
  189. # nnorwitz says:
  190. # """
  191. # Typically (in python), an underscore is appended to names that are
  192. # keywords. So they would become lambda_ or yield_.
  193. # """
  194. def ListFields(self):
  195. """Returns a list of (FieldDescriptor, value) tuples for present fields.
  196. A message field is non-empty if HasField() would return true. A singular
  197. primitive field is non-empty if HasField() would return true in proto2 or it
  198. is non zero in proto3. A repeated field is non-empty if it contains at least
  199. one element. The fields are ordered by field number.
  200. Returns:
  201. list[tuple(FieldDescriptor, value)]: field descriptors and values
  202. for all fields in the message which are not empty. The values vary by
  203. field type.
  204. """
  205. raise NotImplementedError
  206. def HasField(self, field_name):
  207. """Checks if a certain field is set for the message.
  208. For a oneof group, checks if any field inside is set. Note that if the
  209. field_name is not defined in the message descriptor, :exc:`ValueError` will
  210. be raised.
  211. Args:
  212. field_name (str): The name of the field to check for presence.
  213. Returns:
  214. bool: Whether a value has been set for the named field.
  215. Raises:
  216. ValueError: if the `field_name` is not a member of this message.
  217. """
  218. raise NotImplementedError
  219. def ClearField(self, field_name):
  220. """Clears the contents of a given field.
  221. Inside a oneof group, clears the field set. If the name neither refers to a
  222. defined field or oneof group, :exc:`ValueError` is raised.
  223. Args:
  224. field_name (str): The name of the field to check for presence.
  225. Raises:
  226. ValueError: if the `field_name` is not a member of this message.
  227. """
  228. raise NotImplementedError
  229. def WhichOneof(self, oneof_group):
  230. """Returns the name of the field that is set inside a oneof group.
  231. If no field is set, returns None.
  232. Args:
  233. oneof_group (str): the name of the oneof group to check.
  234. Returns:
  235. str or None: The name of the group that is set, or None.
  236. Raises:
  237. ValueError: no group with the given name exists
  238. """
  239. raise NotImplementedError
  240. def HasExtension(self, extension_handle):
  241. """Checks if a certain extension is present for this message.
  242. Extensions are retrieved using the :attr:`Extensions` mapping (if present).
  243. Args:
  244. extension_handle: The handle for the extension to check.
  245. Returns:
  246. bool: Whether the extension is present for this message.
  247. Raises:
  248. KeyError: if the extension is repeated. Similar to repeated fields,
  249. there is no separate notion of presence: a "not present" repeated
  250. extension is an empty list.
  251. """
  252. raise NotImplementedError
  253. def ClearExtension(self, extension_handle):
  254. """Clears the contents of a given extension.
  255. Args:
  256. extension_handle: The handle for the extension to clear.
  257. """
  258. raise NotImplementedError
  259. def UnknownFields(self):
  260. """Returns the UnknownFieldSet.
  261. Returns:
  262. UnknownFieldSet: The unknown fields stored in this message.
  263. """
  264. raise NotImplementedError
  265. def DiscardUnknownFields(self):
  266. """Clears all fields in the :class:`UnknownFieldSet`.
  267. This operation is recursive for nested message.
  268. """
  269. raise NotImplementedError
  270. def ByteSize(self):
  271. """Returns the serialized size of this message.
  272. Recursively calls ByteSize() on all contained messages.
  273. Returns:
  274. int: The number of bytes required to serialize this message.
  275. """
  276. raise NotImplementedError
  277. def _SetListener(self, message_listener):
  278. """Internal method used by the protocol message implementation.
  279. Clients should not call this directly.
  280. Sets a listener that this message will call on certain state transitions.
  281. The purpose of this method is to register back-edges from children to
  282. parents at runtime, for the purpose of setting "has" bits and
  283. byte-size-dirty bits in the parent and ancestor objects whenever a child or
  284. descendant object is modified.
  285. If the client wants to disconnect this Message from the object tree, she
  286. explicitly sets callback to None.
  287. If message_listener is None, unregisters any existing listener. Otherwise,
  288. message_listener must implement the MessageListener interface in
  289. internal/message_listener.py, and we discard any listener registered
  290. via a previous _SetListener() call.
  291. """
  292. raise NotImplementedError
  293. def __getstate__(self):
  294. """Support the pickle protocol."""
  295. return dict(serialized=self.SerializePartialToString())
  296. def __setstate__(self, state):
  297. """Support the pickle protocol."""
  298. self.__init__()
  299. serialized = state['serialized']
  300. # On Python 3, using encoding='latin1' is required for unpickling
  301. # protos pickled by Python 2.
  302. if not isinstance(serialized, bytes):
  303. serialized = serialized.encode('latin1')
  304. self.ParseFromString(serialized)
  305. def __reduce__(self):
  306. message_descriptor = self.DESCRIPTOR
  307. if message_descriptor.containing_type is None:
  308. return type(self), (), self.__getstate__()
  309. # the message type must be nested.
  310. # Python does not pickle nested classes; use the symbol_database on the
  311. # receiving end.
  312. container = message_descriptor
  313. return (_InternalConstructMessage, (container.full_name,),
  314. self.__getstate__())
  315. def _InternalConstructMessage(full_name):
  316. """Constructs a nested message."""
  317. from google.protobuf import symbol_database # pylint:disable=g-import-not-at-top
  318. return symbol_database.Default().GetSymbol(full_name)()