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