message.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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): pass
  35. class DecodeError(Error): pass
  36. class EncodeError(Error): pass
  37. class Message(object):
  38. """Abstract base class for protocol messages.
  39. Protocol message classes are almost always generated by the protocol
  40. compiler. These generated types subclass Message and implement the methods
  41. shown below.
  42. TODO(robinson): Link to an HTML document here.
  43. TODO(robinson): Document that instances of this class will also
  44. have an Extensions attribute with __getitem__ and __setitem__.
  45. Again, not sure how to best convey this.
  46. TODO(robinson): Document that the class must also have a static
  47. RegisterExtension(extension_field) method.
  48. Not sure how to best express at this point.
  49. """
  50. # TODO(robinson): Document these fields and methods.
  51. __slots__ = []
  52. DESCRIPTOR = None
  53. def __deepcopy__(self, memo=None):
  54. clone = type(self)()
  55. clone.MergeFrom(self)
  56. return clone
  57. def __eq__(self, other_msg):
  58. """Recursively compares two messages by value and structure."""
  59. raise NotImplementedError
  60. def __ne__(self, other_msg):
  61. # Can't just say self != other_msg, since that would infinitely recurse. :)
  62. return not self == other_msg
  63. def __hash__(self):
  64. raise TypeError('unhashable object')
  65. def __str__(self):
  66. """Outputs a human-readable representation of the message."""
  67. raise NotImplementedError
  68. def __unicode__(self):
  69. """Outputs a human-readable representation of the message."""
  70. raise NotImplementedError
  71. def MergeFrom(self, other_msg):
  72. """Merges the contents of the specified message into current message.
  73. This method merges the contents of the specified message into the current
  74. message. Singular fields that are set in the specified message overwrite
  75. the corresponding fields in the current message. Repeated fields are
  76. appended. Singular sub-messages and groups are recursively merged.
  77. Args:
  78. other_msg: Message to merge into the current message.
  79. """
  80. raise NotImplementedError
  81. def CopyFrom(self, other_msg):
  82. """Copies the content of the specified message into the current message.
  83. The method clears the current message and then merges the specified
  84. message using MergeFrom.
  85. Args:
  86. other_msg: Message to copy into the current one.
  87. """
  88. if self is other_msg:
  89. return
  90. self.Clear()
  91. self.MergeFrom(other_msg)
  92. def Clear(self):
  93. """Clears all data that was set in the message."""
  94. raise NotImplementedError
  95. def SetInParent(self):
  96. """Mark this as present in the parent.
  97. This normally happens automatically when you assign a field of a
  98. sub-message, but sometimes you want to make the sub-message
  99. present while keeping it empty. If you find yourself using this,
  100. you may want to reconsider your design."""
  101. raise NotImplementedError
  102. def IsInitialized(self):
  103. """Checks if the message is initialized.
  104. Returns:
  105. The method returns True if the message is initialized (i.e. all of its
  106. required fields are set).
  107. """
  108. raise NotImplementedError
  109. # TODO(robinson): MergeFromString() should probably return None and be
  110. # implemented in terms of a helper that returns the # of bytes read. Our
  111. # deserialization routines would use the helper when recursively
  112. # deserializing, but the end user would almost always just want the no-return
  113. # MergeFromString().
  114. def MergeFromString(self, serialized):
  115. """Merges serialized protocol buffer data into this message.
  116. When we find a field in |serialized| that is already present
  117. in this message:
  118. - If it's a "repeated" field, we append to the end of our list.
  119. - Else, if it's a scalar, we overwrite our field.
  120. - Else, (it's a nonrepeated composite), we recursively merge
  121. into the existing composite.
  122. TODO(robinson): Document handling of unknown fields.
  123. Args:
  124. serialized: Any object that allows us to call buffer(serialized)
  125. to access a string of bytes using the buffer interface.
  126. TODO(robinson): When we switch to a helper, this will return None.
  127. Returns:
  128. The number of bytes read from |serialized|.
  129. For non-group messages, this will always be len(serialized),
  130. but for messages which are actually groups, this will
  131. generally be less than len(serialized), since we must
  132. stop when we reach an END_GROUP tag. Note that if
  133. we *do* stop because of an END_GROUP tag, the number
  134. of bytes returned does not include the bytes
  135. for the END_GROUP tag information.
  136. """
  137. raise NotImplementedError
  138. def ParseFromString(self, serialized):
  139. """Parse serialized protocol buffer data into this message.
  140. Like MergeFromString(), except we clear the object first and
  141. do not return the value that MergeFromString returns.
  142. """
  143. self.Clear()
  144. self.MergeFromString(serialized)
  145. def SerializeToString(self):
  146. """Serializes the protocol message to a binary string.
  147. Returns:
  148. A binary string representation of the message if all of the required
  149. fields in the message are set (i.e. the message is initialized).
  150. Raises:
  151. message.EncodeError if the message isn't initialized.
  152. """
  153. raise NotImplementedError
  154. def SerializePartialToString(self):
  155. """Serializes the protocol message to a binary string.
  156. This method is similar to SerializeToString but doesn't check if the
  157. message is initialized.
  158. Returns:
  159. A string representation of the partial message.
  160. """
  161. raise NotImplementedError
  162. # TODO(robinson): Decide whether we like these better
  163. # than auto-generated has_foo() and clear_foo() methods
  164. # on the instances themselves. This way is less consistent
  165. # with C++, but it makes reflection-type access easier and
  166. # reduces the number of magically autogenerated things.
  167. #
  168. # TODO(robinson): Be sure to document (and test) exactly
  169. # which field names are accepted here. Are we case-sensitive?
  170. # What do we do with fields that share names with Python keywords
  171. # like 'lambda' and 'yield'?
  172. #
  173. # nnorwitz says:
  174. # """
  175. # Typically (in python), an underscore is appended to names that are
  176. # keywords. So they would become lambda_ or yield_.
  177. # """
  178. def ListFields(self):
  179. """Returns a list of (FieldDescriptor, value) tuples for all
  180. fields in the message which are not empty. A message field is
  181. non-empty if HasField() would return true. A singular primitive field
  182. is non-empty if HasField() would return true in proto2 or it is non zero
  183. in proto3. A repeated field is non-empty if it contains at least one
  184. element. The fields are ordered by field number"""
  185. raise NotImplementedError
  186. def HasField(self, field_name):
  187. """Checks if a certain field is set for the message, or if any field inside
  188. a oneof group is set. Note that if the field_name is not defined in the
  189. message descriptor, ValueError will be raised."""
  190. raise NotImplementedError
  191. def ClearField(self, field_name):
  192. """Clears the contents of a given field, or the field set inside a oneof
  193. group. If the name neither refers to a defined field or oneof group,
  194. ValueError is raised."""
  195. raise NotImplementedError
  196. def WhichOneof(self, oneof_group):
  197. """Returns the name of the field that is set inside a oneof group, or
  198. None if no field is set. If no group with the given name exists, ValueError
  199. will be raised."""
  200. raise NotImplementedError
  201. def HasExtension(self, extension_handle):
  202. raise NotImplementedError
  203. def ClearExtension(self, extension_handle):
  204. raise NotImplementedError
  205. def DiscardUnknownFields(self):
  206. raise NotImplementedError
  207. def ByteSize(self):
  208. """Returns the serialized size of this message.
  209. Recursively calls ByteSize() on all contained messages.
  210. """
  211. raise NotImplementedError
  212. def _SetListener(self, message_listener):
  213. """Internal method used by the protocol message implementation.
  214. Clients should not call this directly.
  215. Sets a listener that this message will call on certain state transitions.
  216. The purpose of this method is to register back-edges from children to
  217. parents at runtime, for the purpose of setting "has" bits and
  218. byte-size-dirty bits in the parent and ancestor objects whenever a child or
  219. descendant object is modified.
  220. If the client wants to disconnect this Message from the object tree, she
  221. explicitly sets callback to None.
  222. If message_listener is None, unregisters any existing listener. Otherwise,
  223. message_listener must implement the MessageListener interface in
  224. internal/message_listener.py, and we discard any listener registered
  225. via a previous _SetListener() call.
  226. """
  227. raise NotImplementedError
  228. def __getstate__(self):
  229. """Support the pickle protocol."""
  230. return dict(serialized=self.SerializePartialToString())
  231. def __setstate__(self, state):
  232. """Support the pickle protocol."""
  233. self.__init__()
  234. self.ParseFromString(state['serialized'])