Package flumotion :: Package common :: Module watched
[hide private]

Source Code for Module flumotion.common.watched

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  """abstract data types with built-in notification support 
 19  """ 
 20   
 21  __version__ = "$Rev$" 
 22   
 23   
24 -class WatchedList(list):
25
26 - def __init__(self):
27 list.__init__(self) 28 self.watch_id = 0 29 self.watch_procs = {}
30
31 - def append(self, o):
32 list.append(self, o) 33 self.notify_changed(o)
34
35 - def insert(self, idx, o):
36 list.insert(self, idx, o) 37 self.notify_changed(o)
38
39 - def remove(self, o):
40 list.remove(self, o) 41 self.notify_changed(o)
42
43 - def pop(self, *args):
44 o = list.pop(self, *args) 45 self.notify_changed(o) 46 return o
47
48 - def sort(self, *args, **kwargs):
49 list.sort(self, *args, **kwargs) 50 self.notify_changed(self)
51
52 - def reverse(self):
53 list.reverse(self) 54 self.notify_changed(self)
55
56 - def notify_changed(self, obj):
57 for proc in self.watch_procs.values(): 58 proc(obj)
59
60 - def watch(self, proc):
61 self.watch_id += 1 62 self.watch_procs[self.watch_id] = proc 63 return self.watch_id
64
65 - def unwatch(self, proc_id):
66 del self.watch_procs[proc_id]
67 68
69 -class WatchedDict(dict):
70
71 - def __init__(self):
72 dict.__init__(self) 73 self.watch_id = 0 74 self.watch_procs = {}
75
76 - def __setitem__(self, key, val):
77 dict.__setitem__(self, key, val) 78 self.notify_changed((key, val))
79
80 - def __delitem__(self, key):
81 val = self[key] 82 dict.__delitem__(self, key) 83 self.notify_changed((key, val))
84
85 - def pop(self, key, *args):
86 if len(args) > 1: 87 raise TypeError('pop expected at most 2 arguments, got %d' % 88 (len(args) + 1)) 89 try: 90 val = dict.pop(self, key) 91 except KeyError: 92 if not len(args): 93 raise 94 val = args[0] 95 self.notify_changed((key, val))
96
97 - def popitem(self):
98 ret = dict.popitem(self) 99 self.notify_changed(ret) 100 return ret
101
102 - def update(self, *args, **kwargs):
103 dict.update(self, *args, **kwargs) 104 self.notify_changed(self)
105
106 - def notify_changed(self, obj):
107 for proc in self.watch_procs.values(): 108 proc(obj)
109
110 - def watch(self, proc):
111 self.watch_id += 1 112 self.watch_procs[self.watch_id] = proc 113 return self.watch_id
114
115 - def unwatch(self, proc_id):
116 del self.watch_procs[proc_id]
117