git-cola v1.9.4
[git-cola.git] / cola / observable.py
blob397809daf5feec979ed708c4f616e4367016dd22
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."""
8 def __init__(self):
9 self.notification_enabled = True
10 self.observers = {}
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:
26 return
27 observers = self.observers.get(message, ())
28 for method in observers:
29 method(*args, **opts)