-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.py
37 lines (26 loc) · 988 Bytes
/
notification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
from compat import set
from decorators import memoize
debug = os.environ.get('COLA_NOTIFIER_DEBUG', False)
@memoize
def notifier():
return Notifier()
class Notifier(object):
"""Object for sending and receiving notification messages"""
def __init__(self):
self.channels = {}
def broadcast(self, signal, *args, **opts):
if debug:
print ('broadcast: %s(%s, %s)' % (signal,
args or '<empty>',
opts or '<empty>'))
self.emit(signal, *args, **opts)
def emit(self, signal, *args, **opts):
subscribers = self.channels.get(signal, None)
if not subscribers:
return
for fxn in subscribers:
fxn(*args, **opts)
def connect(self, signal, callback):
subscribers = self.channels.setdefault(signal, set())
subscribers.add(callback)