Ejemplo n.º 1
0
 def __init__(self, callable_, argsInfo, onDead=None):
     '''Use callable_ as a listener of topicName. The argsInfo is the 
     return value from a Validator, ie an instance of callables.CallArgsInfo.
     If given, the onDead will be called with self as parameter, if/when
     callable_ gets garbage collected (callable_ is held only by weak
     reference). '''
     # set call policies
     self.acceptsAllKwargs = argsInfo.acceptsAllKwargs
     
     self._autoTopicArgName = argsInfo.autoTopicArgName
     self._callable = weakmethod.getWeakRef(callable_, self.__notifyOnDead)
     self.__onDead = onDead
     
     # save identity now in case callable dies:
     name, mod = getID(callable_)   #
     self.__nameID = name
     self.__module = mod 
     self.__id     = str(id(callable_))[-4:] # only last four digits of id
     self.__hash   = hash(callable_)
Ejemplo n.º 2
0
    def __init__(self, callable_, argsInfo, onDead=None):
        '''Use callable_ as a listener of topicName. The argsInfo is the 
        return value from a Validator, ie an instance of callables.CallArgsInfo.
        If given, the onDead will be called with self as parameter, if/when
        callable_ gets garbage collected (callable_ is held only by weak
        reference). '''
        # set call policies
        self.acceptsAllKwargs = argsInfo.acceptsAllKwargs

        self._autoTopicArgName = argsInfo.autoTopicArgName
        self._callable = weakmethod.getWeakRef(callable_, self.__notifyOnDead)
        self.__onDead = onDead

        # save identity now in case callable dies:
        name, mod = getID(callable_)  #
        self.__nameID = name
        self.__module = mod
        self.__id = str(id(callable_))[-4:]  # only last four digits of id
        self.__hash = hash(callable_)
Ejemplo n.º 3
0
    def getOrCreateTopic(self, name, protoListener=None):
        '''Get the topic object for topic of given name, creating it 
        (and any of its missing parent topics) as necessary. This should
        be useful mostly to TopicManager itself.

        Topic creation: The topic definition will be obtained
        from the first registered TopicDefnProvider (see addTopicDefnProvider()
        method) that can provide it. If none is found, then protoListener,
        if given, will be used to extract the specification for the topic
        message arguments.
        
        So the topic object returned will be either
        1. an existing one
        2. a new one whose specification was obtained from a TopicDefnProvider
        3. a new one whose specification was inferred from protoListener
        4. a new one without any specification

        For the first three cases, the Topic is ready for sending messages.
        In the last case, topic.isSendable() is false and the specification
        will be set by the first call to subscribe() (unless you call
        topicObj.setMsgArgSpec() first to set it yourself).

        Note that if the topic gets created, missing intervening parents
        will be created with an empty specification. For instance, if topic
        A exists, and name="A.B.C", then A.B will also be created. It will
        only be complete (sendable) if a topic definition provider had
        its definition.

        Note also that if protoListener given, and topic already defined,
        the method does not check whether protoListener adheres to the
        specification.'''
        obj = self.getTopic(name, okIfNone=True)
        if obj:
            # if object is not sendable but a proto listener was given,
            # update its specification so that it is sendable
            if (protoListener is not None) and not obj.isSendable():
                allArgsDocs, required = topicArgsFromCallable(protoListener)
                obj.setMsgArgSpec(allArgsDocs, required)
            return obj

        # create missing parents
        nameTuple = tupleize(name)
        parentObj = self.__createParentTopics(nameTuple)

        # now the final topic object, args from listener if provided
        desc, specGiven = self.__defnProvider.getDefn(nameTuple)
        # POLICY: protoListener is used only if no definition available
        if specGiven is None:
            if protoListener is None:
                desc = 'UNDOCUMENTED: created without spec'
            else:
                allArgsDocs, required = topicArgsFromCallable(protoListener)
                specGiven = ArgSpecGiven(allArgsDocs, required)
                desc = 'UNDOCUMENTED: created from protoListener "%s" in module %s' % getID(protoListener)

        return self.__createTopic(nameTuple, desc, parent = parentObj, specGiven = specGiven)