def scheduleConnect(self, src, dst, outf): """Add an instance of a connect in the transaction schedule.""" src = keywords.fixID(src) dst = keywords.fixID(dst) with self.transactionStackLock: ac = self.transactionStack[-1].addConnection(src, dst) if not self._tryImmediateTransaction(outf): ac.describe(outf)
def scheduleConnect(self, src, dst, outf): """Add an instance of a connect in the transaction schedule.""" src=keywords.fixID(src) dst=keywords.fixID(dst) with self.transactionStackLock: ac=self.transactionStack[-1].addConnection(src, dst) if not self._tryImmediateTransaction(outf): ac.describe(outf)
def addInstance(self, name, functionName): """Add an instance with a name and function name to the top-level network.""" name = keywords.fixID(name) functionName = keywords.fixID(functionName) with self.updateLock: func = self.imports.getFunctionByFullName(functionName, self.topLevelImport) (net, instanceName) = self.network.getContainingNetwork(name) nm = "" if net.inActiveInstance is not None: nm = net.inActiveInstance.getCanonicalName() #log.debug("net=%s, instanceName=%s"%(nm, instanceName)) inst = instance.Instance(instanceName, func, functionName) net.addInstance(inst)
def addInstance(self, name, functionName): """Add an instance with a name and function name to the top-level network.""" name=keywords.fixID(name) functionName=keywords.fixID(functionName) with self.updateLock: func=self.imports.getFunctionByFullName(functionName, self.topLevelImport) (net, instanceName)=self.network.getContainingNetwork(name) nm="" if net.inActiveInstance is not None: nm=net.inActiveInstance.getCanonicalName() #log.debug("net=%s, instanceName=%s"%(nm, instanceName)) inst=instance.Instance(instanceName, func, functionName) net.addInstance(inst)
def importName(self, name): """Import a named module.""" name = keywords.fixID(name) with self.updateLock: if not self.imports.exists(name): baseFilename = "%s.xml" % name.replace(keywords.ModSep, '/') baseFilename2 = "%s/_import.xml" % name.replace( keywords.ModSep, '/') filename = None for pathItem in self.conf.getImportPaths(): nfilename = os.path.join(pathItem, baseFilename) if os.path.exists(nfilename): filename = nfilename break nfilename = os.path.join(pathItem, baseFilename2) if os.path.exists(nfilename): filename = nfilename break if filename is None: raise ProjectError("Library %s not found" % name) log.debug("Importing library %s with file name %s" % (name, filename)) newlib = lib.ImportLibrary(name, filename, None) reader = readxml.ProjectXMLReader(newlib, self.imports, self) reader.read(filename) self.imports.add(newlib) return newlib else: return self.imports.get(name)
def importName(self, name): """Import a named module.""" name=keywords.fixID(name) with self.updateLock: if not self.imports.exists(name): baseFilename="%s.xml"%name.replace(keywords.ModSep, '/') baseFilename2="%s/_import.xml"%name.replace(keywords.ModSep, '/') filename=None for pathItem in self.conf.getImportPaths(): nfilename=os.path.join(pathItem, baseFilename) if os.path.exists(nfilename): filename=nfilename break nfilename=os.path.join(pathItem, baseFilename2) if os.path.exists(nfilename): filename=nfilename break if filename is None: raise ProjectError("Library %s not found"%name) log.debug("Importing library %s with file name %s"% (name, filename)) newlib=lib.ImportLibrary(name, filename, None) reader=readxml.ProjectXMLReader(newlib, self.imports, self) reader.read(filename) self.imports.add(newlib) return newlib else: return self.imports.get(name)
def getNamedInstance(self, instname): pathname=keywords.fixID(instname) with self.updateLock: itemlist=vtype.parseItemList(pathname) item=self.getSubValue(itemlist) if not isinstance(item, active_inst.ActiveInstance): raise ProjectError("%s is not an active instance"%instname) return item
def getNamedInstance(self, instname): pathname = keywords.fixID(instname) with self.updateLock: itemlist = vtype.parseItemList(pathname) item = self.getSubValue(itemlist) if not isinstance(item, active_inst.ActiveInstance): raise ProjectError("%s is not an active instance" % instname) return item
def scheduleSet(self, itemname, literal, outf, sourceType=None, printName=None): """Add an instance of a set in the transaction schedule.""" itemname=keywords.fixID(itemname) with self.transactionStackLock: sv=self.transactionStack[-1].addSetValue(itemname, literal, sourceType, printName) if not self._tryImmediateTransaction(outf): sv.describe(outf)
def activate(self, pathname): """Activate all active instances.""" pathname = keywords.fixID(pathname) with self.updateLock: itemlist = vtype.parseItemList(pathname) item = self.getSubValue(itemlist) ret = dict() if isinstance(item, active_inst.ActiveInstance): item.activate() elif isinstance(item, Project): item.network.activateAll() else: raise ProjectError("%s is not an instance" % pathname)
def activate(self, pathname): """Activate all active instances.""" pathname=keywords.fixID(pathname) with self.updateLock: itemlist=vtype.parseItemList(pathname) item=self.getSubValue(itemlist) ret=dict() if isinstance(item, active_inst.ActiveInstance): item.activate() elif isinstance(item, Project): item.network.activateAll() else: raise ProjectError("%s is not an instance"%pathname)
def scheduleSet(self, itemname, literal, outf, sourceType=None, printName=None): """Add an instance of a set in the transaction schedule.""" itemname = keywords.fixID(itemname) with self.transactionStackLock: sv = self.transactionStack[-1].addSetValue(itemname, literal, sourceType, printName) if not self._tryImmediateTransaction(outf): sv.describe(outf)
def rerun(self, pathname, recursive, clearError, outf): """Re-run and optionally clear an error on an item.""" pathname = keywords.fixID(pathname) with self.updateLock: itemlist = vtype.parseItemList(pathname) item = self.getSubValue(itemlist) if isinstance(item, active_inst.ActiveInstance): ret = item.rerun(recursive, clearError, outf) if ret == 0: if clearError: outf.write("No errors cleared.") else: outf.write("No reruns performed.") else: raise ProjectError("%s is not an instance" % pathname)
def deactivate(self, pathname): """De-activate all active instances contained in pathname (or everything if pathname is empty).""" pathname = keywords.fixID(pathname) with self.updateLock: itemlist = vtype.parseItemList(pathname) item = self.getSubValue(itemlist) ret = dict() log.debug("%s" % str(item)) if isinstance(item, active_inst.ActiveInstance): item.deactivate() elif isinstance(item, Project): item.network.deactivateAll() else: raise ProjectError("%s is not an instance" % pathname)
def rerun(self, pathname, recursive, clearError, outf): """Re-run and optionally clear an error on an item.""" pathname=keywords.fixID(pathname) with self.updateLock: itemlist=vtype.parseItemList(pathname) item=self.getSubValue(itemlist) if isinstance(item, active_inst.ActiveInstance): ret=item.rerun(recursive, clearError, outf) if ret==0: if clearError: outf.write("No errors cleared.") else: outf.write("No reruns performed.") else: raise ProjectError("%s is not an instance"%pathname)
def deactivate(self, pathname): """De-activate all active instances contained in pathname (or everything if pathname is empty).""" pathname=keywords.fixID(pathname) with self.updateLock: itemlist=vtype.parseItemList(pathname) item=self.getSubValue(itemlist) ret=dict() log.debug("%s"%str(item)) if isinstance(item, active_inst.ActiveInstance): item.deactivate() elif isinstance(item, Project): item.network.deactivateAll() else: raise ProjectError("%s is not an instance"%pathname)
def getDebugInfo(self, itemname): """Give debug info about a particular item.""" global profile outf=StringIO() if itemname == "": outf.write("the item was empty") if profile: all_objects = muppy.get_objects() sum1 = summary.summarize(all_objects) summary.print_(sum1, 100) ib = refbrowser.InteractiveBrowser(self) ib.main() return outf.getvalue() itemname=keywords.fixID(itemname) itemlist=vtype.parseItemList(itemname) item=self.getSubValue(itemlist) item.writeDebug(outf) return outf.getvalue()
def getDebugInfo(self, itemname): """Give debug info about a particular item.""" global profile outf = StringIO() if itemname == "": outf.write("the item was empty") if profile: all_objects = muppy.get_objects() sum1 = summary.summarize(all_objects) summary.print_(sum1, 100) ib = refbrowser.InteractiveBrowser(self) ib.main() return outf.getvalue() itemname = keywords.fixID(itemname) itemlist = vtype.parseItemList(itemname) item = self.getSubValue(itemlist) item.writeDebug(outf) return outf.getvalue()
def getGraph(self, pathname): """Get an graph description based on a path name according to the rule [instance]:[instance].""" pathname=keywords.fixID(pathname) with self.updateLock: itemlist=vtype.parseItemList(pathname) item=self.getSubValue(itemlist) ret=dict() if item is not None: if isinstance(item, active_inst.ActiveInstance): net=item.network ret["name"]=pathname ret["instances"]=net.getActiveInstanceList(True, True) ret["connections"]=net.getConnectionList() elif isinstance(item, Project): net=item.network ret["name"]=pathname ret["instances"]=net.getActiveInstanceList(True, True) ret["connections"]=net.getConnectionList() else: ret["name"]=pathname ret["instances"]=[] ret["connections"]=[] return ret
def getGraph(self, pathname): """Get an graph description based on a path name according to the rule [instance]:[instance].""" pathname = keywords.fixID(pathname) with self.updateLock: itemlist = vtype.parseItemList(pathname) item = self.getSubValue(itemlist) ret = dict() if item is not None: if isinstance(item, active_inst.ActiveInstance): net = item.network ret["name"] = pathname ret["instances"] = net.getActiveInstanceList(True, True) ret["connections"] = net.getConnectionList() elif isinstance(item, Project): net = item.network ret["name"] = pathname ret["instances"] = net.getActiveInstanceList(True, True) ret["connections"] = net.getConnectionList() else: ret["name"] = pathname ret["instances"] = [] ret["connections"] = [] return ret
def startElement(self, name, attrs): #if name == "value" or name == "data": if name in vtype.basicTypes: basicType=vtype.basicTypes[name] if 'type' in attrs: tpnm=keywords.fixID(attrs.getValue('type')) if self.importList is not None: tp=self.importList.getTypeByFullName(tpnm, self.currentImport) else: tp=basicType else: tp=basicType # determine the numerical location in the subitem tree if self.depth == self.lastDepth: self.subCounters[self.depth] += 1 elif self.depth > self.lastDepth: if len(self.subCounters) < self.depth+1: self.subCounters.append(0) else: self.subCounters[self.depth] = 0 else: self.subCounters[self.depth] += 1 # now determine the sub-item context itemStackAdd=None # the item to add to the item stack at the end if len(self.subStack) > 0: subVal=self.subStack[-1] # subVal is the current value to read if 'field' in attrs: if (len(self.typeStack)>0 and self.typeStack[-1].isCompound()): # if it's a field, it's named. itemStackAdd=keywords.fixID(attrs.getValue('field')) createType=None if self.allowUnknownTypes: createType=tp subVal=subVal.getCreateSubValue([itemStackAdd], createType=createType, setCreateSourceTag=self.sourceTag) if subVal is None: raise ValXMLError("Did not find field '%s'"% attrs.getValue('field'), self) else: raise ValXMLError("field '%s' inside non-compound type"% attrs.getValue('field'), self) elif 'subitem' in attrs: # this is a single, directly addressed sub-item subitems=vtype.parseItemList( keywords.fixID(attrs.getValue('subitem'))) subVal=subVal.getCreateSubValue(subitems, setCreateSourceTag=self.sourceTag) elif ( len(self.typeStack) > 0 and (self.typeStack[-1] == vtype.arrayType) ): itemStackAdd=self.subCounters[self.depth] createType=None if self.allowUnknownTypes: createType=tp subVal=subVal.getCreateSubValue([itemStackAdd], createType=createType, setCreateSourceTag=self.sourceTag) else: # this is the top-level value self.value=self.valueType(None, tp) subVal=self.value if tp is not None: # check the type if not tp.isSubtype(subVal.getType()): raise ValXMLError("%s not a subtype of %s."% (tp.getName(), subVal.getType().getName()), self) else: tp=subVal.getType() if not subVal.getType().isCompound(): if 'value' in attrs: # this means that the type is a literal and can be parsed nval=interpretLiteral(attrs.getValue('value'), tp) subVal.copy(nval) #log.debug("Setting value for %s to %s"% # (subVal.getFullName(), subVal.value) ) #subVal._set(tp.valueFromLiteral(attrs.getValue('value'))) else: if 'value' in attrs: raise ValXMLError("Literal value for compound type", self) # increment depth if 'seqnr' in attrs: subVal.seqNr=int(attrs.getValue('seqnr')) if 'updated' in attrs: updated=cpc.util.getBooleanAttribute(attrs, "updated") if updated: subVal.markUpdated(updated) #log.debug("Setting value for %s"%subVal.getFullName()) subVal.sourceTag=self.sourceTag self.lastDepth=self.depth self.depth+=1 self.typeStack.append(basicType) #self.itemStack.append(itemStackAdd) self.subStack.append(subVal)
def getNamedItemList(self, pathname): """Get an list based on a path name according to the rule [instance]:[instance]""" pathname = keywords.fixID(pathname) with self.updateLock: itemlist = vtype.parseItemList(pathname) item = self.getSubValue(itemlist) ret = dict() if item is None: ret["type"] = "Not found: " ret["name"] = pathname elif isinstance(item, value.Value): # it is an active I/O item tp = item.getType() ret["type"] = "input/output value" ret["name"] = pathname if tp is not None: ret["typename"] = tp.getName() ret["value-type"] = tp.jsonDescribe() else: ret["typename"] = "Not found" if tp.isSubtype(vtype.recordType): ret["subitems"] = [] keys = tp.getMemberKeys() for key in keys: mem = tp.getRecordMember(key) subi = dict() subi["name"] = key subi["type"] = mem.type.getName() #subi["value-type"]=mem.type.jsonDescribe() optstr = "" conststr = "" if mem.isOptional(): subi["optional"] = 1 if mem.isConst(): subi["const"] = 1 if mem.isComplete(): subi["complete"] = 1 if mem.desc is not None: subi["desc"] = mem.desc.get() ret["subitems"].append(subi) elif (tp.isSubtype(vtype.arrayType) or tp.isSubtype(vtype.dictType)): mem = tp.getMembers() subi = {"type": mem.getName()} ret["subitems"] = [subi] elif isinstance(item, active_inst.ActiveInstance): ret["type"] = "instance" ret["name"] = item.getCanonicalName() ret["fn_name"] = item.function.getFullName() ret["inputs"] = item.getInputs().getSubValueList() ret["outputs"] = item.getOutputs().getSubValueList() net = item.getNet() if net is not None: ret["instances"] = net.getActiveInstanceList(False, False) ret["state"] = item.getPropagatedStateStr() cputime = int(item.getCputime()) if cputime > 0: ret["cputime"] = str(cputime) cputime = int(item.getCumulativeCputime()) if cputime > 0: ret["cumulative-cputime"] = str(cputime) elif isinstance(item, Project): ret["type"] = "network" ret["name"] = pathname ret["instances"] = item.network.getActiveInstanceList( False, False) cputime = int(item.network.getCumulativeCputime()) if cputime > 0: ret["cumulative-cputime"] = str(cputime) else: ret["type"] = "Unknown type of item: " ret["name"] = pathname return ret
def getNamedDescription(self, pathname): """Get a description of a named function/type/lib""" pathname = keywords.fixID(pathname) with self.updateLock: ret = dict() item = self.imports.getItemByFullName(pathname) if item is not None: ret["name"] = pathname desc = item.getDescription() if desc is not None: ret["desc"] = desc.get() else: ret["desc"] = "" if isinstance(item, lib.ImportLibrary): ret["type"] = "library" rfuncs = [] funcs = item.getFunctionList() for f in funcs: nf = {"name": f} desc = item.getFunction(f).getDescription() if desc is not None: nf["desc"] = desc.get() else: nf["desc"] = "" rfuncs.append(nf) ret["functions"] = rfuncs rtypes = [] types = item.getTypeList() for t in types: if not item.getType(t).isImplicit(): nf = {"name": t} desc = item.getType(t).getDescription() if desc is not None: nf["desc"] = desc.get() else: nf["desc"] = "" rtypes.append(nf) if len(rtypes) > 0: ret["types"] = rtypes elif isinstance(item, function.Function): ret["type"] = "function" ioitems = item.getInputs() inps = [] for key in ioitems.getMemberKeys(): retd = dict() retd["name"] = key retd["type"] = ioitems.getMember(key).getName() desc = ioitems.getRecordMember(key).getDescription() if desc is not None: retd["desc"] = desc.get() else: retd["desc"] = "" inps.append(retd) ret["inputs"] = inps ioitems = item.getOutputs() outs = [] for key in ioitems.getMemberKeys(): retd = dict() retd["name"] = key retd["type"] = ioitems.getMember(key).getName() desc = ioitems.getRecordMember(key).getDescription() if desc is not None: retd["desc"] = desc.get() else: retd["desc"] = "" outs.append(retd) ret["outputs"] = outs elif isinstance(item, vtype.Type): ret["type"] = "type" else: ret["name"] = "Not found: %s" % pathname ret["desc"] = "" return ret
def getNamedDescription(self, pathname): """Get a description of a named function/type/lib""" pathname=keywords.fixID(pathname) with self.updateLock: ret=dict() item=self.imports.getItemByFullName(pathname) if item is not None: ret["name"]=pathname desc=item.getDescription() if desc is not None: ret["desc"]=desc.get() else: ret["desc"]="" if isinstance(item, lib.ImportLibrary): ret["type"]="library" rfuncs=[] funcs=item.getFunctionList() for f in funcs: nf={ "name" : f } desc=item.getFunction(f).getDescription() if desc is not None: nf["desc"] = desc.get() else: nf["desc"] = "" rfuncs.append(nf) ret["functions"]=rfuncs rtypes=[] types=item.getTypeList() for t in types: if not item.getType(t).isImplicit(): nf={ "name" : t } desc=item.getType(t).getDescription() if desc is not None: nf["desc"] = desc.get() else: nf["desc"] = "" rtypes.append(nf) if len(rtypes)>0: ret["types"]=rtypes elif isinstance(item, function.Function): ret["type"]="function" ioitems=item.getInputs() inps=[] for key in ioitems.getMemberKeys(): retd=dict() retd["name"]=key retd["type"]=ioitems.getMember(key).getName() desc=ioitems.getRecordMember(key).getDescription() if desc is not None: retd["desc"]=desc.get() else: retd["desc"]="" inps.append(retd) ret["inputs"]=inps ioitems=item.getOutputs() outs=[] for key in ioitems.getMemberKeys(): retd=dict() retd["name"]=key retd["type"]=ioitems.getMember(key).getName() desc=ioitems.getRecordMember(key).getDescription() if desc is not None: retd["desc"]=desc.get() else: retd["desc"]="" outs.append(retd) ret["outputs"]=outs elif isinstance(item, vtype.Type): ret["type"]="type" else: ret["name"]="Not found: %s"%pathname ret["desc"]="" return ret
def getNamedItemList(self, pathname): """Get an list based on a path name according to the rule [instance]:[instance]""" pathname=keywords.fixID(pathname) with self.updateLock: itemlist=vtype.parseItemList(pathname) item=self.getSubValue(itemlist) ret=dict() if item is None: ret["type"]="Not found: " ret["name"]=pathname elif isinstance(item, value.Value): # it is an active I/O item tp=item.getType() ret["type"]="input/output value" ret["name"]=pathname if tp is not None: ret["typename"]=tp.getName() ret["value-type"]=tp.jsonDescribe() else: ret["typename"]="Not found" if tp.isSubtype(vtype.recordType): ret["subitems"]=[] keys=tp.getMemberKeys() for key in keys: mem=tp.getRecordMember(key) subi=dict() subi["name"]=key subi["type"]=mem.type.getName() #subi["value-type"]=mem.type.jsonDescribe() optstr="" conststr="" if mem.isOptional(): subi["optional"]=1 if mem.isConst(): subi["const"]=1 if mem.isComplete(): subi["complete"]=1 if mem.desc is not None: subi["desc"]=mem.desc.get() ret["subitems"].append( subi ) elif (tp.isSubtype(vtype.arrayType) or tp.isSubtype(vtype.dictType)): mem=tp.getMembers() subi={"type" : mem.getName()} ret["subitems"]=[ subi ] elif isinstance(item, active_inst.ActiveInstance): ret["type"]="instance" ret["name"]=item.getCanonicalName() ret["fn_name"]=item.function.getFullName() ret["inputs" ]=item.getInputs().getSubValueList() ret["outputs" ]=item.getOutputs().getSubValueList() net=item.getNet() if net is not None: ret["instances" ]=net.getActiveInstanceList(False, False) ret["state"]=item.getPropagatedStateStr() cputime=int(item.getCputime()) if cputime > 0: ret["cputime" ]=str(cputime) cputime=int(item.getCumulativeCputime()) if cputime > 0: ret["cumulative-cputime" ]=str(cputime) elif isinstance(item, Project): ret["type"]="network" ret["name"]=pathname ret["instances"]=item.network.getActiveInstanceList( False, False) cputime=int(item.network.getCumulativeCputime()) if cputime > 0: ret["cumulative-cputime" ]=str(cputime) else: ret["type"]="Unknown type of item: " ret["name"]=pathname return ret
def startElement(self, name, attrs): # first handle all the sub-readers if self.cmdReader is not None: self.cmdReader.startElement(name, attrs) elif self.fnInputReader is not None: self.fnInputReader.startElement(name, attrs) elif self.valueReader is not None: self.valueReader.startElement(name, attrs) elif self.descReader is not None: self.descReader.startElement(name, attrs) # and then actual elements elif name == "cpc": # top-level element if attrs.has_key('version'): self.fileVersion = int(attrs.getValue("version")) else: self.fileVersion = 0 if self.fileVersion > curVersion: raise ProjectXMLError("Can't read file from the future.") elif name == "import": if not attrs.has_key('name'): raise ProjectXMLError("import has no name", self) name = keywords.fixID(attrs.getValue('name')) nimport = self.importList.get(name) if nimport is None: # we don't have it yet. Read it. nimport = self.project.importName(name) # and try again nimport = self.importList.get(name) if nimport is None: raise ProjectXMLError("Failed to import %s" % name, self) self.localImports[name] = nimport elif name == "function": if self.function is not None: raise ProjectXMLError( "function-in-function definitions not supported", self) if not attrs.has_key("type"): raise ProjectXMLError("function has no type", self) if not attrs.has_key("id"): raise ProjectXMLError("function has no id", self) fntype = attrs.getValue("type") id = keywords.fixID(attrs.getValue("id")) if fntype == "python": tsk = atomic.SimpleFunctionFunction(id, lib=self.thisImport) elif fntype == "python-extended": tsk = atomic.ExtendedFunctionFunction(id, lib=self.thisImport) elif fntype == "network": tsk = network_function.NetworkFunction(id, lib=self.thisImport) elif fntype == "external": if self.dirName is None: raise ProjectXMLError( "external function without directory", self) tsk = external.ExternalFunction(id, basedir=self.dirName, lib=self.thisImport) else: raise ProjectXMLError( "function type '%s' not recognized" % (fntype), self) self.function = tsk self.functionType = fntype elif name == "type": if self.function is not None: raise ProjectXMLError( "type-in-function definitions not supported", self) if self.type is not None: raise ProjectXMLError("type-in-type definitions not supported", self) if not attrs.has_key("id"): raise ProjectXMLError("type has no id", self) if not attrs.has_key("base"): raise ProjectXMLError("type has no base", self) name = keywords.fixID(attrs.getValue("id")) basetype = self.importList.getTypeByFullName( attrs.getValue("base"), self.thisImport) self.type = basetype.inherit(name, self.thisImport) self.typeStack.append((self.type, None)) if basetype.isSubtype(vtype.arrayType): if attrs.has_key("member-type"): tnm = keywords.fixID(attrs.getValue("member-type")) members = self.importList.getTypeByFullName( tnm, self.thisImport) self.type.setMembers(members) log.debug("new array(%s) type %s" % (members.name, name)) #else: # raise ProjectXMLError("Array type %s has no member type"% # name, self) elif basetype.isSubtype(vtype.dictType): if attrs.has_key("member-type"): tnm = keywords.fixID(attrs.getValue("member-type")) members = self.importList.getTypeByFullName( tnm, self.thisImport) self.type.setMembers(members) log.debug("new dict(%s) type %s" % (members.name, name)) elif basetype.isSubtype(vtype.fileType): if attrs.has_key("extension"): self.type.setExtension(attrs.getValue("extension")) if attrs.has_key("mime-type"): self.type.setExtension(attrs.getValue("mime-type")) elif name == "inputs": if self.type is not None: raise ProjectXMLError("nested inputs", self) self.ioitem = "inputs" if self.instance is not None: self.type = self.instance.getInputs() self.typeStack.append((self.type, None)) elif self.function is not None: self.type = self.function.getInputs() self.typeStack.append((self.type, None)) elif self.activeInst is not None: curValue = self.activeInst.getStagedInputs() self.affectedInputAIs.add(self.activeInst) self.setValueReader( value.ValueReader(self.filename, curValue, importList=self.importList, currentImport=self.thisImport, sourceTag=self), name) else: raise ProjectXMLError("inputs without function/instance", self) elif name == "outputs": if self.type is not None: raise ProjectXMLError("nested outputs", self) self.ioitem = "outputs" if self.instance is not None: self.type = self.instance.getOutputs() self.typeStack.append((self.type, None)) elif self.function is not None: self.type = self.function.getOutputs() self.typeStack.append((self.type, None)) elif self.activeInst is not None: curValue = self.activeInst.getOutputs() self.affectedOutputAIs.add(self.activeInst) self.setValueReader( value.ValueReader(self.filename, curValue, importList=self.importList, currentImport=self.thisImport, sourceTag=self), name) else: raise ProjectXMLError("outputs without function/instance", self) elif name == "subnet-inputs": if self.type is not None: raise ProjectXMLError("nested subnet-inputs", self) self.ioitem = "subnet-inputs" if self.instance is not None: self.type = self.instance.getSubnetInputs() self.typeStack.append((self.type, None)) elif self.function is not None: self.type = self.function.getSubnetInputs() self.typeStack.append((self.type, None)) elif self.activeInst is not None: curValue = self.activeInst.getStagedSubnetInputs() self.affectedInputAIs.add(self.activeInst) self.setValueReader( value.ValueReader(self.filename, curValue, importList=self.importList, currentImport=self.thisImport, sourceTag=self), name) else: raise ProjectXMLError( "subnet-inputs without function/instance", self) elif name == "subnet-outputs": if self.type is not None: raise ProjectXMLError("nested subnet-outputs", self) self.ioitem = "subnet-outputs" if self.instance is not None: self.type = self.instance.getSubnetOutputs() self.typeStack.append((self.type, None)) elif self.function is not None: self.type = self.function.getSubnetOutputs() self.typeStack.append((self.type, None)) elif self.activeInst is not None: curValue = self.activeInst.getSubnetOutputs() self.affectedOutputAIs.add(self.activeInst) self.setValueReader( value.ValueReader(self.filename, curValue, importList=self.importList, currentImport=self.thisImport, sourceTag=self), name) else: raise ProjectXMLError( "subnet-outputs without function/instance", self) elif name == "field": if self.type is None and self.ioitem is None: raise ProjectXMLError("Field without type context", self) if not attrs.has_key("type"): raise ProjectXMLError("No type in field", self) tpnm = keywords.fixID(attrs.getValue("type")) if self.type is not None and self.type.isCompound(): tp = self.importList.getTypeByFullName(tpnm, self.thisImport) nm = None if self.type.isSubtype(vtype.arrayType): self.type.setMembers(tp) elif self.type.isSubtype(vtype.dictType): self.type.setMembers(tp) elif self.type.isSubtype(vtype.recordType): if not attrs.has_key("id"): raise ProjectXMLError("No id in list field", self) const = cpc.util.getBooleanAttribute(attrs, "const") opt = cpc.util.getBooleanAttribute(attrs, "opt") complete = cpc.util.getBooleanAttribute(attrs, "complete") nm = keywords.fixID(attrs.getValue("id")) self.type.addMember(nm, tp, opt, const, complete) # add it to the stack self.type = tp self.typeStack.append((tp, nm)) else: raise ProjectXMLError( "Non-compound type %s can't have fields" % self.type.getName(), self) elif name == "network": #if len(self.networkStack) < 1: # raise ProjectXMLError("network in network definition", self) if self.function is None: # there is no function, check whether we're in an active # network: if len(self.activeInstStack) < 1: # we're not. Get the top level if len(self.networkStack) > 0: raise ProjectXMLError("network in network definition", self) self.networkStack.append(self.thisImport.getNetwork()) else: self.networkStack.append(self.activeInst.getNet()) else: # this is a function network if len(self.networkStack) > 0: raise ProjectXMLError("network in network definition", self) self.networkStack.append(self.function.getSubnet()) self.network = self.networkStack[-1] elif name == "instance": if self.network is None: raise ProjectXMLError("instance without network", self) if not attrs.has_key("id"): raise ProjectXMLError("instance has no id", self) if not attrs.has_key("function"): raise ProjectXMLError("instance has no function", self) id = keywords.fixID(attrs.getValue("id")) fn = keywords.fixID(attrs.getValue('function')) func = self.importList.getFunctionByFullName(fn, self.thisImport) self.instance = instance.Instance(id, func, fn, self.thisImport) elif name == "assign": if not 'value' in attrs: raise ProjectXMLError("assign has no value", self) if not attrs.has_key('type'): raise ProjectXMLError("assign has no type", self) if not attrs.has_key('dest'): raise ProjectXMLError("assign has no destination", self) valueString = attrs.getValue('value') typestr = keywords.fixID(attrs.getValue('type')) dst = keywords.fixID(attrs.getValue('dest')) # get the type object tp = self.importList.getTypeByFullName(typestr, self.thisImport) # get the value from the type object val = active_value.ActiveValue( value.interpretLiteral(valueString, tp), tp) val.setUpdated(True) #log.debug("value is %s, %s"%(str(val), valueString)) # get the destination dstInstName, dstDir, dstItemName = (connection.splitIOName( dst, keywords.In)) cn = connection.makeInitialValue(self.network, dstInstName, dstDir, dstItemName, val) self.network.findConnectionSrcDest(cn, self.affectedInputAIs, self.affectedOutputAIs) self.network.addConnection(cn, self) elif name == "connection": if self.network is None: raise ProjectXMLError("connection without network", self) if not attrs.has_key('src') and not attrs.has_key('value'): raise ProjectXMLError("connection has no source", self) if not attrs.has_key('dest'): raise ProjectXMLError("connection has no destination", self) dst = keywords.fixID(attrs.getValue('dest')) dstInstName, dstDir, dstItemName = (connection.splitIOName( dst, None)) if attrs.has_key('src'): src = keywords.fixID(attrs.getValue('src')) # now check the source srcInstName, srcDir, srcItemName = (connection.splitIOName( src, None)) cn = connection.makeConnection(self.network, srcInstName, srcDir, srcItemName, dstInstName, dstDir, dstItemName) else: if not attrs.has_key("type"): raise ProjectXMLError("connection has no type", self) typestr = keywords.fixID(attrs.getValue('type')) valueString = attrs.getValue('value') tp = self.importList.getTypeByFullName(typestr, self.thisImport) # get the value from the type object val = value.interpretLiteral(valueString, tp) cn = connection.makeInitialValue(self.network, dstInstName, dstDir, dstItemName, val) self.network.findConnectionSrcDest(cn, self.affectedInputAIs, self.affectedOutputAIs) self.network.addConnection(cn, self) elif name == "controller": # generic items if cpc.util.getBooleanAttribute(attrs, "persistent_dir"): # a persistent scratch dir is needed log.debug("Setting persistent dir for %s" % self.function.getName()) self.function.setPersistentDir(True) if cpc.util.getBooleanAttribute(attrs, "output_dir"): log.debug("Setting output dir always on for %s" % self.function.getName()) # a run dir is needed even if there's no file output self.function.setOutputDirWithoutFiles(True) if cpc.util.getBooleanAttribute(attrs, "log"): log.debug("Turning on logging for %s" % (self.function.getName())) self.function.setLog(True) if cpc.util.getBooleanAttribute(attrs, "access_outputs"): log.debug("Controller uses current outputs for %s" % (self.function.getName())) self.function.setAccessOutputs(True) if cpc.util.getBooleanAttribute(attrs, "access_subnet_outputs"): log.debug("Controller uses current subnet outputs for %s" % (self.function.getName())) self.function.setAccessSubnetOutputs(True) # type-specific items if (self.functionType == "python" or self.functionType == "python-extended"): importName = None if attrs.has_key("import"): importName = keywords.fixID(attrs.getValue('import')) if not attrs.has_key('function'): raise ProjectXMLError("python controller has no function", self) fnName = keywords.fixID(attrs.getValue("function")) self.function.setFunction(fnName, importName) elif self.functionType == "command": pass elif self.functionType == "external": if not attrs.has_key('executable'): raise ProjectXMLError( "command controller has no executable", self) executable = attrs.getValue("executable") self.function.setExecutable(executable) self.inController = True elif name == "stdin": if self.inController and self.functionType == "command": if not attrs.has_key('value'): raise ProjectXMLError("stdin has no value", self) self.function.setStdin(attrs.getValue('value')) else: raise ProjectXMLError("stdin tag, without command controller", self) elif name == "arg": if self.inController and self.functionType == "command": pass else: raise ProjectXMLError("arg tag, without command controller", self) elif name == "active": # read in a description of an active network + active instances if not isinstance(self.network, active_network.ActiveNetwork): raise ProjectXMLError("active instance without active network", self) if not attrs.has_key("id"): raise ProjectXMLError("active instance has no id", self) if not attrs.has_key("state"): raise ProjectXMLError("active instance has no state", self) if not attrs.has_key("seqnr"): raise ProjectXMLError("active instance has no seqnr", self) name = keywords.fixID(attrs.getValue("id")) stStr = attrs.getValue("state") # now get the actual state state = None for st in active_inst.ActiveInstance.states: if st.str == stStr: state = st if state is None: raise ProjectXMLError( "active instance state %s invalid" % stStr, self) seqnr = int(attrs.getValue("seqnr")) ai = self.network.getActiveInstance(name) ai.setState(state) ai.setSeqNr(seqnr) if attrs.has_key('errmsg'): ai.markError( xml.sax.saxutils.unescape(attrs.getValue('errmsg')), False) if attrs.has_key('warnmsg'): ai.setWarning( xml.sax.saxutils.unescape(attrs.getValue('warnmsg'))) if attrs.has_key('cputime'): try: ai.setCputime(float(attrs.getValue('cputime'))) except ValueError: pass self.activeInstStack.append(ai) self.activeInst = ai self.affectedInputAIs.add(ai) self.affectedOutputAIs.add(ai) elif name == "active-connection": # any value associated with active connection points if self.activeInst is None: raise ProjectXMLError( "active connection without active instance", self) if not attrs.has_key("id"): raise ProjectXMLError("active conn field has no id", self) if not attrs.has_key("type"): raise ProjectXMLError("active conn field has no type", self) tpnm = keywords.fixID(attrs.getValue("type")) tp = self.importList.getTypeByFullName(tpnm, self.thisImport) if attrs.has_key("seqnr"): seqnr = int(attrs.getValue("seqnr")) else: seqnr = 0 name = keywords.fixID(attrs.getValue("id")) # TODO fix this for new type struct. val = None if attrs.has_key("value"): if not attrs.has_key("value_type"): raise ProjectXMLError( "active connection value without value_type", self) vtpnm = keywords.fixID(attrs.getValue("value_type")) vtp = self.importList.getTypeByFullName(vtpnm, self.thisImport) valnm = attrs.getValue("value") valueString = attrs.getValue('value') val = value.interpretLiteral(valueString, tp) if self.ioitem == "inputs": self.activeInst.setInput(name, tp, val, seqnr) elif self.ioitem == "outputs": self.activeInst.setOutput(name, tp, val, seqnr) elif self.ioitem == "subnet-inputs": self.activeInst.setSubnetInput(name, tp, val, seqnr) elif self.ioitem == "subnet-outputs": self.activeInst.setSubnetOutput(name, tp, val, seqnr) else: raise ProjectXMLError( "unknown active connection ioitem '%s'" % (self.ioitem), self) elif name == "tasks": pass # just ignore it; we deal with tasks when they come elif name == "task": if self.curTask is not None: raise ProjectXMLError("task within task", self) if self.activeInst is None: raise ProjectXMLError("task without active instance", self) if not attrs.has_key("seqnr"): raise ProjectXMLError("task has no seqnr", self) if not attrs.has_key("priority"): raise ProjectXMLError("task has no priority", self) priority = int(attrs.getValue("priority")) seqnr = int(attrs.getValue("seqnr")) self.curTask = task.Task(self.project, self.activeInst, self.activeInst.getFunction(), None, priority, seqnr) elif name == "function-input": if self.curTask is None: raise ProjectXMLError("function-input without task", self) inp = run.FunctionRunInput() self.setFnInputReader(run.IOReader(inp, None), name) self.fnInputReader.setReportFilename(self.filename) self.fnInputReader.startElement(name, attrs) elif name == "command-list": if self.curTask is None: raise ProjectXMLError("commands without task", self) self.cmdReader = cpc.command.CommandReader() elif name == "desc": # A description. First find out what it describes. if self.type is not None: if (len(self.typeStack) > 1 and self.typeStack[-2][0].isSubtype(vtype.recordType)): # if it is a field, describe it as a field tp = self.typeStack[-2][0] field = self.typeStack[-1][1] lstm = tp.getRecordMember(field) self.setDescReader( description.DescriptionReader(lstm, self.filename), name) elif not self.type.isBuiltin() and len(self.typeStack) == 1: # it is a custom, describable type self.setDescReader( description.DescriptionReader(self.type, self.filename), name) else: raise ProjectXMLError("description of a builtin type.", self) elif self.function is not None: self.setDescReader( description.DescriptionReader(self.function, self.filename), name) elif self.curTask is None and self.network is None: self.setDescReader( description.DescriptionReader(self.thisImport, self.filename), name) # raise ProjectXMLError("Unknown item to describe.") else: raise ProjectXMLError("Unknown tag %s" % name, self)
def startElement(self, name, attrs): # first handle all the sub-readers if self.cmdReader is not None: self.cmdReader.startElement(name, attrs) elif self.fnInputReader is not None: self.fnInputReader.startElement(name, attrs) elif self.valueReader is not None: self.valueReader.startElement(name, attrs) elif self.descReader is not None: self.descReader.startElement(name, attrs) # and then actual elements elif name == "cpc": # top-level element if attrs.has_key('version'): self.fileVersion=int(attrs.getValue("version")) else: self.fileVersion=0 if self.fileVersion > curVersion: raise ProjectXMLError("Can't read file from the future.") elif name == "import": if not attrs.has_key('name'): raise ProjectXMLError("import has no name", self) name=keywords.fixID(attrs.getValue('name')) nimport=self.importList.get(name) if nimport is None: # we don't have it yet. Read it. nimport=self.project.importName(name) # and try again nimport=self.importList.get(name) if nimport is None: raise ProjectXMLError("Failed to import %s"%name, self) self.localImports[name] = nimport elif name == "function": if self.function is not None: raise ProjectXMLError( "function-in-function definitions not supported", self) if not attrs.has_key("type"): raise ProjectXMLError("function has no type", self) if not attrs.has_key("id"): raise ProjectXMLError("function has no id", self) fntype=attrs.getValue("type") id=keywords.fixID(attrs.getValue("id")) if fntype == "python": tsk=atomic.SimpleFunctionFunction(id, lib=self.thisImport) elif fntype == "python-extended": tsk=atomic.ExtendedFunctionFunction(id, lib=self.thisImport) elif fntype == "network": tsk=network_function.NetworkFunction(id, lib=self.thisImport) elif fntype == "external": if self.dirName is None: raise ProjectXMLError( "external function without directory", self) tsk=external.ExternalFunction(id, basedir=self.dirName, lib=self.thisImport) else: raise ProjectXMLError("function type '%s' not recognized"% (fntype), self) self.function=tsk self.functionType=fntype elif name == "type": if self.function is not None: raise ProjectXMLError( "type-in-function definitions not supported", self) if self.type is not None: raise ProjectXMLError( "type-in-type definitions not supported", self) if not attrs.has_key("id"): raise ProjectXMLError("type has no id", self) if not attrs.has_key("base"): raise ProjectXMLError("type has no base", self) name=keywords.fixID(attrs.getValue("id")) basetype=self.importList.getTypeByFullName(attrs.getValue("base"), self.thisImport) self.type=basetype.inherit(name, self.thisImport) self.typeStack.append( (self.type, None) ) if basetype.isSubtype(vtype.arrayType): if attrs.has_key("member-type"): tnm=keywords.fixID(attrs.getValue("member-type")) members=self.importList.getTypeByFullName(tnm, self.thisImport) self.type.setMembers(members) log.debug("new array(%s) type %s"%(members.name, name)) #else: # raise ProjectXMLError("Array type %s has no member type"% # name, self) elif basetype.isSubtype(vtype.dictType): if attrs.has_key("member-type"): tnm=keywords.fixID(attrs.getValue("member-type")) members=self.importList.getTypeByFullName(tnm, self.thisImport) self.type.setMembers(members) log.debug("new dict(%s) type %s"%(members.name, name)) elif basetype.isSubtype(vtype.fileType): if attrs.has_key("extension"): self.type.setExtension(attrs.getValue("extension")) if attrs.has_key("mime-type"): self.type.setExtension(attrs.getValue("mime-type")) elif name == "inputs": if self.type is not None: raise ProjectXMLError("nested inputs", self) self.ioitem="inputs" if self.instance is not None: self.type=self.instance.getInputs() self.typeStack.append( (self.type, None) ) elif self.function is not None: self.type=self.function.getInputs() self.typeStack.append( (self.type, None) ) elif self.activeInst is not None: curValue=self.activeInst.getStagedInputs() self.affectedInputAIs.add(self.activeInst) self.setValueReader(value.ValueReader(self.filename, curValue, importList=self.importList, currentImport=self.thisImport, sourceTag=self), name) else: raise ProjectXMLError("inputs without function/instance", self) elif name == "outputs": if self.type is not None: raise ProjectXMLError("nested outputs", self) self.ioitem="outputs" if self.instance is not None: self.type=self.instance.getOutputs() self.typeStack.append( (self.type, None) ) elif self.function is not None: self.type=self.function.getOutputs() self.typeStack.append( (self.type, None) ) elif self.activeInst is not None: curValue=self.activeInst.getOutputs() self.affectedOutputAIs.add(self.activeInst) self.setValueReader(value.ValueReader(self.filename, curValue, importList=self.importList, currentImport=self.thisImport, sourceTag=self), name) else: raise ProjectXMLError("outputs without function/instance", self) elif name == "subnet-inputs": if self.type is not None: raise ProjectXMLError("nested subnet-inputs", self) self.ioitem="subnet-inputs" if self.instance is not None: self.type=self.instance.getSubnetInputs() self.typeStack.append( (self.type, None) ) elif self.function is not None: self.type=self.function.getSubnetInputs() self.typeStack.append( (self.type, None) ) elif self.activeInst is not None: curValue=self.activeInst.getStagedSubnetInputs() self.affectedInputAIs.add(self.activeInst) self.setValueReader(value.ValueReader(self.filename, curValue, importList=self.importList, currentImport=self.thisImport, sourceTag=self), name) else: raise ProjectXMLError( "subnet-inputs without function/instance", self) elif name == "subnet-outputs": if self.type is not None: raise ProjectXMLError("nested subnet-outputs", self) self.ioitem="subnet-outputs" if self.instance is not None: self.type=self.instance.getSubnetOutputs() self.typeStack.append((self.type, None)) elif self.function is not None: self.type=self.function.getSubnetOutputs() self.typeStack.append((self.type, None)) elif self.activeInst is not None: curValue=self.activeInst.getSubnetOutputs() self.affectedOutputAIs.add(self.activeInst) self.setValueReader(value.ValueReader(self.filename, curValue, importList=self.importList, currentImport=self.thisImport, sourceTag=self), name) else: raise ProjectXMLError( "subnet-outputs without function/instance", self) elif name == "field": if self.type is None and self.ioitem is None: raise ProjectXMLError("Field without type context", self) if not attrs.has_key("type"): raise ProjectXMLError("No type in field", self) tpnm=keywords.fixID(attrs.getValue("type")) if self.type is not None and self.type.isCompound(): tp=self.importList.getTypeByFullName(tpnm, self.thisImport) nm=None if self.type.isSubtype(vtype.arrayType): self.type.setMembers(tp) elif self.type.isSubtype(vtype.dictType): self.type.setMembers(tp) elif self.type.isSubtype(vtype.recordType): if not attrs.has_key("id"): raise ProjectXMLError("No id in list field", self) const=cpc.util.getBooleanAttribute(attrs,"const") opt=cpc.util.getBooleanAttribute(attrs,"opt") complete=cpc.util.getBooleanAttribute(attrs, "complete") nm=keywords.fixID(attrs.getValue("id")) self.type.addMember(nm, tp, opt, const, complete) # add it to the stack self.type=tp self.typeStack.append((tp, nm)) else: raise ProjectXMLError("Non-compound type %s can't have fields"% self.type.getName(), self) elif name == "network": #if len(self.networkStack) < 1: # raise ProjectXMLError("network in network definition", self) if self.function is None: # there is no function, check whether we're in an active # network: if len(self.activeInstStack) < 1: # we're not. Get the top level if len(self.networkStack)>0: raise ProjectXMLError("network in network definition", self) self.networkStack.append(self.thisImport.getNetwork()) else: self.networkStack.append(self.activeInst.getNet()) else: # this is a function network if len(self.networkStack)>0: raise ProjectXMLError("network in network definition", self) self.networkStack.append(self.function.getSubnet()) self.network=self.networkStack[-1] elif name == "instance": if self.network is None: raise ProjectXMLError("instance without network", self) if not attrs.has_key("id"): raise ProjectXMLError("instance has no id", self) if not attrs.has_key("function"): raise ProjectXMLError("instance has no function", self) id=keywords.fixID(attrs.getValue("id")) fn=keywords.fixID(attrs.getValue('function')) func=self.importList.getFunctionByFullName(fn, self.thisImport) self.instance=instance.Instance(id, func, fn, self.thisImport) elif name == "assign": if not 'value' in attrs: raise ProjectXMLError("assign has no value", self) if not attrs.has_key('type'): raise ProjectXMLError("assign has no type", self) if not attrs.has_key('dest'): raise ProjectXMLError("assign has no destination", self) valueString=attrs.getValue('value') typestr=keywords.fixID(attrs.getValue('type')) dst=keywords.fixID(attrs.getValue('dest')) # get the type object tp=self.importList.getTypeByFullName(typestr, self.thisImport) # get the value from the type object val=active_value.ActiveValue(value.interpretLiteral(valueString,tp), tp) val.setUpdated(True) #log.debug("value is %s, %s"%(str(val), valueString)) # get the destination dstInstName,dstDir,dstItemName=(connection.splitIOName(dst, keywords.In)) cn=connection.makeInitialValue(self.network, dstInstName, dstDir, dstItemName, val) self.network.findConnectionSrcDest(cn, self.affectedInputAIs, self.affectedOutputAIs) self.network.addConnection(cn, self) elif name == "connection": if self.network is None: raise ProjectXMLError("connection without network", self) if not attrs.has_key('src') and not attrs.has_key('value'): raise ProjectXMLError("connection has no source", self) if not attrs.has_key('dest'): raise ProjectXMLError("connection has no destination", self) dst=keywords.fixID(attrs.getValue('dest')) dstInstName,dstDir,dstItemName=(connection.splitIOName(dst, None)) if attrs.has_key('src'): src=keywords.fixID(attrs.getValue('src')) # now check the source srcInstName,srcDir,srcItemName=(connection.splitIOName(src, None)) cn=connection.makeConnection(self.network, srcInstName, srcDir, srcItemName, dstInstName, dstDir, dstItemName) else: if not attrs.has_key("type"): raise ProjectXMLError("connection has no type", self) typestr=keywords.fixID(attrs.getValue('type')) valueString=attrs.getValue('value') tp=self.importList.getTypeByFullName(typestr, self.thisImport) # get the value from the type object val=value.interpretLiteral(valueString, tp) cn=connection.makeInitialValue(self.network, dstInstName, dstDir, dstItemName, val) self.network.findConnectionSrcDest(cn, self.affectedInputAIs, self.affectedOutputAIs) self.network.addConnection(cn, self) elif name=="controller": # generic items if cpc.util.getBooleanAttribute(attrs,"persistent_dir"): # a persistent scratch dir is needed log.debug("Setting persistent dir for %s"% self.function.getName()) self.function.setPersistentDir(True) if cpc.util.getBooleanAttribute(attrs,"output_dir"): log.debug("Setting output dir always on for %s"% self.function.getName()) # a run dir is needed even if there's no file output self.function.setOutputDirWithoutFiles(True) if cpc.util.getBooleanAttribute(attrs, "log"): log.debug("Turning on logging for %s"%(self.function.getName())) self.function.setLog(True) if cpc.util.getBooleanAttribute(attrs, "access_outputs"): log.debug("Controller uses current outputs for %s"% (self.function.getName())) self.function.setAccessOutputs(True) if cpc.util.getBooleanAttribute(attrs, "access_subnet_outputs"): log.debug("Controller uses current subnet outputs for %s"% (self.function.getName())) self.function.setAccessSubnetOutputs(True) # type-specific items if (self.functionType == "python" or self.functionType == "python-extended"): importName=None if attrs.has_key("import"): importName=keywords.fixID(attrs.getValue('import')) if not attrs.has_key('function'): raise ProjectXMLError("python controller has no function", self) fnName=keywords.fixID(attrs.getValue("function")) self.function.setFunction(fnName, importName) elif self.functionType == "command": pass elif self.functionType == "external": if not attrs.has_key('executable'): raise ProjectXMLError( "command controller has no executable", self) executable=attrs.getValue("executable") self.function.setExecutable(executable) self.inController=True elif name=="stdin": if self.inController and self.functionType == "command": if not attrs.has_key('value'): raise ProjectXMLError("stdin has no value", self) self.function.setStdin(attrs.getValue('value')) else: raise ProjectXMLError("stdin tag, without command controller", self) elif name=="arg": if self.inController and self.functionType == "command": pass else: raise ProjectXMLError("arg tag, without command controller", self) elif name == "active": # read in a description of an active network + active instances if not isinstance(self.network, active_network.ActiveNetwork): raise ProjectXMLError("active instance without active network", self) if not attrs.has_key("id"): raise ProjectXMLError("active instance has no id", self) if not attrs.has_key("state"): raise ProjectXMLError("active instance has no state", self) if not attrs.has_key("seqnr"): raise ProjectXMLError("active instance has no seqnr", self) name=keywords.fixID(attrs.getValue("id")) stStr=attrs.getValue("state") # now get the actual state state=None for st in active_inst.ActiveInstance.states: if st.str == stStr: state=st if state is None: raise ProjectXMLError("active instance state %s invalid"%stStr, self) seqnr=int(attrs.getValue("seqnr")) ai=self.network.getActiveInstance(name) ai.setState(state) ai.setSeqNr(seqnr) if attrs.has_key('errmsg'): ai.markError(xml.sax.saxutils.unescape(attrs. getValue('errmsg')), False) if attrs.has_key('warnmsg'): ai.setWarning(xml.sax.saxutils.unescape(attrs. getValue('warnmsg'))) if attrs.has_key('cputime'): try: ai.setCputime(float(attrs.getValue('cputime'))) except ValueError: pass self.activeInstStack.append(ai) self.activeInst=ai self.affectedInputAIs.add(ai) self.affectedOutputAIs.add(ai) elif name == "active-connection": # any value associated with active connection points if self.activeInst is None: raise ProjectXMLError( "active connection without active instance", self) if not attrs.has_key("id"): raise ProjectXMLError("active conn field has no id", self) if not attrs.has_key("type"): raise ProjectXMLError("active conn field has no type", self) tpnm=keywords.fixID(attrs.getValue("type")) tp=self.importList.getTypeByFullName(tpnm, self.thisImport) if attrs.has_key("seqnr"): seqnr=int(attrs.getValue("seqnr")) else: seqnr=0 name=keywords.fixID(attrs.getValue("id")) # TODO fix this for new type struct. val=None if attrs.has_key("value"): if not attrs.has_key("value_type"): raise ProjectXMLError( "active connection value without value_type", self) vtpnm=keywords.fixID(attrs.getValue("value_type")) vtp=self.importList.getTypeByFullName(vtpnm, self.thisImport) valnm=attrs.getValue("value") valueString=attrs.getValue('value') val=value.interpretLiteral(valueString, tp) if self.ioitem == "inputs": self.activeInst.setInput(name, tp, val, seqnr) elif self.ioitem == "outputs": self.activeInst.setOutput(name, tp, val, seqnr) elif self.ioitem == "subnet-inputs": self.activeInst.setSubnetInput(name, tp, val, seqnr) elif self.ioitem == "subnet-outputs": self.activeInst.setSubnetOutput(name, tp, val, seqnr) else: raise ProjectXMLError( "unknown active connection ioitem '%s'"% (self.ioitem), self) elif name == "tasks": pass # just ignore it; we deal with tasks when they come elif name == "task": if self.curTask is not None: raise ProjectXMLError("task within task", self) if self.activeInst is None: raise ProjectXMLError("task without active instance", self) if not attrs.has_key("seqnr"): raise ProjectXMLError("task has no seqnr", self) if not attrs.has_key("priority"): raise ProjectXMLError("task has no priority", self) priority=int(attrs.getValue("priority")) seqnr=int(attrs.getValue("seqnr")) self.curTask=task.Task(self.project, self.activeInst, self.activeInst.getFunction(), None, priority, seqnr) elif name == "function-input": if self.curTask is None: raise ProjectXMLError("function-input without task", self) inp=run.FunctionRunInput() self.setFnInputReader(run.IOReader(inp, None), name) self.fnInputReader.setReportFilename(self.filename) self.fnInputReader.startElement(name, attrs) elif name == "command-list": if self.curTask is None: raise ProjectXMLError("commands without task", self) self.cmdReader=cpc.command.CommandReader() elif name == "desc": # A description. First find out what it describes. if self.type is not None: if ( len(self.typeStack) > 1 and self.typeStack[-2][0].isSubtype(vtype.recordType)): # if it is a field, describe it as a field tp=self.typeStack[-2][0] field=self.typeStack[-1][1] lstm=tp.getRecordMember(field) self.setDescReader(description.DescriptionReader(lstm, self.filename), name) elif not self.type.isBuiltin() and len(self.typeStack)==1: # it is a custom, describable type self.setDescReader(description.DescriptionReader(self.type, self.filename), name) else: raise ProjectXMLError("description of a builtin type.", self) elif self.function is not None: self.setDescReader(description.DescriptionReader(self.function, self.filename), name) elif self.curTask is None and self.network is None: self.setDescReader(description.DescriptionReader( self.thisImport, self.filename), name) # raise ProjectXMLError("Unknown item to describe.") else: raise ProjectXMLError("Unknown tag %s"%name, self)
def startElement(self, name, attrs): # if name == "value" or name == "data": if name in vtype.basicTypes: basicType = vtype.basicTypes[name] if "type" in attrs: tpnm = keywords.fixID(attrs.getValue("type")) if self.importList is not None: tp = self.importList.getTypeByFullName(tpnm, self.currentImport) else: tp = basicType else: tp = basicType # determine the numerical location in the subitem tree if self.depth == self.lastDepth: self.subCounters[self.depth] += 1 elif self.depth > self.lastDepth: if len(self.subCounters) < self.depth + 1: self.subCounters.append(0) else: self.subCounters[self.depth] = 0 else: self.subCounters[self.depth] += 1 # now determine the sub-item context itemStackAdd = None # the item to add to the item stack at the end if len(self.subStack) > 0: subVal = self.subStack[-1] # subVal is the current value to read if "field" in attrs: if len(self.typeStack) > 0 and self.typeStack[-1].isCompound(): # if it's a field, it's named. itemStackAdd = keywords.fixID(attrs.getValue("field")) createType = None if self.allowUnknownTypes: createType = tp subVal = subVal.getCreateSubValue( [itemStackAdd], createType=createType, setCreateSourceTag=self.sourceTag ) if subVal is None: raise ValXMLError("Did not find field '%s'" % attrs.getValue("field"), self) else: raise ValXMLError("field '%s' inside non-compound type" % attrs.getValue("field"), self) elif "subitem" in attrs: # this is a single, directly addressed sub-item subitems = vtype.parseItemList(keywords.fixID(attrs.getValue("subitem"))) subVal = subVal.getCreateSubValue(subitems, setCreateSourceTag=self.sourceTag) elif len(self.typeStack) > 0 and (self.typeStack[-1] == vtype.arrayType): itemStackAdd = self.subCounters[self.depth] createType = None if self.allowUnknownTypes: createType = tp subVal = subVal.getCreateSubValue( [itemStackAdd], createType=createType, setCreateSourceTag=self.sourceTag ) else: # this is the top-level value self.value = self.valueType(None, tp) subVal = self.value if tp is not None: # check the type if not tp.isSubtype(subVal.getType()): raise ValXMLError("%s not a subtype of %s." % (tp.getName(), subVal.getType().getName()), self) else: tp = subVal.getType() if not subVal.getType().isCompound(): if "value" in attrs: # this means that the type is a literal and can be parsed nval = interpretLiteral(attrs.getValue("value"), tp) subVal.copy(nval) # log.debug("Setting value for %s to %s"% # (subVal.getFullName(), subVal.value) ) # subVal._set(tp.valueFromLiteral(attrs.getValue('value'))) else: if "value" in attrs: raise ValXMLError("Literal value for compound type", self) # increment depth if "seqnr" in attrs: subVal.seqNr = int(attrs.getValue("seqnr")) if "updated" in attrs: updated = cpc.util.getBooleanAttribute(attrs, "updated") if updated: subVal.markUpdated(updated) # log.debug("Setting value for %s"%subVal.getFullName()) subVal.sourceTag = self.sourceTag self.lastDepth = self.depth self.depth += 1 self.typeStack.append(basicType) # self.itemStack.append(itemStackAdd) self.subStack.append(subVal)