def _doSub(self, key, val): attributeInfo = self._subscriberAttributes.get(key,{}).items() previousAttribute = getattr(self,key,None) if reflect.isinst(previousAttribute, Publisher): for event, handlers in attributeInfo: for handler in handlers: if handler is None: handler = WhenMethodSubscription(self, key, event) # handler = getattr(self, "when_"+key+"_"+event) try: previousAttribute.unsubscribe(event, handler) except KeyError: # Since this deals with when_ methods, the # most likely time for an exception to be # thrown here is when you've added a when # method. There's no point in stopping # execution there. pass if reflect.isinst(val, Publisher): for event, handlers in attributeInfo: for handler in handlers: if handler is None: handler = WhenMethodSubscription(self, key, event) #handler = getattr(self, "when_"+key+"_"+event) val.subscribe(event, handler)
def unsubscribeFromAttribute(self, attribute, channel, callback): assert not (self._subscriberAttributes is self.__class__._subscriberAttributes),\ "No attribute channels have been subscribed." channels = self._subscriberAttributes[attribute] handlers = channels[channel] handlers.remove(callback) currentPublisher = getattr(self, attribute, None) if reflect.isinst(currentPublisher, Publisher): currentPublisher.unsubscribe(channel, callback)
def __cmp__(self, other): if other is self: return 0 if not reflect.isinst(other, WhenMethodSubscription): return -1 for attr in 'subscriber','attribute','channel': retval = cmp(getattr(self,attr),getattr(other,attr)) if retval != 0: return retval return 0
def subscribeToAttribute(self, attribute, channel, callback): assert callable(callback), "Callback must be a callable type." if self._subscriberAttributes is self.__class__._subscriberAttributes: self._subscriberAttributes = copy.deepcopy(self._subscriberAttributes) channels = self._subscriberAttributes.get(attribute,{}) handlers = channels.get(channel,[]) handlers.append(callback) channels[channel] = handlers self._subscriberAttributes[attribute] = channels currentPublisher = getattr(self, attribute, None) if reflect.isinst(currentPublisher, Publisher): currentPublisher.subscribe(channel, callback)