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: 7162 $" 
26   
27   
28 -def _make_watched(type, *mutators):
29 30 class Watched(type): 31 32 def __init__(self): 33 type.__init__(self) 34 self.watch_id = 0 35 self.watch_procs = {} # id -> proc
36 37 def watch(self, proc): 38 self.watch_id += 1 39 self.watch_procs[self.watch_id] = proc 40 return self.watch_id 41 42 def unwatch(self, id): 43 del self.watch_procs[id] 44 45 def notify_changed(self): 46 for proc in self.watch_procs.values(): 47 proc(self) 48 49 def mutate(method): 50 51 def do_mutate(self, *args, **kwargs): 52 method(self, *args, **kwargs) 53 self.notify_changed() 54 setattr(Watched, method.__name__, do_mutate) 55 for i in mutators: 56 mutate(getattr(type, i)) 57 58 return Watched 59 60 WatchedList = _make_watched(list, 'append', 'insert', 'remove', 'pop', 61 'sort', 'reverse') 62 WatchedDict = _make_watched(dict, '__setitem__', '__delitem__', 'pop', 63 'popitem', 'update') 64