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