aboutsummaryrefslogtreecommitdiffstats
path: root/pubsub.py
blob: 6b860e0fed991198f233cd98b232d36eb992475a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Publisher(object):
    def __init__(self):
        self.subscribers = []

    def subscribe(self, who):
        if who not in self.subscribers:
            self.subscribers.append(who)

    def unsubscribe(self, who):
        if who in self.subscribers:
            self.subscribers.remove(who)

    def publish(self, message, *args):
        for subscriber in self.subscribers:
            method = "msg_" + message
            if hasattr(subscriber, method):
                getattr(subscriber, method)(*args)