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

Source Code for Package pyamf.flex

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