diff --git a/developer_tools/createRegressionTestDocumentation.py b/developer_tools/createRegressionTestDocumentation.py index 1eb2248aeb..75883e32a4 100644 --- a/developer_tools/createRegressionTestDocumentation.py +++ b/developer_tools/createRegressionTestDocumentation.py @@ -105,19 +105,18 @@ def noDescriptionTestsAndInformationOnTheOther(self): for testInfoFile in __testInfoList: if 'moose' in testInfoFile.split(os.sep) or not os.path.isfile(testInfoFile): continue - fileObject = open(testInfoFile,"r+") - fileLines = fileObject.readlines() - dirName = os.path.dirname(testInfoFile) - # I do not want to use getpot! - for line in fileLines: - if line.strip().startswith("input"): - fileName = line.split("=")[-1].replace("'", "").replace('"', '').rstrip().strip() - fileName = os.path.join(dirName,fileName) - if os.path.split(fileName)[-1].lower().endswith('xml'): - __testList.append(os.path.abspath(fileName)) - if os.path.split(fileName)[-1].lower().endswith('py'): - __testList.append(os.path.abspath(fileName)) - fileObject.close() + with open(testInfoFile,"r+") as fileObject: + fileLines = fileObject.readlines() + dirName = os.path.dirname(testInfoFile) + # I do not want to use getpot! + for line in fileLines: + if line.strip().startswith("input"): + fileName = line.split("=")[-1].replace("'", "").replace('"', '').rstrip().strip() + fileName = os.path.join(dirName,fileName) + if os.path.split(fileName)[-1].lower().endswith('xml'): + __testList.append(os.path.abspath(fileName)) + if os.path.split(fileName)[-1].lower().endswith('py'): + __testList.append(os.path.abspath(fileName)) for testFile in __testList: if testFile.endswith('xml'): try: root = ET.parse(testFile).getroot() diff --git a/developer_tools/readRequirementsAndCreateLatex.py b/developer_tools/readRequirementsAndCreateLatex.py index 2ef1b2c1ee..f0dc42865b 100644 --- a/developer_tools/readRequirementsAndCreateLatex.py +++ b/developer_tools/readRequirementsAndCreateLatex.py @@ -30,18 +30,17 @@ def writeLatexFromDictionaryOfRequirements(applicationName,requirementGroups,out """ sections = ["section","subsection","subsubsection","paragraph"] indexSec = 0 - fileObject = open(outputFileName,"w+") - fileObject.write("\\"+sections[0].strip()+"{System Requirements: " +applicationName.strip()+"}\n") - for group, value in requirementGroups.items(): - fileObject.write("\\"+sections[1].strip()+"{" +group.strip()+"}\n") - for reqSet, requirements in value.items(): - # construct table - fileObject.write("\\"+sections[2].strip()+"{" +reqSet.strip()+"}\n") - # start write the requirements for this set - for req, content in requirements.items(): - fileObject.write("\\"+sections[3].strip()+"{" +req.strip()+"} \n") - fileObject.write(content['description'].strip() + "\n") - fileObject.close() + with open(outputFileName,"w+") as fileObject: + fileObject.write("\\"+sections[0].strip()+"{System Requirements: " +applicationName.strip()+"}\n") + for group, value in requirementGroups.items(): + fileObject.write("\\"+sections[1].strip()+"{" +group.strip()+"}\n") + for reqSet, requirements in value.items(): + # construct table + fileObject.write("\\"+sections[2].strip()+"{" +reqSet.strip()+"}\n") + # start write the requirements for this set + for req, content in requirements.items(): + fileObject.write("\\"+sections[3].strip()+"{" +req.strip()+"} \n") + fileObject.write(content['description'].strip() + "\n") def readRequirementsXml(fileName): diff --git a/ravenframework/CodeInterfaceClasses/MooseBasedApp/BISONMESHSCRIPTparser.py b/ravenframework/CodeInterfaceClasses/MooseBasedApp/BISONMESHSCRIPTparser.py index 1aa2d99336..cfe1c8ee10 100644 --- a/ravenframework/CodeInterfaceClasses/MooseBasedApp/BISONMESHSCRIPTparser.py +++ b/ravenframework/CodeInterfaceClasses/MooseBasedApp/BISONMESHSCRIPTparser.py @@ -147,13 +147,12 @@ def writeNewInput(self,outfile=None): """ if outfile==None: outfile = self.inputfile - IOfile = open(outfile,'w') - for e, entry in enumerate(self.fileOrderStorage): - if type(entry) == unicode: - IOfile.writelines(entry) - elif type(entry) == list: - DictBlockName = self.fileOrderStorage[e][0] - DictBlock = self.AllVarDict[DictBlockName] - for key, value in DictBlock.items(): - IOfile.writelines(DictBlockName + "['" + key + "'] = " + str(value) + '\n') - IOfile.close() + with open(outfile,'w') as IOfile: + for e, entry in enumerate(self.fileOrderStorage): + if type(entry) == unicode: + IOfile.writelines(entry) + elif type(entry) == list: + DictBlockName = self.fileOrderStorage[e][0] + DictBlock = self.AllVarDict[DictBlockName] + for key, value in DictBlock.items(): + IOfile.writelines(DictBlockName + "['" + key + "'] = " + str(value) + '\n') diff --git a/ravenframework/CodeInterfaceClasses/MooseBasedApp/CUBITparser.py b/ravenframework/CodeInterfaceClasses/MooseBasedApp/CUBITparser.py index 0c86b602fc..af47332588 100644 --- a/ravenframework/CodeInterfaceClasses/MooseBasedApp/CUBITparser.py +++ b/ravenframework/CodeInterfaceClasses/MooseBasedApp/CUBITparser.py @@ -86,11 +86,10 @@ def writeNewInput(self,outFile=None): """ if outFile == None: outFile = self.inputfile - IOfile = open(outFile,'w') - for entry in self.fileOrderStorage: - if type(entry) == unicode: - IOfile.writelines(entry) - elif type(entry) == list: - for key, value in self.keywordDictionary.items(): - IOfile.writelines('#{ '+key+' = '+str(value)+'}'+'\n') - IOfile.close() + with open(outFile,'w') as IOfile: + for entry in self.fileOrderStorage: + if type(entry) == unicode: + IOfile.writelines(entry) + elif type(entry) == list: + for key, value in self.keywordDictionary.items(): + IOfile.writelines('#{ '+key+' = '+str(value)+'}'+'\n') diff --git a/ravenframework/CodeInterfaceClasses/Neutrino/neutrinoInterface.py b/ravenframework/CodeInterfaceClasses/Neutrino/neutrinoInterface.py index cc9f8a7302..645d71cf50 100644 --- a/ravenframework/CodeInterfaceClasses/Neutrino/neutrinoInterface.py +++ b/ravenframework/CodeInterfaceClasses/Neutrino/neutrinoInterface.py @@ -203,23 +203,20 @@ def finalizeCodeOutput(self, command, output, workingDir): return newOutputPath''' # open original output file (the working directory is provided) - outputFile = open(outputPath,"r+") + with open(outputPath,"r+") as outputFile: + #Open the new output file so the results can be written to it and put in the form for RAVEN to read + with open(newOutputPath + ".csv", 'w') as resultsFile: - #Open the new output file so the results can be written to it and put in the form for RAVEN to read - resultsFile = open(newOutputPath + ".csv", 'w') + lines = outputFile.readlines() - lines = outputFile.readlines() + #Needed for RAVEN to read output + #These need to match RAVEN input file output names + resultsFile.write('time,result\n') - #Needed for RAVEN to read output - #These need to match RAVEN input file output names - resultsFile.write('time,result\n') + #Write Neutrino results to a new file for RAVEN + for line in lines: + resultsFile.write(line) - #Write Neutrino results to a new file for RAVEN - for line in lines: - resultsFile.write(line) - resultsFile.close() - - outputFile.close() return newOutputPath diff --git a/ravenframework/CodeInterfaceClasses/OpenModelica/OpenModelicaInterface.py b/ravenframework/CodeInterfaceClasses/OpenModelica/OpenModelicaInterface.py index 3b0bf7dfe3..23e5b19a1a 100644 --- a/ravenframework/CodeInterfaceClasses/OpenModelica/OpenModelicaInterface.py +++ b/ravenframework/CodeInterfaceClasses/OpenModelica/OpenModelicaInterface.py @@ -270,12 +270,11 @@ def finalizeCodeOutput(self, command, output, workingDir): print('sourcefilename:',sourceFileName) destFileName = sourceFileName.replace('rawout~', 'out~') # When fix the CSV, change rawout~ to out~ sourceFileName += '.csv' - inputFile = open(sourceFileName) - for line in inputFile: - # Line ends with a comma followed by a newline - #XXX toBytes seems to be needed here in python3, despite the text = True - os.write(tempOutputFD, utils.toBytes(line.replace('"','').strip().strip(',') + '\n')) - inputFile.close() + with open(sourceFileName) as inputFile: + for line in inputFile: + # Line ends with a comma followed by a newline + #XXX toBytes seems to be needed here in python3, despite the text = True + os.write(tempOutputFD, utils.toBytes(line.replace('"','').strip().strip(',') + '\n')) os.close(tempOutputFD) shutil.move(tempOutputFileName, destFileName + '.csv') return destFileName # Return the name without the .csv on it...RAVEN will add it diff --git a/ravenframework/CodeInterfaceClasses/PHISICS/DecayParser.py b/ravenframework/CodeInterfaceClasses/PHISICS/DecayParser.py index aad27389e1..d722a33595 100644 --- a/ravenframework/CodeInterfaceClasses/PHISICS/DecayParser.py +++ b/ravenframework/CodeInterfaceClasses/PHISICS/DecayParser.py @@ -30,9 +30,8 @@ def __init__(self, inputFiles, workingDir, **pertDict): self.inputFiles = inputFiles self.pertDict = self.scientificNotation(pertDict) # open the unperturbed file - openInputFile = open(self.inputFiles, "r") - lines = openInputFile.readlines() - openInputFile.close() + with open(self.inputFiles, "r") as openInputFile: + lines = openInputFile.readlines() self.characterizeLibrary(lines) self.fileReconstruction() diff --git a/ravenframework/CodeInterfaceClasses/PHISICS/FissionYieldParser.py b/ravenframework/CodeInterfaceClasses/PHISICS/FissionYieldParser.py index 02f146ed53..84c3f014e7 100644 --- a/ravenframework/CodeInterfaceClasses/PHISICS/FissionYieldParser.py +++ b/ravenframework/CodeInterfaceClasses/PHISICS/FissionYieldParser.py @@ -34,9 +34,8 @@ def __init__(self, inputFiles, workingDir, **pertDict): pertDict) # Perturbed variables # open the unperturbed file - openInputFile = open(self.inputFiles, "r") - lines = openInputFile.readlines() - openInputFile.close() + with open(self.inputFiles, "r") as openInputFile: + lines = openInputFile.readlines() self.characterizeLibrary(lines) self.isotopeList = list(set( diff --git a/ravenframework/CodeInterfaceClasses/PHISICS/MassParser.py b/ravenframework/CodeInterfaceClasses/PHISICS/MassParser.py index 75aba255d8..63bb3f365e 100644 --- a/ravenframework/CodeInterfaceClasses/PHISICS/MassParser.py +++ b/ravenframework/CodeInterfaceClasses/PHISICS/MassParser.py @@ -109,9 +109,8 @@ def printInput(self, workingDir): @ Out, None """ # open the unperturbed file - openInputFile = open(self.inputFiles, "r") - lines = openInputFile.readlines() - openInputFile.close() + with open(self.inputFiles, "r") as openInputFile: + lines = openInputFile.readlines() # remove the file if was already existing if os.path.exists(self.inputFiles): diff --git a/ravenframework/CodeInterfaceClasses/PHISICS/PathParser.py b/ravenframework/CodeInterfaceClasses/PHISICS/PathParser.py index 21db6909da..2aacdb79b1 100644 --- a/ravenframework/CodeInterfaceClasses/PHISICS/PathParser.py +++ b/ravenframework/CodeInterfaceClasses/PHISICS/PathParser.py @@ -95,9 +95,8 @@ def printInput(self, workingDir): @ Out, None """ # open the unperturbed file - openInputFile = open(self.inputFiles, "r") - lines = openInputFile.readlines() - openInputFile.close() + with open(self.inputFiles, "r") as openInputFile: + lines = openInputFile.readlines() # remove the file if was already existing if os.path.exists(self.inputFiles): diff --git a/ravenframework/CodeInterfaceClasses/PHISICS/QValuesParser.py b/ravenframework/CodeInterfaceClasses/PHISICS/QValuesParser.py index e6bf911a14..3ddf9c8137 100644 --- a/ravenframework/CodeInterfaceClasses/PHISICS/QValuesParser.py +++ b/ravenframework/CodeInterfaceClasses/PHISICS/QValuesParser.py @@ -105,9 +105,8 @@ def printInput(self, workingDir): @ Out, None """ # open the unperturbed file - openInputFile = open(self.inputFiles, "r") - lines = openInputFile.readlines() - openInputFile.close() + with open(self.inputFiles, "r") as openInputFile: + lines = openInputFile.readlines() # remove the file if was already existing if os.path.exists(self.inputFiles): diff --git a/ravenframework/CodeInterfaceClasses/PHISICS/XSCreator.py b/ravenframework/CodeInterfaceClasses/PHISICS/XSCreator.py index 8d78057842..52ee10ef57 100644 --- a/ravenframework/CodeInterfaceClasses/PHISICS/XSCreator.py +++ b/ravenframework/CodeInterfaceClasses/PHISICS/XSCreator.py @@ -95,9 +95,8 @@ def generateXML(self,workingDir,bool,inputFiles,tabMapFileName): for count,(group,value) in enumerate(self.listedDict.get('XS').get(tabulation).get(material).get(isotope).get(typeOfXs).get(reaction).items()): reactionChild = SubElement(libraryChild, self.formatXS(reaction), {'g':group}) reactionChild.text = value - fileObj = open(inputFiles, 'w') - fileObj.write(self.prettify(top)) - fileObj.close() + with open(inputFiles, 'w') as fileObj: + fileObj.write(self.prettify(top)) def formatXS(self,reaction): """ diff --git a/ravenframework/CodeInterfaceClasses/Prescient/PrescientCodeInterface.py b/ravenframework/CodeInterfaceClasses/Prescient/PrescientCodeInterface.py index 0ac772da80..a7fdf51c97 100644 --- a/ravenframework/CodeInterfaceClasses/Prescient/PrescientCodeInterface.py +++ b/ravenframework/CodeInterfaceClasses/Prescient/PrescientCodeInterface.py @@ -93,10 +93,9 @@ def createNewInput(self, inputs, oinputs, samplerType, **Kwargs): elif line.lstrip().startswith("--output-directory="): self._outputDirectory = line.split("=",1)[1].rstrip() newLines.append(line) - newFile = open(singleInput.getAbsFile(),"w") - for line in newLines: - newFile.write(line) - newFile.close() + with open(singleInput.getAbsFile(),"w") as newFile: + for line in newLines: + newFile.write(line) elif singleInput.getType() == 'PrescientInput': #print("SampledVars", Kwargs["SampledVars"]) #print("Modifying", singleInput) diff --git a/ravenframework/CodeInterfaceClasses/Rattlesnake/YakInstantLibraryParser.py b/ravenframework/CodeInterfaceClasses/Rattlesnake/YakInstantLibraryParser.py index c87fa39fc8..1baa2a74c4 100644 --- a/ravenframework/CodeInterfaceClasses/Rattlesnake/YakInstantLibraryParser.py +++ b/ravenframework/CodeInterfaceClasses/Rattlesnake/YakInstantLibraryParser.py @@ -456,16 +456,15 @@ def writeNewInput(self,inFiles=None,**Kwargs): @ Out, None. """ for outFile in inFiles: - newFile = open(outFile.getAbsFile(),'w') - tree = self.xmlsDict[outFile.getFilename()] - root = tree.getroot() - for child in root: - for mat in child: - matID = mat.attrib['ID'].strip() - if matID not in self.aliases.keys(): - continue - self._replaceXMLNodeText(mat,self.pertLib[matID]) + with open(outFile.getAbsFile(),'w') as newFile: + tree = self.xmlsDict[outFile.getFilename()] + root = tree.getroot() + for child in root: + for mat in child: + matID = mat.attrib['ID'].strip() + if matID not in self.aliases.keys(): + continue + self._replaceXMLNodeText(mat,self.pertLib[matID]) - toWrite = self._prettify(tree) - newFile.writelines(toWrite) - newFile.close() + toWrite = self._prettify(tree) + newFile.writelines(toWrite) diff --git a/ravenframework/CodeInterfaceClasses/Utilities/dynamicEventTreeUtilities.py b/ravenframework/CodeInterfaceClasses/Utilities/dynamicEventTreeUtilities.py index 1c8424af5a..6cae94547d 100644 --- a/ravenframework/CodeInterfaceClasses/Utilities/dynamicEventTreeUtilities.py +++ b/ravenframework/CodeInterfaceClasses/Utilities/dynamicEventTreeUtilities.py @@ -52,6 +52,5 @@ def writeXmlForDET(filename,trigger,listDict,stopInfo): var.set('actual_value',str(varInfo['new_value'])) if 'associated_pb' in varInfo.keys(): var.set('probability',str(varInfo['associated_pb'])) - fileObject = open(filename,'w') - fileObject.write(minidom.parseString(ET.tostring(root, 'utf-8')).toprettyxml(indent="\t")) - fileObject.close() + with open(filename,'w') as fileObject: + fileObject.write(minidom.parseString(ET.tostring(root, 'utf-8')).toprettyxml(indent="\t"))