def addCallable(self, callable): """Add a callable to list of callables for this topic node""" try: id = self.__callables.index(_getWeakRef(callable)) return self.__callables[id] except ValueError: wrCall = _getWeakRef(callable, _NodeCallback(self.__notifyDead)) self.__callables.append(wrCall) return wrCall
def __init__(self): self.__callbackDict = {} self.__callbackDictCleanup = 0 # all child nodes will call our __rootNotifyDead method # when one of their registered listeners dies _TopicTreeNode.__init__(self, (ALL_TOPICS,), _getWeakRef(self.__rootNotifyDead))
def hasCallable(self, callable): """Return true if callable in this node""" try: self.__callables.index(_getWeakRef(callable)) return True except ValueError: return False
def getTopics(self, listener): """Return the list of topics for given listener""" weakNodes = self.__callbackDict.get(_getWeakRef(listener), []) return [ weakNode().getPathname() for weakNode in weakNodes if weakNode() is not None ]
def __init__(self): self.__callbackDict = {} self.__callbackDictCleanup = 0 # all child nodes will call our __rootNotifyDead method # when one of their registered listeners dies _TopicTreeNode.__init__(self, (ALL_TOPICS, ), _getWeakRef(self.__rootNotifyDead))
def removeCallable(self, callable): """Remove weak callable from our node (and return True). Does nothing if not here (and returns False).""" try: self.__callables.remove(_getWeakRef(callable)) return True except ValueError: return False
def isSubscribed(self, listener, topic=None): """Return true if listener is registered for topic specified. If no topic specified, return true if subscribed to something. Use topic=getStrAllTopics() to determine if a listener will receive messages for all topics.""" weakCB = _getWeakRef(listener) if topic is None: return self.__callbackDict.has_key(weakCB) else: topicPath = _tupleize(topic) for weakNode in self.__callbackDict[weakCB]: if topicPath == weakNode().getPathname(): return True return False
def unsubscribe(self, listener, topicList): """Remove listener from given list of topics. If topicList doesn't have any topics for which listener has subscribed, nothing happens.""" weakCB = _getWeakRef(listener) if not self.__callbackDict.has_key(weakCB): return cbNodes = self.__callbackDict[weakCB] if topicList is None: for weakNode in cbNodes: weakNode().removeCallable(listener) del self.__callbackDict[weakCB] return for weakNode in cbNodes: node = weakNode() if node is not None and node.getPathname() in topicList: success = node.removeCallable(listener) assert success == True cbNodes.remove(weakNode) assert not self.isSubscribed(listener, node.getPathname())
def getTopics(self, listener): """Return the list of topics for given listener""" weakNodes = self.__callbackDict.get(_getWeakRef(listener), []) return [weakNode().getPathname() for weakNode in weakNodes if weakNode() is not None]
def __init__(self, obj): self.objRef = _getWeakRef(obj)