Package elisa :: Package core :: Package utils :: Module notifying_dict

Source Code for Module elisa.core.utils.notifying_dict

 1  # -*- coding: utf-8 -*- 
 2  # Moovida - Home multimedia server 
 3  # Copyright (C) 2006-2009 Fluendo Embedded S.L. (www.fluendo.com). 
 4  # All rights reserved. 
 5  # 
 6  # This file is available under one of two license agreements. 
 7  # 
 8  # This file is licensed under the GPL version 3. 
 9  # See "LICENSE.GPL" in the root of this distribution including a special 
10  # exception to use Moovida with Fluendo's plugins. 
11  # 
12  # The GPL part of Moovida is also available under a commercial licensing 
13  # agreement from Fluendo. 
14  # See "LICENSE.Moovida" in the root directory of this distribution package 
15  # for details on that license. 
16   
17  """ 
18  Notifying dictionary. 
19  """ 
20   
21  import gobject 
22   
23   
24 -class Notifier(gobject.GObject):
25 26 """ 27 A notifier that emits an items-changed signal when its associated dictionary 28 is updated. 29 30 The items-changed signal is emitted with three parameters: the dictionary 31 itself, the key of the modified item, the value of the modified item. 32 """ 33 34 __gsignals__ = \ 35 {'items-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_PYOBJECT, 36 (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT)), 37 }
38 39 gobject.type_register(Notifier) 40 41
42 -class NotifyingDictionary(dict):
43 44 """ 45 A dictionary that emits a signal when items are set/modified. 46 47 The signal is emitted through a L{Notifier} object. 48 """ 49 50 # FIXME: incomplete: it does not override all the dict methods that write: 51 # - setdefault 52 # - clear 53 # - pop 54 # - popitem 55 # - __delitem__ 56 # 57 # cf. http://www.python.org/doc/2.5.2/lib/typesmapping.html 58
59 - def __init__(self, *args, **kwargs):
60 dict.__init__(self, *args, **kwargs) 61 self.notifier = Notifier()
62
63 - def __setitem__(self, key, value):
64 dict.__setitem__(self, key, value) 65 self.notifier.emit('items-changed', self, {key: value})
66
67 - def update(self, other):
68 dict.update(self, other) 69 self.notifier.emit('items-changed', self, other)
70
71 - def __deepcopy__(self, memo):
72 new_dict = self.__class__(self) 73 return new_dict
74