PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/python/lib/PyAMF-0.7.2/pyamf/flex/__init__.py

https://gitlab.com/gregtyka/frankenserver
Python | 267 lines | 128 code | 35 blank | 104 comment | 18 complexity | 23ad6a69c63f3f3c4c761bbef64d505c MD5 | raw file
  1. # Copyright (c) The PyAMF Project.
  2. # See LICENSE.txt for details.
  3. """
  4. Compatibility classes/functions for Adobe Flex.
  5. @note: Not available in ActionScript 1.0 and 2.0.
  6. @see: U{Flex on Wikipedia<http://en.wikipedia.org/wiki/Adobe_Flex>}
  7. @since: 0.1
  8. """
  9. import pyamf
  10. __all__ = ['ArrayCollection', 'ObjectProxy']
  11. class ArrayCollection(list):
  12. """
  13. I represent the ActionScript 3 based class
  14. C{flex.messaging.io.ArrayCollection} used in the Flex framework.
  15. The C{ArrayCollection} class is a wrapper class that exposes an Array
  16. as a collection that can be accessed and manipulated using the
  17. methods and properties of the C{ICollectionView} or C{IList}
  18. interfaces in the Flex framework.
  19. @see: U{ArrayCollection on Livedocs <http://
  20. help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/
  21. collections/ArrayCollection.html>}
  22. @note: This class does not implement the RemoteObject part of the
  23. documentation.
  24. @ivar length: [read-only] The number of items in this collection.
  25. Introduced in 0.4.
  26. @type length: C{int}
  27. """
  28. class __amf__:
  29. external = True
  30. amf3 = True
  31. exclude = ('length',)
  32. def __init__(self, source=None):
  33. if source is not None:
  34. if isinstance(source, dict):
  35. raise TypeError('Cannot convert dicts to ArrayCollection')
  36. if hasattr(source, '__iter__'):
  37. self.extend(source)
  38. def __repr__(self):
  39. return "<flex.messaging.io.ArrayCollection %s>" % list.__repr__(self)
  40. def __readamf__(self, input):
  41. data = input.readObject()
  42. if hasattr(data, 'source'):
  43. data = data.source
  44. else:
  45. if not hasattr(data, '__iter__'):
  46. raise pyamf.DecodeError(
  47. 'Unable to read a list when decoding ArrayCollection'
  48. )
  49. self.extend(data)
  50. def __writeamf__(self, output):
  51. # meh, this needs to be re-thought out
  52. output.encoder.writeList(list(self), is_proxy=True)
  53. def _get_length(self):
  54. return len(self)
  55. def _set_length(self, length):
  56. raise AttributeError("Property length is read-only")
  57. length = property(_get_length, _set_length)
  58. def addItem(self, item):
  59. """
  60. Adds the specified item to the end of the list.
  61. @param item: The object to add to the collection.
  62. @since: 0.4
  63. """
  64. self.append(item)
  65. def addItemAt(self, item, index):
  66. """
  67. Adds the item at the specified index.
  68. @param item: The object to add to the collection.
  69. @param index: The index at which to place the item.
  70. @raise IndexError: If index is less than 0 or greater than the length
  71. of the list.
  72. @since: 0.4
  73. """
  74. if index < 0 or index > len(self):
  75. raise IndexError
  76. self.insert(index, item)
  77. def getItemAt(self, index, prefetch=0):
  78. """
  79. Gets the item at the specified index.
  80. @param index: The index in the list from which to retrieve the item.
  81. @type index: C{int}
  82. @param prefetch: This param is ignored and is only here as part of the
  83. interface.
  84. @raise IndexError: if `index < 0` or `index >= length`
  85. @since: 0.4
  86. """
  87. if index < 0:
  88. raise IndexError
  89. if index > len(self):
  90. raise IndexError
  91. return self.__getitem__(index)
  92. def getItemIndex(self, item):
  93. """
  94. Returns the index of the item if it is in the list such that
  95. C{getItemAt(index) == item}.
  96. @return: The index of the item or C{-1} if the item is not in the list.
  97. @since: 0.4
  98. """
  99. try:
  100. return self.index(item)
  101. except ValueError:
  102. return -1
  103. def removeAll(self):
  104. """
  105. Removes all items from the list.
  106. @since: 0.4
  107. """
  108. while len(self) > 0:
  109. self.pop()
  110. def removeItemAt(self, index):
  111. """
  112. Removes the item at the specified index and returns it. Any items that
  113. were after this index are now one index earlier.
  114. @param index: The index from which to remove the item.
  115. @return: The item that was removed.
  116. @raise IndexError: If index is less than 0 or greater than length.
  117. @since: 0.4
  118. """
  119. if index < 0 or index > len(self):
  120. raise IndexError
  121. x = self[index]
  122. del self[index]
  123. return x
  124. def setItemAt(self, item, index):
  125. """
  126. Places the item at the specified index. If an item was already at that
  127. index the new item will replace it and it will be returned.
  128. @return: The item that was replaced, or C{None}.
  129. @raise IndexError: If index is less than 0 or greater than length.
  130. @since: 0.4
  131. """
  132. if index < 0 or index > len(self):
  133. raise IndexError
  134. tmp = self.__getitem__(index)
  135. self.__setitem__(index, item)
  136. return tmp
  137. def toArray(self):
  138. """
  139. Returns an Array that is populated in the same order as the C{IList}
  140. implementation.
  141. @return: The array.
  142. @rtype: C{list}
  143. """
  144. return self
  145. class ObjectProxy(object):
  146. """
  147. I represent the ActionScript 3 based class C{flex.messaging.io.ObjectProxy}
  148. used in the Flex framework. Flex's C{ObjectProxy} class allows an
  149. anonymous, dynamic ActionScript Object to be bindable and report change
  150. events.
  151. @see: U{ObjectProxy on Livedocs<http://
  152. help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/
  153. ObjectProxy.html>}
  154. """
  155. class __amf__:
  156. external = True
  157. amf3 = True
  158. def __init__(self, object=None):
  159. if object is None:
  160. self._amf_object = pyamf.ASObject()
  161. else:
  162. self._amf_object = object
  163. def __repr__(self):
  164. return "<flex.messaging.io.ObjectProxy %r>" % self._amf_object
  165. def __getattr__(self, name):
  166. if name == '_amf_object':
  167. return self.__dict__['_amf_object']
  168. return getattr(self.__dict__['_amf_object'], name)
  169. def __setattr__(self, name, value):
  170. if name == '_amf_object':
  171. self.__dict__['_amf_object'] = value
  172. else:
  173. setattr(self._amf_object, name, value)
  174. def __readamf__(self, input):
  175. self._amf_object = input.readObject()
  176. def __writeamf__(self, output):
  177. output.encoder.writeObject(self._amf_object, is_proxy=True)
  178. def unproxy_object(obj):
  179. """
  180. Returns the unproxied version of the object.
  181. """
  182. if isinstance(obj, ArrayCollection):
  183. return list(obj)
  184. elif isinstance(obj, ObjectProxy):
  185. return obj._amf_object
  186. return obj
  187. def proxy_object(obj):
  188. """
  189. Returns a proxied representation of C{obj}
  190. Conversion
  191. ==========
  192. - C{list}: L{ArrayCollection}
  193. - C{dict}: L{ObjectProxy}
  194. - Everything else: C{obj}
  195. @since: 0.6
  196. """
  197. if type(obj) in (list, tuple):
  198. return ArrayCollection(obj)
  199. if isinstance(obj, dict):
  200. return ObjectProxy(obj)
  201. return obj
  202. pyamf.register_package(globals(), package='flex.messaging.io')