Counter Strike : Global Offensive Source Code
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.

269 lines
9.8 KiB

  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. # http://code.google.com/p/protobuf/
  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 container classes to represent different protocol buffer types.
  31. This file defines container classes which represent categories of protocol
  32. buffer field types which need extra maintenance. Currently these categories
  33. are:
  34. - Repeated scalar fields - These are all repeated fields which aren't
  35. composite (e.g. they are of simple types like int32, string, etc).
  36. - Repeated composite fields - Repeated fields which are composite. This
  37. includes groups and nested messages.
  38. """
  39. __author__ = '[email protected] (Petar Petrov)'
  40. class BaseContainer(object):
  41. """Base container class."""
  42. # Minimizes memory usage and disallows assignment to other attributes.
  43. __slots__ = ['_message_listener', '_values']
  44. def __init__(self, message_listener):
  45. """
  46. Args:
  47. message_listener: A MessageListener implementation.
  48. The RepeatedScalarFieldContainer will call this object's
  49. Modified() method when it is modified.
  50. """
  51. self._message_listener = message_listener
  52. self._values = []
  53. def __getitem__(self, key):
  54. """Retrieves item by the specified key."""
  55. return self._values[key]
  56. def __len__(self):
  57. """Returns the number of elements in the container."""
  58. return len(self._values)
  59. def __ne__(self, other):
  60. """Checks if another instance isn't equal to this one."""
  61. # The concrete classes should define __eq__.
  62. return not self == other
  63. def __hash__(self):
  64. raise TypeError('unhashable object')
  65. def __repr__(self):
  66. return repr(self._values)
  67. def sort(self, *args, **kwargs):
  68. # Continue to support the old sort_function keyword argument.
  69. # This is expected to be a rare occurrence, so use LBYL to avoid
  70. # the overhead of actually catching KeyError.
  71. if 'sort_function' in kwargs:
  72. kwargs['cmp'] = kwargs.pop('sort_function')
  73. self._values.sort(*args, **kwargs)
  74. class RepeatedScalarFieldContainer(BaseContainer):
  75. """Simple, type-checked, list-like container for holding repeated scalars."""
  76. # Disallows assignment to other attributes.
  77. __slots__ = ['_type_checker']
  78. def __init__(self, message_listener, type_checker):
  79. """
  80. Args:
  81. message_listener: A MessageListener implementation.
  82. The RepeatedScalarFieldContainer will call this object's
  83. Modified() method when it is modified.
  84. type_checker: A type_checkers.ValueChecker instance to run on elements
  85. inserted into this container.
  86. """
  87. super(RepeatedScalarFieldContainer, self).__init__(message_listener)
  88. self._type_checker = type_checker
  89. def append(self, value):
  90. """Appends an item to the list. Similar to list.append()."""
  91. self._type_checker.CheckValue(value)
  92. self._values.append(value)
  93. if not self._message_listener.dirty:
  94. self._message_listener.Modified()
  95. def insert(self, key, value):
  96. """Inserts the item at the specified position. Similar to list.insert()."""
  97. self._type_checker.CheckValue(value)
  98. self._values.insert(key, value)
  99. if not self._message_listener.dirty:
  100. self._message_listener.Modified()
  101. def extend(self, elem_seq):
  102. """Extends by appending the given sequence. Similar to list.extend()."""
  103. if not elem_seq:
  104. return
  105. new_values = []
  106. for elem in elem_seq:
  107. self._type_checker.CheckValue(elem)
  108. new_values.append(elem)
  109. self._values.extend(new_values)
  110. self._message_listener.Modified()
  111. def MergeFrom(self, other):
  112. """Appends the contents of another repeated field of the same type to this
  113. one. We do not check the types of the individual fields.
  114. """
  115. self._values.extend(other._values)
  116. self._message_listener.Modified()
  117. def remove(self, elem):
  118. """Removes an item from the list. Similar to list.remove()."""
  119. self._values.remove(elem)
  120. self._message_listener.Modified()
  121. def __setitem__(self, key, value):
  122. """Sets the item on the specified position."""
  123. self._type_checker.CheckValue(value)
  124. self._values[key] = value
  125. self._message_listener.Modified()
  126. def __getslice__(self, start, stop):
  127. """Retrieves the subset of items from between the specified indices."""
  128. return self._values[start:stop]
  129. def __setslice__(self, start, stop, values):
  130. """Sets the subset of items from between the specified indices."""
  131. new_values = []
  132. for value in values:
  133. self._type_checker.CheckValue(value)
  134. new_values.append(value)
  135. self._values[start:stop] = new_values
  136. self._message_listener.Modified()
  137. def __delitem__(self, key):
  138. """Deletes the item at the specified position."""
  139. del self._values[key]
  140. self._message_listener.Modified()
  141. def __delslice__(self, start, stop):
  142. """Deletes the subset of items from between the specified indices."""
  143. del self._values[start:stop]
  144. self._message_listener.Modified()
  145. def __eq__(self, other):
  146. """Compares the current instance with another one."""
  147. if self is other:
  148. return True
  149. # Special case for the same type which should be common and fast.
  150. if isinstance(other, self.__class__):
  151. return other._values == self._values
  152. # We are presumably comparing against some other sequence type.
  153. return other == self._values
  154. class RepeatedCompositeFieldContainer(BaseContainer):
  155. """Simple, list-like container for holding repeated composite fields."""
  156. # Disallows assignment to other attributes.
  157. __slots__ = ['_message_descriptor']
  158. def __init__(self, message_listener, message_descriptor):
  159. """
  160. Note that we pass in a descriptor instead of the generated directly,
  161. since at the time we construct a _RepeatedCompositeFieldContainer we
  162. haven't yet necessarily initialized the type that will be contained in the
  163. container.
  164. Args:
  165. message_listener: A MessageListener implementation.
  166. The RepeatedCompositeFieldContainer will call this object's
  167. Modified() method when it is modified.
  168. message_descriptor: A Descriptor instance describing the protocol type
  169. that should be present in this container. We'll use the
  170. _concrete_class field of this descriptor when the client calls add().
  171. """
  172. super(RepeatedCompositeFieldContainer, self).__init__(message_listener)
  173. self._message_descriptor = message_descriptor
  174. def add(self, **kwargs):
  175. """Adds a new element at the end of the list and returns it. Keyword
  176. arguments may be used to initialize the element.
  177. """
  178. new_element = self._message_descriptor._concrete_class(**kwargs)
  179. new_element._SetListener(self._message_listener)
  180. self._values.append(new_element)
  181. if not self._message_listener.dirty:
  182. self._message_listener.Modified()
  183. return new_element
  184. def extend(self, elem_seq):
  185. """Extends by appending the given sequence of elements of the same type
  186. as this one, copying each individual message.
  187. """
  188. message_class = self._message_descriptor._concrete_class
  189. listener = self._message_listener
  190. values = self._values
  191. for message in elem_seq:
  192. new_element = message_class()
  193. new_element._SetListener(listener)
  194. new_element.MergeFrom(message)
  195. values.append(new_element)
  196. listener.Modified()
  197. def MergeFrom(self, other):
  198. """Appends the contents of another repeated field of the same type to this
  199. one, copying each individual message.
  200. """
  201. self.extend(other._values)
  202. def remove(self, elem):
  203. """Removes an item from the list. Similar to list.remove()."""
  204. self._values.remove(elem)
  205. self._message_listener.Modified()
  206. def __getslice__(self, start, stop):
  207. """Retrieves the subset of items from between the specified indices."""
  208. return self._values[start:stop]
  209. def __delitem__(self, key):
  210. """Deletes the item at the specified position."""
  211. del self._values[key]
  212. self._message_listener.Modified()
  213. def __delslice__(self, start, stop):
  214. """Deletes the subset of items from between the specified indices."""
  215. del self._values[start:stop]
  216. self._message_listener.Modified()
  217. def __eq__(self, other):
  218. """Compares the current instance with another one."""
  219. if self is other:
  220. return True
  221. if not isinstance(other, self.__class__):
  222. raise TypeError('Can only compare repeated composite fields against '
  223. 'other repeated composite fields.')
  224. return self._values == other._values