图片解析应用
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.

422 lines
14 KiB

  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 these fields and methods.
  54. __slots__ = []
  55. #: The :class:`google.protobuf.Descriptor`
  56. # for this message type.
  57. DESCRIPTOR = None
  58. def __deepcopy__(self, memo=None):
  59. clone = type(self)()
  60. clone.MergeFrom(self)
  61. return clone
  62. def __eq__(self, other_msg):
  63. """Recursively compares two messages by value and structure."""
  64. raise NotImplementedError
  65. def __ne__(self, other_msg):
  66. # Can't just say self != other_msg, since that would infinitely recurse. :)
  67. return not self == other_msg
  68. def __hash__(self):
  69. raise TypeError('unhashable object')
  70. def __str__(self):
  71. """Outputs a human-readable representation of the message."""
  72. raise NotImplementedError
  73. def __unicode__(self):
  74. """Outputs a human-readable representation of the message."""
  75. raise NotImplementedError
  76. def MergeFrom(self, other_msg):
  77. """Merges the contents of the specified message into current message.
  78. This method merges the contents of the specified message into the current
  79. message. Singular fields that are set in the specified message overwrite
  80. the corresponding fields in the current message. Repeated fields are
  81. appended. Singular sub-messages and groups are recursively merged.
  82. Args:
  83. other_msg (Message): A message to merge into the current message.
  84. """
  85. raise NotImplementedError
  86. def CopyFrom(self, other_msg):
  87. """Copies the content of the specified message into the current message.
  88. The method clears the current message and then merges the specified
  89. message using MergeFrom.
  90. Args:
  91. other_msg (Message): A message to copy into the current one.
  92. """
  93. if self is other_msg:
  94. return
  95. self.Clear()
  96. self.MergeFrom(other_msg)
  97. def Clear(self):
  98. """Clears all data that was set in the message."""
  99. raise NotImplementedError
  100. def SetInParent(self):
  101. """Mark this as present in the parent.
  102. This normally happens automatically when you assign a field of a
  103. sub-message, but sometimes you want to make the sub-message
  104. present while keeping it empty. If you find yourself using this,
  105. you may want to reconsider your design.
  106. """
  107. raise NotImplementedError
  108. def IsInitialized(self):
  109. """Checks if the message is initialized.
  110. Returns:
  111. bool: The method returns True if the message is initialized (i.e. all of
  112. its required fields are set).
  113. """
  114. raise NotImplementedError
  115. # TODO(robinson): MergeFromString() should probably return None and be
  116. # implemented in terms of a helper that returns the # of bytes read. Our
  117. # deserialization routines would use the helper when recursively
  118. # deserializing, but the end user would almost always just want the no-return
  119. # MergeFromString().
  120. def MergeFromString(self, serialized):
  121. """Merges serialized protocol buffer data into this message.
  122. When we find a field in `serialized` that is already present
  123. in this message:
  124. - If it's a "repeated" field, we append to the end of our list.
  125. - Else, if it's a scalar, we overwrite our field.
  126. - Else, (it's a nonrepeated composite), we recursively merge
  127. into the existing composite.
  128. Args:
  129. serialized (bytes): Any object that allows us to call
  130. ``memoryview(serialized)`` to access a string of bytes using the
  131. buffer interface.
  132. Returns:
  133. int: The number of bytes read from `serialized`.
  134. For non-group messages, this will always be `len(serialized)`,
  135. but for messages which are actually groups, this will
  136. generally be less than `len(serialized)`, since we must
  137. stop when we reach an ``END_GROUP`` tag. Note that if
  138. we *do* stop because of an ``END_GROUP`` tag, the number
  139. of bytes returned does not include the bytes
  140. for the ``END_GROUP`` tag information.
  141. Raises:
  142. DecodeError: if the input cannot be parsed.
  143. """
  144. # TODO(robinson): Document handling of unknown fields.
  145. # TODO(robinson): When we switch to a helper, this will return None.
  146. raise NotImplementedError
  147. def ParseFromString(self, serialized):
  148. """Parse serialized protocol buffer data in binary form into this message.
  149. Like :func:`MergeFromString()`, except we clear the object first.
  150. Raises:
  151. message.DecodeError if the input cannot be parsed.
  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, field_descriptor):
  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. field_descriptor: The field descriptor 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, field_descriptor):
  254. """Clears the contents of a given extension.
  255. Args:
  256. field_descriptor: The field descriptor 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. @classmethod
  278. def FromString(cls, s):
  279. raise NotImplementedError
  280. # TODO(b/286557203): Remove it in OSS
  281. @staticmethod
  282. def RegisterExtension(field_descriptor):
  283. raise NotImplementedError
  284. def _SetListener(self, message_listener):
  285. """Internal method used by the protocol message implementation.
  286. Clients should not call this directly.
  287. Sets a listener that this message will call on certain state transitions.
  288. The purpose of this method is to register back-edges from children to
  289. parents at runtime, for the purpose of setting "has" bits and
  290. byte-size-dirty bits in the parent and ancestor objects whenever a child or
  291. descendant object is modified.
  292. If the client wants to disconnect this Message from the object tree, she
  293. explicitly sets callback to None.
  294. If message_listener is None, unregisters any existing listener. Otherwise,
  295. message_listener must implement the MessageListener interface in
  296. internal/message_listener.py, and we discard any listener registered
  297. via a previous _SetListener() call.
  298. """
  299. raise NotImplementedError
  300. def __getstate__(self):
  301. """Support the pickle protocol."""
  302. return dict(serialized=self.SerializePartialToString())
  303. def __setstate__(self, state):
  304. """Support the pickle protocol."""
  305. self.__init__()
  306. serialized = state['serialized']
  307. # On Python 3, using encoding='latin1' is required for unpickling
  308. # protos pickled by Python 2.
  309. if not isinstance(serialized, bytes):
  310. serialized = serialized.encode('latin1')
  311. self.ParseFromString(serialized)
  312. def __reduce__(self):
  313. message_descriptor = self.DESCRIPTOR
  314. if message_descriptor.containing_type is None:
  315. return type(self), (), self.__getstate__()
  316. # the message type must be nested.
  317. # Python does not pickle nested classes; use the symbol_database on the
  318. # receiving end.
  319. container = message_descriptor
  320. return (_InternalConstructMessage, (container.full_name,),
  321. self.__getstate__())
  322. def _InternalConstructMessage(full_name):
  323. """Constructs a nested message."""
  324. from google.protobuf import symbol_database # pylint:disable=g-import-not-at-top
  325. return symbol_database.Default().GetSymbol(full_name)()