1 # Copyright (c) 2008 David Aguilar
2 """This module provides the Observable class"""
4 from cola
.compat
import set
6 class Observable(object):
7 """Handles subject/observer notifications."""
9 self
.notification_enabled
= True
12 def add_observer(self
, message
, observer
):
13 """Add an observer for a specific message."""
14 observers
= self
.observers
.setdefault(message
, set())
15 observers
.add(observer
)
17 def remove_observer(self
, observer
):
18 """Remove an observer."""
19 for message
, observers
in self
.observers
.items():
20 if observer
in observers
:
21 observers
.remove(observer
)
23 def notify_observers(self
, message
, *args
, **opts
):
24 """Pythonic signals and slots."""
25 if not self
.notification_enabled
:
27 observers
= self
.observers
.get(message
, ())
28 for method
in observers
: