From b041051be245d13fb83305f220185ce70bec56e5 Mon Sep 17 00:00:00 2001 From: Lee Newberg Date: Tue, 1 Mar 2022 11:04:53 -0500 Subject: [PATCH] STYLE: Format Python code with black --- SoftwareGuide/Examples/ParseCxxExamples.py | 53 ++--- SoftwareGuide/Examples/RunExamples.py | 214 ++++++++++++++------- 2 files changed, 171 insertions(+), 96 deletions(-) diff --git a/SoftwareGuide/Examples/ParseCxxExamples.py b/SoftwareGuide/Examples/ParseCxxExamples.py index e3f92d54..247a01a6 100755 --- a/SoftwareGuide/Examples/ParseCxxExamples.py +++ b/SoftwareGuide/Examples/ParseCxxExamples.py @@ -15,19 +15,19 @@ beginCodeBlockTag = "BeginCodeSnippet" endCodeBlockTag = "EndCodeSnippet" -validCodeBlockTypes = ['Latex', 'CodeSnippet'] +validCodeBlockTypes = ["Latex", "CodeSnippet"] ## This class is initialized with a the starting line of ## the command processing, and the block of text for ## this command invocation -class OneDocBlock(): +class OneDocBlock: def __init__(self, sourceFile, id, codeblock): self.sourceFile = sourceFile self.id = id self.codeblock = codeblock - self.blockType = 'Unknown' # Something other than items in validCodeBlockTypes + self.blockType = "Unknown" # Something other than items in validCodeBlockTypes def Print(self): blockline = self.id @@ -40,16 +40,16 @@ def Print(self): def GetCodeBlockString(self): blockstring = "" - if self.blockType == 'Latex': + if self.blockType == "Latex": for blocktext in self.codeblock: blockstring += "{0}\n".format(blocktext) pass - elif self.blockType == 'CodeSnippet': + elif self.blockType == "CodeSnippet": # blockstring += "\\small\n" # blockstring += "\\begin{verbatim}\n" # blockstring += "\\begin{itklisting}[language=C++]\n" blockstring += "\\begin{minted}[baselinestretch=1,fontsize=\\footnotesize,linenos=false,bgcolor=ltgray]{c++}\n" -#blockstring += "\\begin{minted}[baselinestretch=1,fontsize=\small,linenos=false,bgcolor=ltgray]{c++}\n" + # blockstring += "\\begin{minted}[baselinestretch=1,fontsize=\small,linenos=false,bgcolor=ltgray]{c++}\n" for blocktext in self.codeblock: blockstring += "{0}".format(blocktext) blockstring += "\\end{minted}\n" @@ -70,7 +70,7 @@ def ParseOneFile(sourceFile): # Read each line and Parse the input file # # Get the command line args from the source file - sf = open(sourceFile, 'r') + sf = open(sourceFile, "r") INFILE = sf.readlines() sf.close() parseLine = 0 @@ -90,7 +90,7 @@ def ParseOneFile(sourceFile): checkForBlankLine = True elif thisline.count(endLatexTag) == 1: # end of LatexCodeBlock ocb = OneDocBlock(sourceFile, starttagline, codeBlock) - ocb.blockType = 'Latex' + ocb.blockType = "Latex" thisFileCommandBlocks.append(ocb) starttagline = 0 elif thisline.count(beginCodeBlockTag) == 1: # start of CodeSnippet @@ -99,28 +99,27 @@ def ParseOneFile(sourceFile): codeBlock = [] elif thisline.count(endCodeBlockTag) == 1: # end of CodeSnippet ocb = OneDocBlock(sourceFile, starttagline, codeBlock) - ocb.blockType = 'CodeSnippet' + ocb.blockType = "CodeSnippet" thisFileCommandBlocks.append(ocb) starttagline = 0 elif starttagline > 0: # Inside a codeBlock if isLatexBlock == True: - thisline = commentPattern.sub("",thisline) + thisline = commentPattern.sub("", thisline) thisline = thisline.lstrip().rstrip() if checkForBlankLine: - if thisline != "": - print("{filename}:{line}: warning: Line after start of LaTeX block should be a newline -- instead got {value}".format( - filename=sourceFile, - line=parseLine, - value=thisline - ) + if thisline != "": + print( + "{filename}:{line}: warning: Line after start of LaTeX block should be a newline -- instead got {value}".format( + filename=sourceFile, line=parseLine, value=thisline + ) + ) + checkForBlankLine = False + + if not isLatexBlock and (len(thisline) > 80): + print( + "{filename}:{line}:80: warning: Line length too long for LaTeX printing".format( + filename=sourceFile, line=parseLine ) - checkForBlankLine = False - - if not isLatexBlock and ( len(thisline) > 80 ): - print("{filename}:{line}:80: warning: Line length too long for LaTeX printing".format( - filename=sourceFile, - line=parseLine - ) ) codeBlock.append(thisline) else: # non-codeBlock line @@ -142,11 +141,15 @@ def GetPreambleString(examplefilename): The source code for this section can be found in the file\\\\ \\texttt{2}{1}{3}. -""".format(examplefilename, os.path.basename(examplefilename), '{', '}') +""".format( + examplefilename, os.path.basename(examplefilename), "{", "}" + ) return preamble + if __name__ == "__main__": import sys + if len(sys.argv) < 2: print("Usage: {0} ".format(argv[0])) sys.exit(-1) @@ -166,7 +169,7 @@ def GetPreambleString(examplefilename): else: raise - outPtr = open(outputfilename, 'w') + outPtr = open(outputfilename, "w") outPtr.write(GetPreambleString(inputfilename)) for cb in thisCodeBlocks: outPtr.write(cb.GetCodeBlockString()) diff --git a/SoftwareGuide/Examples/RunExamples.py b/SoftwareGuide/Examples/RunExamples.py index c9136e75..aca8d818 100644 --- a/SoftwareGuide/Examples/RunExamples.py +++ b/SoftwareGuide/Examples/RunExamples.py @@ -17,6 +17,7 @@ outputToCodeBlockMap = dict() + def mkdir_p(path): """ Safely make a new directory, checking if it already exists""" try: @@ -26,6 +27,8 @@ def mkdir_p(path): pass else: raise + + """ def GetFilesInThisLine(line, IOtag): line.replace(IOtag, "") # Strip the tag away @@ -46,7 +49,7 @@ def GetInputFilesInThisLine(line): ## This class is initialized with a the starting line of ## the command processing, and the block of text for ## this command invocation -class OneCodeBlock(): +class OneCodeBlock: def __init__(self, sourceFile, id, codeblock, pathFinder): self.sourceFile = sourceFile self.id = id @@ -61,7 +64,11 @@ def __init__(self, sourceFile, id, codeblock, pathFinder): self.parents = set() self.children = set() if not os.path.exists(self.progFullPath): - print("ERROR: Required program {0} does not exists. Please rebuild ITK".format(self.progBaseName)) + print( + "ERROR: Required program {0} does not exists. Please rebuild ITK".format( + self.progBaseName + ) + ) sys.exit(-1) def GetProgBaseName(self): @@ -111,11 +118,19 @@ def AreOutputsNewer(self): else: print("Missing input {0}".format(i)) print("Searched {0}".format(self.inputs)) - print("ERROR:"*20) - print("Failing to process all data, This should never happen because you should only run this function once all inputs exists.") - sys.exit(-1) # This should never happen because you should only run this function once all inputs exists. + print("ERROR:" * 20) + print( + "Failing to process all data, This should never happen because you should only run this function once all inputs exists." + ) + sys.exit( + -1 + ) # This should never happen because you should only run this function once all inputs exists. if self.verbose: - print("Newest Input: {0}, Oldest Output: {1}".format(newest_input, oldest_output)) + print( + "Newest Input: {0}, Oldest Output: {1}".format( + newest_input, oldest_output + ) + ) if newest_input < oldest_output: return True else: @@ -123,35 +138,46 @@ def AreOutputsNewer(self): def GetCommandLine(self): commandLine = self.progFullPath + " " - lineparse = re.compile(' *(.*): *(.*)') + lineparse = re.compile(" *(.*): *(.*)") currLineNumber = self.id for currLine in self.codeblock: currLineNumber = currLineNumber + 1 parseGroups = lineparse.search(currLine) if parseGroups == None: - print("ERROR: Invalid parsing of {0} at line {1}".format(self.sourceFile, currLineNumber)) + print( + "ERROR: Invalid parsing of {0} at line {1}".format( + self.sourceFile, currLineNumber + ) + ) sys.exit(-1) - if parseGroups.group(1) == 'INPUTS': + if parseGroups.group(1) == "INPUTS": inputBaseFileName = parseGroups.group(2) inputFileName = pathFinder.GetInputPath(inputBaseFileName) if inputFileName == None: - print("ERROR: Invalid input {0} at {1} at line {2}".format(parseGroups.group(2), - self.sourceFile, currLineNumber)) + print( + "ERROR: Invalid input {0} at {1} at line {2}".format( + parseGroups.group(2), self.sourceFile, currLineNumber + ) + ) exit(-1) else: commandLine = commandLine + " " + inputFileName if not os.path.exists(inputFileName): inputFileName = pathFinder.GetOutputPath(inputBaseFileName) if not os.path.exists(inputFileName): - print("WARNING: Can not find {0} path, assuming it is autogenerated".format(inputFileName)) + print( + "WARNING: Can not find {0} path, assuming it is autogenerated".format( + inputFileName + ) + ) self.inputs.append(inputFileName) - elif parseGroups.group(1) == 'OUTPUTS': + elif parseGroups.group(1) == "OUTPUTS": outputFileName = pathFinder.GetOutputPath(parseGroups.group(2)) commandLine = commandLine + " " + outputFileName self.outputs.append(outputFileName) - elif parseGroups.group(1) == 'ARGUMENTS': + elif parseGroups.group(1) == "ARGUMENTS": commandLine = commandLine + " " + parseGroups.group(2) - elif parseGroups.group(1) == 'NOT_IMPLEMENTED': + elif parseGroups.group(1) == "NOT_IMPLEMENTED": pass return commandLine @@ -164,28 +190,32 @@ def GetInputPaths(self): def MakeAllFileLists(self): self.inputs = [] self.outputs = [] - lineparse = re.compile(' *(.*): *(.*)') + lineparse = re.compile(" *(.*): *(.*)") lineNumber = self.id for currLine in self.codeblock: lineNumber = lineNumber + 1 parseGroups = lineparse.search(currLine) parseKey = parseGroups.group(1).rstrip().lstrip() - if parseKey == '': + if parseKey == "": continue # Empty lines are OK - elif parseKey == 'INPUTS': + elif parseKey == "INPUTS": inputFile = currLine.replace("INPUTS:", "").rstrip().lstrip() inputFile = pathFinder.GetInputPath(inputFile) self.inputs.append(inputFile) - elif parseKey == 'OUTPUTS': + elif parseKey == "OUTPUTS": outputFile = currLine.replace("OUTPUTS:", "").rstrip().lstrip() outputFile = pathFinder.GetOutputPath(outputFile) self.outputs.append(outputFile) - elif parseKey == 'ARGUMENTS': + elif parseKey == "ARGUMENTS": pass - elif parseKey == 'NOT_IMPLEMENTED': + elif parseKey == "NOT_IMPLEMENTED": pass else: - print("ERROR: INVALID LINE IDENTIFIER {0} at line {1} in {2}".format(parseGroups.group(1), lineNumber, self.sourceFile)) + print( + "ERROR: INVALID LINE IDENTIFIER {0} at line {1} in {2}".format( + parseGroups.group(1), lineNumber, self.sourceFile + ) + ) sys.exit(-1) def Print(self): @@ -209,11 +239,11 @@ def PopulateChildren(self): def ParseOneFile(sourceFile, pathFinder): - # - # Read each line and Parse the input file - # - # Get the command line args from the source file - sf = open(sourceFile, 'r') + # + # Read each line and Parse the input file + # + # Get the command line args from the source file + sf = open(sourceFile, "r") INFILE = sf.readlines() sf.close() parseLine = 0 @@ -222,9 +252,9 @@ def ParseOneFile(sourceFile, pathFinder): for thisline in INFILE: parseLine += 1 - thisline = thisline.replace('//', '') - thisline = thisline.replace('{', '').replace('}', '') - thisline = thisline.rstrip().rstrip('/').rstrip().lstrip().lstrip('/').lstrip() + thisline = thisline.replace("//", "") + thisline = thisline.replace("{", "").replace("}", "") + thisline = thisline.rstrip().rstrip("/").rstrip().lstrip().lstrip("/").lstrip() # If the "BeginCommandLineArgs" tag is found, set the "starttagline" var and # initialize a few variables and arrays. if thisline.count(beginCmdLineArgstag) == 1: # start of codeBlock @@ -240,6 +270,7 @@ def ParseOneFile(sourceFile, pathFinder): pass return thisFileCommandBlocks + dirsNotUsed = [] @@ -261,16 +292,18 @@ def getdirs(basedir, age): class ITKPathFinder: def __init__(self, itkSourceDir, itkExecutablesDir, itkBuildDir, SWGuidBaseOutput): self.execDir = itkExecutablesDir - self.execDir = self.execDir.rstrip('/') - self.outPicDir = os.path.join(SWGuidBaseOutput,'Art','Generated') + self.execDir = self.execDir.rstrip("/") + self.outPicDir = os.path.join(SWGuidBaseOutput, "Art", "Generated") # Check if there are any input files that need to be flipped. self.outPicDir = os.path.realpath(self.outPicDir) - self.outPicDir = self.outPicDir.rstrip('/') + self.outPicDir = self.outPicDir.rstrip("/") mkdir_p(self.outPicDir) # HACK: Need beter search criteria - searchPaths = '{0}/ExternalData/Testing/Data/Input#{0}/ExternalData/Examples/Data/BrainWeb#{0}/Testing/Temporary#{0}/Modules/Nonunit/Review/test#{0}/ExternalData/Modules/Segmentation/LevelSetsv4/test/Baseline#{0}/ExternalData/Modules/IO/GE/test/Baseline#{0}/ExternalData/Examples/Filtering/test/Baseline#{0}/Examples/Segmentation/test#{1}/Art/Generated#{2}/Examples/Data'.format(itkBuildDir, SWGuidBaseOutput, itkSourceDir) - dirtyDirPaths = searchPaths.split('#') + searchPaths = "{0}/ExternalData/Testing/Data/Input#{0}/ExternalData/Examples/Data/BrainWeb#{0}/Testing/Temporary#{0}/Modules/Nonunit/Review/test#{0}/ExternalData/Modules/Segmentation/LevelSetsv4/test/Baseline#{0}/ExternalData/Modules/IO/GE/test/Baseline#{0}/ExternalData/Examples/Filtering/test/Baseline#{0}/Examples/Segmentation/test#{1}/Art/Generated#{2}/Examples/Data".format( + itkBuildDir, SWGuidBaseOutput, itkSourceDir + ) + dirtyDirPaths = searchPaths.split("#") self.searchDirList = [] for eachpath in dirtyDirPaths: @@ -282,8 +315,8 @@ def __init__(self, itkSourceDir, itkExecutablesDir, itkBuildDir, SWGuidBaseOutpu def GetProgramPath(self, execfilenamebase): testPath = os.path.join(self.execDir, execfilenamebase) - if os.name == 'nt': - testPath += '.exe' + if os.name == "nt": + testPath += ".exe" if os.path.exists(testPath): return testPath else: @@ -296,7 +329,7 @@ def GetInputPath(self, inputBaseName): if os.path.exists(testPath): return testPath else: - #print('##STATUS: Not yet found input {0}'.format(testPath)) + # print('##STATUS: Not yet found input {0}'.format(testPath)) pass return self.GetOutputPath(inputBaseName) @@ -304,10 +337,11 @@ def GetOutputPath(self, outputBaseName): outPath = os.path.join(self.outPicDir, outputBaseName) # outPath = outPath.replace(self.outPicDir+'/'+self.outPicDir, self.outPicDir ) #Avoid multiple path concatenations # if not os.path.exists(outPath): - # print("@@Warning: Output missing {0}".format(outPath)) + # print("@@Warning: Output missing {0}".format(outPath)) return outPath -class CodeBlockTopSort(): + +class CodeBlockTopSort: def __init__(self, CodeBlockList): self.CodeBlockList = list(codeblock for codeblock in CodeBlockList) self.PopulateCodeBlockToOutputMap() @@ -319,7 +353,7 @@ def __init__(self, CodeBlockList): def PopulateCodeBlockToOutputMap(self): for codeblock in self.CodeBlockList: for outputFile in codeblock.outputs: - outputToCodeBlockMap[outputFile] = codeblock; + outputToCodeBlockMap[outputFile] = codeblock return def LinkCodeBlocks(self): @@ -329,9 +363,9 @@ def LinkCodeBlocks(self): def SortCodeBlocks(self): # In this implementation, a parent CodeBlock depends on its children - while(len(self.unsortedCodeBlockSet) > 0): + while len(self.unsortedCodeBlockSet) > 0: candidate = self.unsortedCodeBlockSet.pop() - if(len(candidate.children) > 0): + if len(candidate.children) > 0: # If it still has children, put it back in the pile of unsorted CodeBlocks self.unsortedCodeBlockSet.add(candidate) else: @@ -344,29 +378,52 @@ def GetSortedCodeBlockList(self): return self.sortedCodeBlocks - if __name__ == "__main__": import argparse - parser = argparse.ArgumentParser(description='Parse an ITK source tree and run programs in order to make output files for Software Guide.') - parser.add_argument('--itkSourceDir', dest='itkSourceDir', action='store', default=None, - help='The path to the ITK source tree.') - parser.add_argument('--itkBuildDir', dest='itkBuildDir', action='store', default=None, - help='The path to the ITK build tree where test data is found.') - parser.add_argument('--itkExecDir', dest='itkExecDir', action='store', default=None, - help='The path to the ITK binary tree bin directory were executables are found.') - parser.add_argument('--SWGuidBaseOutput', dest='SWGuidBaseOutput', action='store', default=None, - help="The base directory of the output directory.") + parser = argparse.ArgumentParser( + description="Parse an ITK source tree and run programs in order to make output files for Software Guide." + ) + parser.add_argument( + "--itkSourceDir", + dest="itkSourceDir", + action="store", + default=None, + help="The path to the ITK source tree.", + ) + parser.add_argument( + "--itkBuildDir", + dest="itkBuildDir", + action="store", + default=None, + help="The path to the ITK build tree where test data is found.", + ) + parser.add_argument( + "--itkExecDir", + dest="itkExecDir", + action="store", + default=None, + help="The path to the ITK binary tree bin directory were executables are found.", + ) + parser.add_argument( + "--SWGuidBaseOutput", + dest="SWGuidBaseOutput", + action="store", + default=None, + help="The base directory of the output directory.", + ) args = parser.parse_args() itkExecutablesDir = os.path.realpath(args.itkExecDir) itkBuildDir = os.path.realpath(args.itkBuildDir) - pathFinder = ITKPathFinder(args.itkSourceDir, itkExecutablesDir, itkBuildDir, args.SWGuidBaseOutput) + pathFinder = ITKPathFinder( + args.itkSourceDir, itkExecutablesDir, itkBuildDir, args.SWGuidBaseOutput + ) allCommandBlocks = [] for rootDir, dirList, fileList in os.walk(args.itkSourceDir): - if rootDir.count('ThirdParty') >= 1: + if rootDir.count("ThirdParty") >= 1: # print("Passing on: {0}".format(rootDir)) continue @@ -374,7 +431,7 @@ def GetSortedCodeBlockList(self): if currFile[-4:] != ".cxx": # Only parse cxx files # print("NOT PARSING: {0} because it has wrong extension {1}".format(currFile,currFile[-r:])) continue - sourceFile = os.path.realpath(rootDir + '/' + currFile) + sourceFile = os.path.realpath(rootDir + "/" + currFile) # A dictionary indexed by starting line to the command blocks allCommandBlocks += ParseOneFile(sourceFile, pathFinder) @@ -404,33 +461,48 @@ def GetSortedCodeBlockList(self): # Now we warn if the input or output doesn't exist for outputFile in block.outputs: if not os.path.exists(outputFile): - print("WARNING: output {0} of {1} does not exist!".format(outputFile,baseProgramName)) + print( + "WARNING: output {0} of {1} does not exist!".format( + outputFile, baseProgramName + ) + ) for inputFile in block.inputs: if not os.path.exists(inputFile): - print("WARNING: input {0} of {1} does not exist!".format(inputFile,baseProgramName)) + print( + "WARNING: input {0} of {1} does not exist!".format( + inputFile, baseProgramName + ) + ) dependencyDictionary[baseProgramName].extend(block.outputs) for inputFile in block.inputs: # Only add pngs because imagemagick does not yet support metaimage if inputFile[-4:] == ".png": dependencyDictionary[baseProgramName].append(inputFile) - mkdir_p(os.path.join(args.SWGuidBaseOutput,'Examples')) - outputCMakeDependancies = os.path.join(args.SWGuidBaseOutput,'Examples',"GeneratedDependencies.cmake") - outputEPSDirectory = os.path.join(args.SWGuidBaseOutput,'Art','Generated') - mkdir_p(os.path.join(args.SWGuidBaseOutput,'Art','Generated')) + mkdir_p(os.path.join(args.SWGuidBaseOutput, "Examples")) + outputCMakeDependancies = os.path.join( + args.SWGuidBaseOutput, "Examples", "GeneratedDependencies.cmake" + ) + outputEPSDirectory = os.path.join(args.SWGuidBaseOutput, "Art", "Generated") + mkdir_p(os.path.join(args.SWGuidBaseOutput, "Art", "Generated")) - outputCDFile = open(outputCMakeDependancies, 'w') - allDependencies = 'set(allEPS-DEPS ' + outputCDFile = open(outputCMakeDependancies, "w") + allDependencies = "set(allEPS-DEPS " for baseName in dependencyDictionary.keys(): outstring = 'set("{name}-DEPS" '.format(name=baseName) - allDependencies += ' "${'+'{name}-DEPS'.format(name=baseName)+'}" ' + allDependencies += ' "${' + "{name}-DEPS".format(name=baseName) + '}" ' for output in dependencyDictionary[baseName]: - epsOutput = os.path.join(outputEPSDirectory, os.path.basename(output.replace('.png','.eps'))) - outstring += ' "{epsOutput}"'.format(epsOutput=epsOutput.replace('\\', '/')) - outputCDFile.write('CONVERT_INPUT_IMG("{0}" "{1}" "{2}")\n'.format(output.replace('\\', '/'), - epsOutput.replace('\\', '/'), "")) - outstring += ')\n' + epsOutput = os.path.join( + outputEPSDirectory, os.path.basename(output.replace(".png", ".eps")) + ) + outstring += ' "{epsOutput}"'.format(epsOutput=epsOutput.replace("\\", "/")) + outputCDFile.write( + 'CONVERT_INPUT_IMG("{0}" "{1}" "{2}")\n'.format( + output.replace("\\", "/"), epsOutput.replace("\\", "/"), "" + ) + ) + outstring += ")\n" outputCDFile.write(outstring) - allDependencies += ')\n' + allDependencies += ")\n" outputCDFile.write(allDependencies) outputCDFile.close()