models/utils: more unicode fixes
[git-cola.git] / cola / observer.py
blob6687166c3b61a57bf91960332bf146195ccdee06
1 #!/usr/bin/env python
2 # Copyright (c) 2008 David Aguilar
3 from pprint import pformat
5 class Observer(object):
6 """
7 Observers receive notify(*attributes) messages from their
8 subjects whenever new data arrives. This notify() message signifies
9 that an observer should update its internal state/view.
10 """
12 def __init__(self, model):
13 self.model = model
14 self.model.add_observer(self)
15 self.__debug = False
17 def __del__(self):
18 self.model.remove_observer(self)
20 def set_debug(self, enabled):
21 self.__debug = enabled
23 def notify(self, *attributes):
24 """Called by the model to notify Observers about changes."""
25 # We can be notified about multiple attribute changes at once
26 model = self.model
27 for attr in attributes:
28 notify = model.get_notify()
29 model.set_notify(False) # NOTIFY OFF
31 value = model.get_param(attr)
32 self.subject_changed(attr, value)
34 model.set_notify(notify) # NOTIFY ON
36 def subject_changed(self, attr, value):
37 """This method handles updating of the observer/UI.
38 This must be implemented in each concrete observer class."""
40 msg = 'Concrete Observers must override subject_changed().'
41 raise NotImplementedError, msg