2 # Copyright (c) 2008 David Aguilar
3 from pprint
import pformat
5 class Observer(object):
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.
12 def __init__(self
, model
):
14 self
.model
.add_observer(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
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