From c6633dae0e1886ac415dcb74fa0b2bde1ea48e66 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 14 Sep 2014 23:37:56 -0400 Subject: introduce a pubsub system for communication --- pubsub.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 pubsub.py (limited to 'pubsub.py') diff --git a/pubsub.py b/pubsub.py new file mode 100644 index 0000000..6b860e0 --- /dev/null +++ b/pubsub.py @@ -0,0 +1,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) -- cgit v1.2.3-54-g00ecf