interaction: flush stdout when printing to the console
[git-cola.git] / cola / observable.py
blob2e26e1aa366c94ae8ba55ebdb0c3cacb43476452
1 # Copyright (C) 2007-2018 David Aguilar and contributors
2 """This module provides the Observable class"""
3 from __future__ import division, absolute_import, unicode_literals
6 class Observable(object):
7 """Handles subject/observer notifications."""
9 def __init__(self):
10 self.notification_enabled = True
11 self.observers = {}
13 def add_observer(self, message, observer):
14 """Add an observer for a specific message."""
15 observers = self.observers.setdefault(message, set())
16 observers.add(observer)
18 def remove_observer(self, observer):
19 """Remove an observer."""
20 for _, observers in self.observers.items():
21 if observer in observers:
22 observers.remove(observer)
24 def notify_observers(self, message, *args, **opts):
25 """Pythonic signals and slots."""
26 if not self.notification_enabled:
27 return
28 # observers can remove themselves during their callback so grab a copy
29 observers = set(self.observers.get(message, set()))
30 for method in observers:
31 method(*args, **opts)