Package buildbot :: Package util :: Module subscription
[frames] | no frames]

Source Code for Module buildbot.util.subscription

 1  # This file is part of Buildbot.  Buildbot is free software: you can 
 2  # redistribute it and/or modify it under the terms of the GNU General Public 
 3  # License as published by the Free Software Foundation, version 2. 
 4  # 
 5  # This program is distributed in the hope that it will be useful, but WITHOUT 
 6  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
 8  # details. 
 9  # 
10  # You should have received a copy of the GNU General Public License along with 
11  # this program; if not, write to the Free Software Foundation, Inc., 51 
12  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
13  # 
14  # Copyright Buildbot Team Members 
15   
16  """ 
17  Classes to handle subscriptions to event streams. 
18   
19  This will eventually (in the 0.9.x timeframe) be replaced with an 
20  implementation based on message passing.  Users should be aware they will need 
21  to rewrite any custom code that touches this module! 
22  """ 
23   
24  from twisted.python import failure, log 
25   
26 -class SubscriptionPoint(object):
27 """ 28 Something that can be subscribed to. 29 """
30 - def __init__(self, name):
31 self.name = name 32 self.subscriptions = set()
33
34 - def __str__(self):
35 return "<SubscriptionPoint '%s'>" % self.name
36
37 - def subscribe(self, callback):
38 """Add C{callback} to the subscriptions; returns a L{Subscription} 39 instance.""" 40 sub = Subscription(self, callback) 41 self.subscriptions.add(sub) 42 return sub
43
44 - def deliver(self, *args, **kwargs):
45 """ 46 Deliver the given args and keyword args to all of the current 47 subscribers. 48 """ 49 for sub in list(self.subscriptions): 50 try: 51 sub.callback(*args, **kwargs) 52 except: 53 log.err(failure.Failure(), 54 'while invoking callback %s to %s' % (sub.callback, self))
55
56 - def _unsubscribe(self, subscription):
57 self.subscriptions.remove(subscription)
58
59 -class Subscription(object):
60 """ 61 Represents a subscription to a L{SubscriptionPoint}; use 62 L{SubscriptionPoint.subscribe} to get an instance. 63 """
64 - def __init__(self, subpt, callback):
65 self.subpt = subpt 66 self.callback = callback
67
68 - def unsubscribe(self):
69 "Cancel the subscription" 70 self.subpt._unsubscribe(self)
71