def __createTopic(self, nameTuple, desc, specGiven, parent=None): '''Actual topic creation step. Adds new Topic instance to topic map, and sends notification message (of topic 'pubsub.newTopic') about new topic having been created.''' if specGiven is None: specGiven = ArgSpecGiven() parentAI = None if parent: parentAI = parent._getListenerSpec() argsInfo = ArgsInfo(nameTuple, specGiven, parentAI) if (self.__treeConfig.raiseOnTopicUnspecified and not argsInfo.isComplete()): raise ListenerSpecIncomplete(nameTuple) newTopicObj = Topic(self.__treeConfig, nameTuple, desc, argsInfo, parent = parent) # sanity checks: assert not self._topicsMap.has_key(newTopicObj.getName()) if parent is self.__allTopics: assert len( newTopicObj.getNameTuple() ) == 1 else: assert parent.getNameTuple() == newTopicObj.getNameTuple()[:-1] assert nameTuple == newTopicObj.getNameTuple() # store new object and notify of creation self._topicsMap[ newTopicObj.getName() ] = newTopicObj self.__treeConfig.notificationMgr.notifyNewTopic( newTopicObj, desc, specGiven.reqdArgs, specGiven.argsDocs) return newTopicObj
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)
def getDefn(self, topicNameTuple): desc, spec = None, None defn = self.__topicDefns.get(topicNameTuple, None) if defn is not None: assert defn.isComplete() desc = defn.description spec = ArgSpecGiven(defn.argsDocs, defn.required) return desc, spec
def __init__(self, treeConfig=None): '''The optional treeConfig is an instance of TreeConfig. A default one is created if not given. ''' self.__allTopics = None # root of topic tree self._topicsMap = {} # registry of all topics self.__treeConfig = treeConfig or TreeConfig() self.__defnProvider = MasterTopicDefnProvider(self.__treeConfig) # define root of all topics assert self.__allTopics is None argsDocs, reqdArgs = getRootTopicSpec() desc = 'Root of all topics' specGiven = ArgSpecGiven(argsDocs, reqdArgs) self.__allTopics = self.__createTopic((ALL_TOPICS,), desc, specGiven=specGiven)
def getDefn(self, topicNameTuple): return 'From incompletely implemented PROVIDER', ArgSpecGiven()