views.main: Use gitcmds.log_helper()
[git-cola.git] / cola / observable.py
blob4ca03b3e442bad033b164aa522dbef87942150fd
1 # Copyright (c) 2008 David Aguilar
2 """This module provides the Observable class"""
4 import types
6 class Observable(object):
7 """Handles subject/observer notifications."""
8 def __init__(self):
9 self.observers = set()
10 self.message_observers = {}
11 self.notification_enabled = True
13 def add_observer(self, observer):
14 """Adds an observer to this model"""
15 self.observers.add(observer)
17 def add_message_observer(self, message, observer):
18 """Add an observer for a specific message."""
19 observers = self.message_observers.setdefault(message, set())
20 observers.add(observer)
22 def remove_observer(self, observer):
23 """Remove an observer."""
24 if observer in self.observers:
25 self.observers.remove(observer)
26 for message, observers in self.message_observers.items():
27 if observer in observers:
28 observers.remove(observer)
30 def notify_observers(self, *param):
31 """Notifies observers about attribute changes"""
32 if not self.notification_enabled:
33 return
34 for observer in self.observers:
35 observer.notify(*param)
37 def notify_message_observers(self, message, *args, **opts):
38 """Pythonic signals and slots."""
39 if not self.notification_enabled:
40 return
41 observers = self.message_observers.get(message, ())
42 for method in observers:
43 method(*args, **opts)