home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / util / observe / observablelist.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  7.7 KB  |  207 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from observable import ObservableBase
  5. from util.introspect import decormethod
  6. import sys
  7. from logging import getLogger
  8. log = getLogger('observablelist')
  9.  
  10. try:
  11.     sentinel
  12. except NameError:
  13.     
  14.     class Sentinel(object):
  15.         
  16.         def __repr__(self):
  17.             return '<Sentinel (%r backup) %#x>' % (__file__, id(self))
  18.  
  19.  
  20.     sentinel = Sentinel()
  21.  
  22.  
  23. def list_notify(f):
  24.     
  25.     def wrapper1(self, *args, **kw):
  26.         old_list = self[:]
  27.         val = f(self, *args, **kw)
  28.         self.notify('list', old_list, self)
  29.         return val
  30.  
  31.     return wrapper1
  32.  
  33.  
  34. class ObservableList(list, ObservableBase):
  35.     
  36.     def __init__(self, *args):
  37.         ObservableBase.__init__(self)
  38.         list.__init__(self, *args)
  39.         self._observable_frozen = False
  40.         self._freezecount = 0
  41.  
  42.     
  43.     def notify_wrap(self, func, *args, **kw):
  44.         val = func(self, *args, **kw)
  45.         if not self._observable_frozen:
  46.             self.notify('list', None, self)
  47.         
  48.         return val
  49.  
  50.     notify_wrap = decormethod(notify_wrap)
  51.     
  52.     def add_list_observer(self, list_changed, child_changed, *attrs, **kwargs):
  53.         ch = kwargs.get('childfunc', (lambda child: child))
  54.         
  55.         class ListWatcher((object,)):
  56.             
  57.             def __init__(self, srclist, list_callback, child_callback, *attrs, **kwargs):
  58.                 srclist.add_observer(self.on_list_changed, **kwargs)
  59.                 self.srclist = srclist
  60.                 self.listcopy = set(srclist)
  61.                 self.list_callback = list_callback
  62.                 self.child_args = [
  63.                     child_callback] + list(attrs)
  64.                 self.child_kwargs = dict(kwargs)
  65.                 for c in srclist:
  66.                     pass
  67.                 
  68.  
  69.             
  70.             def on_list_changed(self, src, attr, old, new):
  71.                 new = set(src)
  72.                 old = self.listcopy
  73.                 chargs = self.child_args
  74.                 kwargs = self.child_kwargs
  75.                 for newchild in new - old:
  76.                     ch(newchild).add_observer(*chargs, **kwargs)
  77.                 
  78.                 for oldchild in old - new:
  79.                     ch(oldchild).remove_observer(*chargs, **kwargs)
  80.                 
  81.                 self.listcopy = new
  82.  
  83.             
  84.             def disconnect(self):
  85.                 chargs = self.child_args
  86.                 kwargs = self.child_kwargs
  87.                 for child in self.srclist:
  88.                     child.remove_observer(*chargs)
  89.                 
  90.                 self.srclist.remove_observer(self.on_list_changed)
  91.                 self.srclist.remove_observer(self.list_callback)
  92.                 self.__dict__.clear()
  93.                 self.disconnect = self.disconnect_warning
  94.  
  95.             
  96.             def disconnect_warning(self):
  97.                 log.critical('calling ListWatcher.disconnect more than once')
  98.  
  99.  
  100.         if list_changed is not None:
  101.             self.add_observer(list_changed, **kwargs)
  102.         
  103.         return ListWatcher(self, list_changed, child_changed, *attrs, **kwargs)
  104.  
  105.     
  106.     def remove_list_observer(self, list_changed, child_changed, *attrs):
  107.         if list_changed is not None:
  108.             self.remove_observer(list_changed)
  109.         
  110.  
  111.     
  112.     def __setitem__(self, i, v):
  113.         return list.__setitem__(self, i, v)
  114.  
  115.     __setitem__ = notify_wrap(__setitem__)
  116.     
  117.     def __delitem__(self, key):
  118.         return list.__delitem__(self, key)
  119.  
  120.     __delitem__ = notify_wrap(__delitem__)
  121.     
  122.     def __setslice__(self, i, j, seq):
  123.         return list.__setslice__(self, i, j, seq)
  124.  
  125.     __setslice__ = notify_wrap(__setslice__)
  126.     
  127.     def __delslice__(self, i, j):
  128.         return list.__delslice__(self, i, j)
  129.  
  130.     __delslice__ = notify_wrap(__delslice__)
  131.     
  132.     def append(self, elem):
  133.         return list.append(self, elem)
  134.  
  135.     append = notify_wrap(append)
  136.     
  137.     def pop(self, i = sentinel):
  138.         if i is not sentinel:
  139.             return list.pop(self, i)
  140.         else:
  141.             return list.pop(self)
  142.  
  143.     pop = notify_wrap(pop)
  144.     
  145.     def extend(self, seq):
  146.         return list.extend(self, seq)
  147.  
  148.     extend = notify_wrap(extend)
  149.     
  150.     def insert(self, i, elem):
  151.         return list.insert(self, i, elem)
  152.  
  153.     insert = notify_wrap(insert)
  154.     
  155.     def remove(self, elem):
  156.         return list.remove(self, elem)
  157.  
  158.     remove = notify_wrap(remove)
  159.     
  160.     def reverse(self):
  161.         return list.reverse(self)
  162.  
  163.     reverse = notify_wrap(reverse)
  164.     
  165.     def sort(self, *a, **k):
  166.         return list.sort(self, *a, **k)
  167.  
  168.     sort = notify_wrap(sort)
  169.     
  170.     def __iadd__(self, item):
  171.         return list.__iadd__(self, item)
  172.  
  173.     __iadd__ = notify_wrap(__iadd__)
  174.  
  175. observable_list = ObservableList
  176. if __name__ == '__main__':
  177.     from util.observe.observable import Observable
  178.     
  179.     class Buddy(Observable):
  180.         
  181.         def __init__(self, name, status = 'online'):
  182.             Observable.__init__(self)
  183.             self.name = name
  184.             self.status = status
  185.  
  186.         
  187.         def __repr__(self):
  188.             return '<Buddy %s (%s)>' % (self.name, self.status)
  189.  
  190.  
  191.     buddies = []([ Buddy(n) for n in 'Bobby Barbera Betty Ben'.split() ])
  192.     
  193.     def buddy_list_changed(src, attr, old, new):
  194.         print 'The buddylist changed. New list is %r' % new
  195.  
  196.     
  197.     def buddy_attr_changed(src, attr, old, new):
  198.         print 'Buddy %r changed %s from %s to %s' % (src, attr, old, new)
  199.  
  200.     buddies.add_list_observer(buddy_list_changed, buddy_attr_changed)
  201.     print 'Original list', buddies
  202.     b = buddies[2]
  203.     b.setnotify('status', 'away')
  204.     del buddies[2]
  205.     b.setnotify('status', 'online')
  206.  
  207.