return same,msg try: bl = next(genB) except StopIteration: return False,msg + ['file '+str(a)+' has more lines than '+str(b)] if al.rstrip('\n\r') != bl.rstrip('\n\r'): same = False print('al '+repr(al)+" != bl "+repr(bl)) msg += ['line '+str(i)+' is not the same!'] #first test XML to XML print('Testing XML to XML ...') tree = TS.parse(open(os.path.join('parse','example_xml.xml'),'r')) strTree = TS.tostring(tree) fname = os.path.join('parse','fromXmltoXML.xml') open(fname,'w').write(toString(strTree)) same,msg = checkSameFile(open(fname,'r'),open(os.path.join('gold',fname),'r')) if same: results['passed']+=1 print(' ... passed!') else: results['failed']+=1 print(' ... failures in XML to XML:') print(' ',msg[0]) getpot = open(os.path.join('parse','example_getpot.i'),'r') gtree = TS.parse(getpot,dType='GetPot')
def XMLread(self,xmlNode,runInfoSkip = set(),xmlFilename=None): """ parses the xml input file, instances the classes need to represent all objects in the simulation @ In, xmlNode, ElementTree.Element, xml node to read in @ In, runInfoSkip, set, optional, nodes to skip @ In, xmlFilename, string, optional, xml filename for relative directory @ Out, None """ #TODO update syntax to note that we read InputTrees not XmlTrees unknownAttribs = utils.checkIfUnknowElementsinList(['printTimeStamps','verbosity','color','profile'],list(xmlNode.attrib.keys())) if len(unknownAttribs) > 0: errorMsg = 'The following attributes are unknown:' for element in unknownAttribs: errorMsg += ' ' + element self.raiseAnError(IOError,errorMsg) self.verbosity = xmlNode.attrib.get('verbosity','all').lower() if 'printTimeStamps' in xmlNode.attrib.keys(): self.raiseADebug('Setting "printTimeStamps" to',xmlNode.attrib['printTimeStamps']) self.messageHandler.setTimePrint(xmlNode.attrib['printTimeStamps']) if 'color' in xmlNode.attrib.keys(): self.raiseADebug('Setting color output mode to',xmlNode.attrib['color']) self.messageHandler.setColor(xmlNode.attrib['color']) if 'profile' in xmlNode.attrib.keys(): thingsToProfile = list(p.strip().lower() for p in xmlNode.attrib['profile'].split(',')) if 'jobs' in thingsToProfile: self.jobHandler.setProfileJobs(True) self.messageHandler.verbosity = self.verbosity runInfoNode = xmlNode.find('RunInfo') if runInfoNode is None: self.raiseAnError(IOError,'The RunInfo node is missing!') self.__readRunInfo(runInfoNode,runInfoSkip,xmlFilename) ### expand variable groups before continuing ### ## build variable groups ## varGroupNode = xmlNode.find('VariableGroups') # init, read XML for variable groups if varGroupNode is not None: varGroups = xmlUtils.readVariableGroups(varGroupNode,self.messageHandler,self) else: varGroups={} # read other nodes for child in xmlNode: if child.tag=='VariableGroups': continue #we did these before the for loop if child.tag in list(self.whichDict.keys()): self.raiseADebug('-'*2+' Reading the block: {0:15}'.format(str(child.tag))+2*'-') Class = child.tag if len(child.attrib.keys()) == 0: globalAttributes = {} else: globalAttributes = child.attrib #if 'verbosity' in globalAttributes.keys(): self.verbosity = globalAttributes['verbosity'] if Class not in ['RunInfo','OutStreams'] and "returnInputParameter" in self.addWhatDict[Class].__dict__: paramInput = self.addWhatDict[Class].returnInputParameter() paramInput.parseNode(child) for childChild in paramInput.subparts: childName = childChild.getName() if "name" not in childChild.parameterValues: self.raiseAnError(IOError,'not found name attribute for '+childName +' in '+Class) name = childChild.parameterValues["name"] if "needsRunInfo" in self.addWhatDict[Class].__dict__: self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childName,self.runInfoDict,self) else: self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childName,self) self.whichDict[Class][name].handleInput(childChild, self.messageHandler, varGroups, globalAttributes=globalAttributes) elif Class != 'RunInfo': for childChild in child: subType = childChild.tag if 'name' in childChild.attrib.keys(): name = childChild.attrib['name'] self.raiseADebug('Reading type '+str(childChild.tag)+' with name '+name) #place the instance in the proper dictionary (self.whichDict[Type]) under his name as key, #the type is the general class (sampler, data, etc) while childChild.tag is the sub type #if name not in self.whichDict[Class].keys(): self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self) if Class != 'OutStreams': if name not in self.whichDict[Class].keys(): if "needsRunInfo" in self.addWhatDict[Class].__dict__: self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self.runInfoDict,self) else: self.whichDict[Class][name] = self.addWhatDict[Class].returnInstance(childChild.tag,self) else: self.raiseAnError(IOError,'Redundant naming in the input for class '+Class+' and name '+name) else: if name not in self.whichDict[Class][subType].keys(): self.whichDict[Class][subType][name] = self.addWhatDict[Class][subType].returnInstance(childChild.tag,self) else: self.raiseAnError(IOError,'Redundant naming in the input for class '+Class+' and sub Type'+subType+' and name '+name) #now we can read the info for this object #if globalAttributes and 'verbosity' in globalAttributes.keys(): localVerbosity = globalAttributes['verbosity'] #else : localVerbosity = self.verbosity if Class != 'OutStreams': self.whichDict[Class][name].readXML(childChild, self.messageHandler, varGroups, globalAttributes=globalAttributes) else: self.whichDict[Class][subType][name].readXML(childChild, self.messageHandler, globalAttributes=globalAttributes) else: self.raiseAnError(IOError,'not found name attribute for one '+Class) else: #tag not in whichDict, check if it's a documentation tag if child.tag not in ['TestInfo']: self.raiseAnError(IOError,'<'+child.tag+'> is not among the known simulation components '+repr(child)) # If requested, duplicate input # ###NOTE: All substitutions to the XML input tree should be done BEFORE this point!! if self.runInfoDict.get('printInput',False): fileName = os.path.join(self.runInfoDict['WorkingDir'],self.runInfoDict['printInput']) self.raiseAMessage('Writing duplicate input file:',fileName) outFile = open(fileName,'w') outFile.writelines(utils.toString(TreeStructure.tostring(xmlNode))+'\n') #\n for no-end-of-line issue outFile.close() if not set(self.stepSequenceList).issubset(set(self.stepsDict.keys())): self.raiseAnError(IOError,'The step list: '+str(self.stepSequenceList)+' contains steps that have not been declared: '+str(list(self.stepsDict.keys())))