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 hasattr(data, 'source'): 54 data = data.source 55 else: 56 if not hasattr(data, '__iter__'): 57 raise pyamf.DecodeError('Unable to read a list when decoding ' 58 'ArrayCollection') 59 60 self.extend(data)
61
62 - def __writeamf__(self, output):
63 output.encoder.writeList(list(self), use_references=True, _use_proxies=False)
64
65 - def _get_length(self):
66 return len(self)
67
68 - def _set_length(self, length):
69 raise RuntimeError("Property length is read-only")
70 71 length = property(_get_length, _set_length) 72
73 - def addItem(self, item):
74 """ 75 Adds the specified item to the end of the list. 76 77 @param item: The object to add to the collection. 78 @type item: C{mixed}. 79 @since: 0.4 80 """ 81 self.append(item)
82
83 - def addItemAt(self, item, index):
84 """ 85 Adds the item at the specified index. 86 87 @param item: The object to add to the collection. 88 @type item: C{mixed}. 89 @param index: The index at which to place the item. 90 @raise IndexError: If index is less than 0 or greater than the length 91 of the list. 92 @since: 0.4 93 """ 94 if index < 0: 95 raise IndexError 96 97 if index > len(self): 98 raise IndexError 99 100 self.insert(index, item)
101
102 - def getItemAt(self, index, prefetch=0):
103 """ 104 Gets the item at the specified index. 105 106 @param index: The index in the list from which to retrieve the item. 107 @type index: C{int} 108 @param prefetch: This param is ignored and is only here as part of the 109 interface. 110 @raise IndexError: if C{index < 0} or C{index >= length} 111 @return: The item at index C{index}. 112 @rtype: C{mixed}. 113 @since: 0.4 114 """ 115 if index < 0: 116 raise IndexError 117 118 if index > len(self): 119 raise IndexError 120 121 return self.__getitem__(index)
122
123 - def getItemIndex(self, item):
124 """ 125 Returns the index of the item if it is in the list such that 126 C{getItemAt(index) == item}. 127 128 @param item: The item to find. 129 @type item: C{mixed}. 130 @return: The index of the item or -1 if the item is not in the list. 131 @rtype: C{int} 132 @since: 0.4 133 """ 134 try: 135 return self.index(item) 136 except ValueError: 137 return -1
138
139 - def removeAll(self):
140 """ 141 Removes all items from the list. 142 143 @since: 0.4 144 """ 145 while len(self) > 0: 146 self.pop()
147
148 - def removeItemAt(self, index):
149 """ 150 Removes the item at the specified index and returns it. Any items that 151 were after this index are now one index earlier. 152 153 @param index: The index from which to remove the item. 154 @return: The item that was removed. 155 @rtype: C{mixed}. 156 @raise IndexError: If index is less than 0 or greater than length. 157 @since: 0.4 158 """ 159 if index < 0: 160 raise IndexError 161 162 if index > len(self): 163 raise IndexError 164 165 x = self[index] 166 167 del self[index] 168 169 return x
170
171 - def setItemAt(self, item, index):
172 """ 173 Places the item at the specified index. If an item was already at that 174 index the new item will replace it and it will be returned. 175 176 @param item: The new item to be placed at the specified index. 177 @type item: C{mixed}. 178 @param index: The index at which to place the item. 179 @type index: C{int} 180 @return: The item that was replaced, or C{None}. 181 @rtype: C{mixed} or C{None}. 182 @raise IndexError: If index is less than 0 or greater than length. 183 @since: 0.4 184 """ 185 if index < 0: 186 raise IndexError 187 188 if index > len(self): 189 raise IndexError 190 191 tmp = self.__getitem__(index) 192 self.__setitem__(index, item) 193 194 return tmp
195
196 - def toArray(self):
197 """ 198 Returns an Array that is populated in the same order as the C{IList} 199 implementation. 200 201 @return: The array. 202 @rtype: C{list} 203 """ 204 return self
205 206 pyamf.register_class(ArrayCollection, 'flex.messaging.io.ArrayCollection', 207 metadata=['external', 'amf3']) 208
209 -class ObjectProxy(object):
210 """ 211 I represent the ActionScript 3 based class C{flex.messaging.io.ObjectProxy} 212 used in the Flex framework. Flex's C{ObjectProxy} class allows an anonymous, 213 dynamic ActionScript Object to be bindable and report change events. 214 215 @see: U{ObjectProxy on Livedocs (external) 216 <http://livedocs.adobe.com/flex/201/langref/mx/utils/ObjectProxy.html>} 217 """ 218
219 - def __init__(self, object=None):
220 if object is None: 221 self._amf_object = pyamf.ASObject() 222 else: 223 self._amf_object = object
224
225 - def __repr__(self):
226 return "<flex.messaging.io.ObjectProxy %s>" % self._amf_object
227
228 - def __getattr__(self, name):
229 if name == '_amf_object': 230 return self.__dict__['_amf_object'] 231 232 return getattr(self.__dict__['_amf_object'], name)
233
234 - def __setattr__(self, name, value):
235 if name == '_amf_object': 236 self.__dict__['_amf_object'] = value 237 else: 238 setattr(self._amf_object, name, value)
239
240 - def __readamf__(self, input):
241 self._amf_object = input.readObject()
242
243 - def __writeamf__(self, output):
244 output.writeObject(self._amf_object)
245 246 pyamf.register_class(ObjectProxy, 'flex.messaging.io.ObjectProxy', 247 metadata=['external', 'amf3']) 248