Package pyamf :: Package flex
[hide private]
[frames] | no frames]

Source Code for Package pyamf.flex

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