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."""
10 self
.notification_enabled
= True
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
:
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
: