diff --git a/tools/formatting/addSynopsis.py b/tools/formatting/addSynopsis.py index a88410b798..f24a55d3c2 100644 --- a/tools/formatting/addSynopsis.py +++ b/tools/formatting/addSynopsis.py @@ -23,12 +23,14 @@ def parseInterface(iFile, logFile=sys.stdout): import exceptions # removed the |(!?[ \t]*contains) - nameRe = re.compile('[ \t]*(((module)[ \t]*:?|subroutine|function|interface|(type)[ \t]*:*)[ \t]*([a-zA-Z0-9_]+)|(interface)|(end)[ \t]*([a-zA-Z_0-9]*)[ \t]*([a-zA-Z0-9_]*))' - , flags=re.IGNORECASE) - (kindPos,modulePos,typePos,namePos,emptyInterfacePos,endPos,endKind,) = (1,2,3,4,5,6,7,) + nameRe = re.compile( + '[ \t]*(((module)[ \t]*:?|subroutine|function|interface|(type)[ \t]*:*)[ \t]*([a-zA-Z0-9_]+)|(interface)|(end)[ \t]*([a-zA-Z_0-9]*)[ \t]*([a-zA-Z0-9_]*))', flags=re.IGNORECASE) + (kindPos, modulePos, typePos, namePos, emptyInterfacePos, + endPos, endKind,) = (1, 2, 3, 4, 5, 6, 7,) expansion = '' lineNr = 0 - result = {'interface': {},'type': {},'function': {},'subroutine': {},'module': {},} + result = {'interface': {}, 'type': {}, + 'function': {}, 'subroutine': {}, 'module': {}, } stack = [] while 1: line = iFile.readline() @@ -42,30 +44,38 @@ def parseInterface(iFile, logFile=sys.stdout): stack[0]['expansion'] = stack[0]['expansion'] + line elif match.groups()[namePos]: if match.groups()[modulePos]: # module def - if match.groups()[namePos] and match.groups()[namePos].lower() == 'procedure': # module procedure + # module procedure + if match.groups()[namePos] and match.groups()[namePos].lower() == 'procedure': if not stack: raise SyntaxError('empty stack') stack[0]['expansion'] = stack[0]['expansion'] + line else: if stack: - raise SyntaxError('stack non empty and module start') - stack = [{'name': match.groups()[namePos].lower(),'kind': 'module', 'expansion': line}] + raise SyntaxError( + 'stack non empty and module start') + stack = [{'name': match.groups()[namePos].lower( + ), 'kind': 'module', 'expansion': line}] elif match.groups()[typePos]: # type def - stack = [{'name': match.groups()[namePos].lower(), 'kind': 'type', 'expansion': line}] + stack + stack = [{'name': match.groups()[namePos].lower( + ), 'kind': 'type', 'expansion': line}] + stack else: # other def start - if not match.groups()[kindPos]: raise SyntaxError('undefined kind') - stack = [{'name': match.groups()[namePos].lower(), 'kind': match.groups()[kindPos].lower(), 'expansion': line}] + stack + if not match.groups()[kindPos]: + raise SyntaxError('undefined kind') + stack = [{'name': match.groups()[namePos].lower(), 'kind': match.groups()[ + kindPos].lower(), 'expansion': line}] + stack elif match.groups()[emptyInterfacePos]: # empty interfce begin - stack = [{'name': '', 'kind': 'interface', 'expansion': line}] + stack + stack = [{'name': '', 'kind': 'interface', + 'expansion': line}] + stack elif match.groups()[endPos]: # end of a def if not stack: raise SyntaxError('empty stack') if not match.groups()[endKind]: - raise SyntaxError('end without kind') # ignore this error?? + # ignore this error?? + raise SyntaxError('end without kind') elif not match.groups()[endKind].lower() == stack[0]['kind'].lower(): raise SyntaxError('mismatched end') # ignore this error?? stack[0]['expansion'] = stack[0]['expansion'] + line @@ -74,14 +84,17 @@ def parseInterface(iFile, logFile=sys.stdout): result[stack[0]['kind']] = {} kind_table = result[stack[0]['kind']] if kind_table.has_key(stack[0]['name']): - logFile.write("\nWARNING double definition of '"+ stack[0]['name'] + "' at line "+ str(lineNr) + ', ignoring\n') + logFile.write("\nWARNING double definition of '" + stack[0][ + 'name'] + "' at line " + str(lineNr) + ', ignoring\n') else: - kind_table[stack[0]['name']] = stack[0] # put only the expansion?? + kind_table[stack[0]['name']] = stack[ + 0] # put only the expansion?? else: logFile.write('ignoring group ' + str(stack[0]) + '\n') del stack[0] else: - raise SyntaxError("inconsistent parse '"+ str(match.groups()) + "'") + raise SyntaxError("inconsistent parse '" + + str(match.groups()) + "'") except SyntaxError: e = sys.exc_info()[1] e.lineno = lineNr @@ -90,7 +103,8 @@ def parseInterface(iFile, logFile=sys.stdout): raise except Warning: w = sys.exc_info()[1] - logFile.write('ignoring warning at line %d of file %s\n'%(lineNr, iFile.name)) + logFile.write('ignoring warning at line %d of file %s\n' % + (lineNr, iFile.name)) logFile.write(str(w)) return result @@ -108,7 +122,8 @@ def writeSynopsis(objDef, outfile, logFile=sys.stdout): while len(line) >= 74: import string posToBreak = string.rfind(line, ' ', 30, 74) - if posToBreak < 0: posToBreak = 74 + if posToBreak < 0: + posToBreak = 74 outfile.write('!! ' + line[:posToBreak] + '&\n') line = ' ' + line[posToBreak:] outfile.write('!! ' + line) @@ -119,7 +134,8 @@ def writeSynopsis(objDef, outfile, logFile=sys.stdout): else: logFile.write('no expansion for ' + str(objDef) + '\n') -def insertSynopsis(defs,infile,outfile,logFile=sys.stdout): + +def insertSynopsis(defs, infile, outfile, logFile=sys.stdout): """Writes the file infile to outfile inserting the synopsis defined in defs. infile and outfile should be file objects (streams). @@ -140,7 +156,8 @@ def insertSynopsis(defs,infile,outfile,logFile=sys.stdout): (startStopPos, directivePos, namePos, genericCommentPos) = (1, 2, 3, 4) # 0: not in comment, 1: in roboDoc comment, synopsis not yet inserted 2: reading name, - # 3: in roboDoc comment, synopsis inserted, 4: skipping section (synopsis inserted) + # 3: in roboDoc comment, synopsis inserted, 4: skipping section (synopsis + # inserted) status = 0 lineNr = 0 @@ -158,7 +175,8 @@ def insertSynopsis(defs,infile,outfile,logFile=sys.stdout): if status == 1: # already in comment: end if name: if not defs.has_key(name): - e = SyntaxWarning("ignoring unknown object with name '" + name + "'") + e = SyntaxWarning( + "ignoring unknown object with name '" + name + "'") outfile.write(line.lstrip()) status = 0 raise e @@ -179,16 +197,19 @@ def insertSynopsis(defs,infile,outfile,logFile=sys.stdout): # not in comment status = 1 name = '' - if not match.groups()[startStopPos]: # only 3 *, start should have 4 + # only 3 *, start should have 4 + if not match.groups()[startStopPos]: outfile.write(line.lstrip()) raise SyntaxWarning('incorrect start of comment') else: status = 0 outfile.write(line.lstrip()) - raise SyntaxWarning('internal error, status=' + str(status)) + raise SyntaxWarning( + 'internal error, status=' + str(status)) outfile.write(line.lstrip()) elif match.groups()[directivePos]: - if status == 4: status = 3 + if status == 4: + status = 3 if status == 1 and name and defs.has_key(name) and defs[name]: # writeSynopsis(defs[name],outfile) status = 3 @@ -228,11 +249,12 @@ def insertSynopsis(defs,infile,outfile,logFile=sys.stdout): raise except Warning: w = sys.exc_info()[1] - logFile.write("ignoring warning at line %d of file '%s'\n" % (lineNr, infile.name)) + logFile.write("ignoring warning at line %d of file '%s'\n" % + (lineNr, infile.name)) logFile.write(str(w) + '\n') -def addSynopsisToFile(ifile,sfile,outfile,logFile=sys.stdout): +def addSynopsisToFile(ifile, sfile, outfile, logFile=sys.stdout): """Adds the synopsis to the file sfile, reading the interface from ifile, and writing the result to outfile""" @@ -254,7 +276,7 @@ def addSynopsisToFile(ifile,sfile,outfile,logFile=sys.stdout): insertSynopsis(defs, sfile, outfile, logFile) -def addSynopsisInDir(interfaceDir,outDir,filePaths,logFile=sys.stdout,): +def addSynopsisInDir(interfaceDir, outDir, filePaths, logFile=sys.stdout,): """Add the synopsis to the files in filePaths, reading the interfaces from interfaceDir, and writing them to outDir. interfaceDir should be the path of the directory where the .int interface files are. @@ -271,7 +293,7 @@ def addSynopsisInDir(interfaceDir,outDir,filePaths,logFile=sys.stdout,): fileName = fileName[:string.rfind(fileName, '.')] interfaceFilePath = interfaceDir + '/' + fileName + '.int' outFilePath = os.path.join(outDir, - os.path.basename(sourceFilePath)) + os.path.basename(sourceFilePath)) logFile.write('=== began brocessing of' + sourceFilePath + '\n') ifile = open(interfaceFilePath, 'r') sfile = open(sourceFilePath, 'r') diff --git a/tools/formatting/normalizeFortranFile.py b/tools/formatting/normalizeFortranFile.py index b49c2c29ff..d637ceada5 100644 --- a/tools/formatting/normalizeFortranFile.py +++ b/tools/formatting/normalizeFortranFile.py @@ -8,22 +8,25 @@ try: except ImportError: from io import StringIO -rUse=0 -rVar=0 -varRe=re.compile(r" *(?P[a-zA-Z_0-9]+) *(?P(?:\((?P(?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\))? *(?:= *(?P(:?[^\"',()]+|\((?:[^()\"']+|\([^()\"']*\)|\"[^\"]*\"|'[^']*')*\)|\"[^\"]*\"|'[^']*')+))?)? *(?:(?P,)|\n?) *",re.IGNORECASE) -useParseRe=re.compile( +rUse = 0 +rVar = 0 +varRe = re.compile(r" *(?P[a-zA-Z_0-9]+) *(?P(?:\((?P(?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\))? *(?:= *(?P(:?[^\"',()]+|\((?:[^()\"']+|\([^()\"']*\)|\"[^\"]*\"|'[^']*')*\)|\"[^\"]*\"|'[^']*')+))?)? *(?:(?P,)|\n?) *", re.IGNORECASE) +useParseRe = re.compile( r" *use +(?P[a-zA-Z_][a-zA-Z_0-9]*)(?P *, *only *:)? *(?P.*)$", flags=re.IGNORECASE) -commonUsesRe=re.compile("^#include *\"([^\"]*(cp_common_uses.f90|base_uses.f90))\"") -localNameRe=re.compile(" *(?P[a-zA-Z_0-9]+)(?: *= *> *[a-zA-Z_0-9]+)? *$") -typeRe=re.compile(r" *(?Pinteger(?: *\* *[0-9]+)?|logical|character(?: *\* *[0-9]+)?|real(?: *\* *[0-9]+)?|complex(?: *\* *[0-9]+)?|type) *(?P\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))? *(?P(?: *, *[a-zA-Z_0-9]+(?: *\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))?)+)? *(?P::)?(?P[^\n]+)\n?",re.IGNORECASE)#$ -indentSize=2 -decllinelength=100 -decloffset=50 +commonUsesRe = re.compile( + "^#include *\"([^\"]*(cp_common_uses.f90|base_uses.f90))\"") +localNameRe = re.compile( + " *(?P[a-zA-Z_0-9]+)(?: *= *> *[a-zA-Z_0-9]+)? *$") +typeRe = re.compile(r" *(?Pinteger(?: *\* *[0-9]+)?|logical|character(?: *\* *[0-9]+)?|real(?: *\* *[0-9]+)?|complex(?: *\* *[0-9]+)?|type) *(?P\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))? *(?P(?: *, *[a-zA-Z_0-9]+(?: *\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))?)+)? *(?P::)?(?P[^\n]+)\n?", re.IGNORECASE) # $ +indentSize = 2 +decllinelength = 100 +decloffset = 50 -ompDirRe = re.compile(r"^\s*(!\$omp)",re.IGNORECASE) +ompDirRe = re.compile(r"^\s*(!\$omp)", re.IGNORECASE) ompRe = re.compile(r"^\s*(!\$)", re.IGNORECASE) + class CharFilter(object): """ An iterator to wrap the iterator returned by `enumerate` @@ -58,10 +61,12 @@ class CharFilter(object): def next(self): return self.__next__() + class InputStream(object): """ Class to read logical Fortran lines from a Fortran file. """ + def __init__(self, infile): self.line_buffer = deque([]) self.infile = infile @@ -72,17 +77,18 @@ class InputStream(object): returns a touple with the joined line, and a list with the original lines. Doesn't support multiline character constants! """ - lineRe=re.compile( - r"(?:(?P#.*\n?)| *(&)?(?P(?:!\$|[^&!\"']+|\"[^\"]*\"|'[^']*')*)(?P&)? *(?P!.*)?\n?)",#$ + lineRe = re.compile( + # $ + r"(?:(?P#.*\n?)| *(&)?(?P(?:!\$|[^&!\"']+|\"[^\"]*\"|'[^']*')*)(?P&)? *(?P!.*)?\n?)", re.IGNORECASE) - joinedLine="" - comments=[] - lines=[] - continuation=0 + joinedLine = "" + comments = [] + lines = [] + continuation = 0 while 1: if not self.line_buffer: - line=self.infile.readline().replace("\t",8*" ") + line = self.infile.readline().replace("\t", 8 * " ") self.line_nr += 1 # convert OMP-conditional fortran statements into normal fortran statements # but remember to convert them back @@ -95,324 +101,350 @@ class InputStream(object): line_start = 0 for pos, char in CharFilter(enumerate(line)): if char == ';' or pos + 1 == len(line): - self.line_buffer.append(omp_indent*' ' + '!$'*is_omp_conditional + - line[line_start:pos+1]) + self.line_buffer.append(omp_indent * ' ' + '!$' * is_omp_conditional + + line[line_start:pos + 1]) omp_indent = 0 is_omp_conditional = False - line_start = pos+1 - if(line_start < len(line)): + line_start = pos + 1 + if(line_start < len(line)): # line + comment - self.line_buffer.append('!$'*is_omp_conditional + + self.line_buffer.append('!$' * is_omp_conditional + line[line_start:]) if self.line_buffer: line = self.line_buffer.popleft() - if not line: break + if not line: + break lines.append(line) - m=lineRe.match(line) - if not m or m.span()[1]!=len(line): - # FIXME: does not handle line continuation of + m = lineRe.match(line) + if not m or m.span()[1] != len(line): + # FIXME: does not handle line continuation of # omp conditional fortran statements # starting with an ampersand. - raise SyntaxError("unexpected line format:"+repr(line)) + raise SyntaxError("unexpected line format:" + repr(line)) if m.group("preprocessor"): - if len(lines)>1: - raise SyntaxError("continuation to a preprocessor line not supported "+repr(line)) + if len(lines) > 1: + raise SyntaxError( + "continuation to a preprocessor line not supported " + repr(line)) comments.append(line) break - coreAtt=m.group("core") + coreAtt = m.group("core") if ompRe.match(coreAtt) and joinedLine.strip(): # remove omp '!$' for line continuation coreAtt = ompRe.sub('', coreAtt, count=1).lstrip() joinedLine = joinedLine.rstrip("\n") + coreAtt - if coreAtt and not coreAtt.isspace(): continuation=0 - if m.group("continue"): continuation=1 + if coreAtt and not coreAtt.isspace(): + continuation = 0 + if m.group("continue"): + continuation = 1 if m.group("comment"): comments.append(m.group("comment")) else: comments.append('') - if not continuation: break - return (joinedLine,comments,lines) + if not continuation: + break + return (joinedLine, comments, lines) + def parseRoutine(inFile): """Parses a routine""" - startRe=re.compile(r" *(?:recursive +|pure +|elemental +)*(?:subroutine|function)",re.IGNORECASE) - endRe=re.compile(r" *end\s*(?:subroutine|function)",re.IGNORECASE) - startRoutineRe=re.compile(r" *(?:recursive +|pure +|elemental +)*(?Psubroutine|function) +(?P[a-zA-Z_][a-zA-Z_0-9]*) *(?:\((?P[^()]*)\))? *(?:result *\( *(?P[a-zA-Z_][a-zA-Z_0-9]*) *\))? *(?:bind *\([^()]+\))? *\n?",re.IGNORECASE)#$ - typeBeginRe=re.compile(r" *(?Pinteger(?: *\* *[0-9]+)?|logical|character(?: *\* *[0-9]+)?|real(?: *\* *[0-9]+)?|complex(?: *\* *[0-9]+)?|type)[,( ]", - re.IGNORECASE) - attributeRe=re.compile(r" *, *(?P[a-zA-Z_0-9]+) *(?:\( *(?P(?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\))? *",re.IGNORECASE) - ignoreRe=re.compile(r" *(?:|implicit +none *)$",re.IGNORECASE) - interfaceStartRe=re.compile(r" *interface *$",re.IGNORECASE) - interfaceEndRe=re.compile(r" *end +interface *$",re.IGNORECASE) - routine={'preRoutine':[], - 'core':[], - 'strippedCore':[], - 'begin':[], - 'end':[], - 'preDeclComments':[], - 'declarations':[], - 'declComments':[], - 'parsedDeclarations':[], - 'postRoutine':[], - 'kind':None,'name':None,'arguments':None,'result':None, - 'interfaceCount':0, - 'use':[] - } - includeRe=re.compile(r"#? *include +[\"'](?P.+)[\"'] *$",re.IGNORECASE) + startRe = re.compile( + r" *(?:recursive +|pure +|elemental +)*(?:subroutine|function)", re.IGNORECASE) + endRe = re.compile(r" *end\s*(?:subroutine|function)", re.IGNORECASE) + startRoutineRe = re.compile( + r" *(?:recursive +|pure +|elemental +)*(?Psubroutine|function) +(?P[a-zA-Z_][a-zA-Z_0-9]*) *(?:\((?P[^()]*)\))? *(?:result *\( *(?P[a-zA-Z_][a-zA-Z_0-9]*) *\))? *(?:bind *\([^()]+\))? *\n?", re.IGNORECASE) # $ + typeBeginRe = re.compile(r" *(?Pinteger(?: *\* *[0-9]+)?|logical|character(?: *\* *[0-9]+)?|real(?: *\* *[0-9]+)?|complex(?: *\* *[0-9]+)?|type)[,( ]", + re.IGNORECASE) + attributeRe = re.compile( + r" *, *(?P[a-zA-Z_0-9]+) *(?:\( *(?P(?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*)\))? *", re.IGNORECASE) + ignoreRe = re.compile(r" *(?:|implicit +none *)$", re.IGNORECASE) + interfaceStartRe = re.compile(r" *interface *$", re.IGNORECASE) + interfaceEndRe = re.compile(r" *end +interface *$", re.IGNORECASE) + routine = {'preRoutine': [], + 'core': [], + 'strippedCore': [], + 'begin': [], + 'end': [], + 'preDeclComments': [], + 'declarations': [], + 'declComments': [], + 'parsedDeclarations': [], + 'postRoutine': [], + 'kind': None, 'name': None, 'arguments': None, 'result': None, + 'interfaceCount': 0, + 'use': [] + } + includeRe = re.compile( + r"#? *include +[\"'](?P.+)[\"'] *$", re.IGNORECASE) stream = InputStream(inFile) while 1: - (jline,_,lines)=stream.nextFortranLine() - if len(lines)==0: break - if startRe.match(jline):break + (jline, _, lines) = stream.nextFortranLine() + if len(lines) == 0: + break + if startRe.match(jline): + break routine['preRoutine'].extend(lines) - m=includeRe.match(lines[0]) + m = includeRe.match(lines[0]) if m: try: - subF=file(m.group('file')) + subF = file(m.group('file')) subStream = InputStream(subF) while 1: - (subjline,_,sublines)=subStream.nextFortranLine() + (subjline, _, sublines) = subStream.nextFortranLine() if not sublines: break routine['strippedCore'].append(subjline) subF.close() except: import traceback - print("error trying to follow include ",m.group('file')) + print("error trying to follow include ", m.group('file')) print("warning this might lead to the removal of used variables") traceback.print_exc() if jline: - routine['begin']=lines - m=startRoutineRe.match(jline) - if not m or m.span()[1]!=len(jline): - raise SyntaxError("unexpected subroutine start format:"+repr(lines)) - routine['name']=m.group('name') - routine['kind']=m.group('kind') + routine['begin'] = lines + m = startRoutineRe.match(jline) + if not m or m.span()[1] != len(jline): + raise SyntaxError( + "unexpected subroutine start format:" + repr(lines)) + routine['name'] = m.group('name') + routine['kind'] = m.group('kind') if (m.group('arguments') and m.group('arguments').strip()): - routine['arguments']=map(lambda x: x.strip(), - m.group('arguments').split(",")) + routine['arguments'] = map(lambda x: x.strip(), + m.group('arguments').split(",")) if (m.group('result')): - routine['result']=m.group('result') - if (not routine['result'])and(routine['kind'].lower()=="function"): - routine['result']=routine['name'] + routine['result'] = m.group('result') + if (not routine['result'])and(routine['kind'].lower() == "function"): + routine['result'] = routine['name'] while 1: - (jline,comment_list,lines)=stream.nextFortranLine() + (jline, comment_list, lines) = stream.nextFortranLine() comments = '\n'.join(_ for _ in comment_list if _) - if len(lines)==0: break - if lines[0].lower().startswith("#include"): break + if len(lines) == 0: + break + if lines[0].lower().startswith("#include"): + break if not ignoreRe.match(jline): if typeBeginRe.match(jline): - m=typeRe.match(jline) - if (m.group('type').lower()=='type' and - not m.group('parameters')): + m = typeRe.match(jline) + if (m.group('type').lower() == 'type' and + not m.group('parameters')): break - if not m or m.span()[1]!=len(jline): - raise SyntaxError("unexpected type format:"+repr(jline)) - decl={'type':m.group("type"), - 'parameters':None, - 'attributes':[], - 'vars':[]} + if not m or m.span()[1] != len(jline): + raise SyntaxError("unexpected type format:" + repr(jline)) + decl = {'type': m.group("type"), + 'parameters': None, + 'attributes': [], + 'vars': []} if m.group('parameters'): - decl['parameters']=(m.group("parameters").replace(" ",""). - replace(",",", ")) - str=m.group("attributes") + decl['parameters'] = (m.group("parameters").replace(" ", ""). + replace(",", ", ")) + str = m.group("attributes") while(str): - m2=attributeRe.match(str) + m2 = attributeRe.match(str) if not m2: - raise SyntaxError("unexpected attribute format "+ - repr(str)+" in "+repr(lines)) - decl['attributes'].append(m2.group().replace(" ",""). - replace(",",", ")[2:]) - str=str[m2.span()[1]:] - str=m.group("vars") + raise SyntaxError("unexpected attribute format " + + repr(str) + " in " + repr(lines)) + decl['attributes'].append(m2.group().replace(" ", ""). + replace(",", ", ")[2:]) + str = str[m2.span()[1]:] + str = m.group("vars") while 1: - m2=varRe.match(str) + m2 = varRe.match(str) if not m2: - raise SyntaxError("unexpected var format "+ - repr(str)+" in "+repr(lines)) - var=m2.group("var") - if m2.group("param"):var+="("+m2.group("param")+")" + raise SyntaxError("unexpected var format " + + repr(str) + " in " + repr(lines)) + var = m2.group("var") + if m2.group("param"): + var += "(" + m2.group("param") + ")" if m2.group("value"): - var+=" = " - var+=m2.group("value") + var += " = " + var += m2.group("value") decl['vars'].append(var) - str=str[m2.span()[1]:] + str = str[m2.span()[1]:] if not m2.group("continue"): if str: - raise SyntaxError("error parsing vars (leftover="+ - repr(str)+") in "+repr(lines)) + raise SyntaxError("error parsing vars (leftover=" + + repr(str) + ") in " + repr(lines)) break routine['parsedDeclarations'].append(decl) elif interfaceStartRe.match(jline): - istart=lines - interfaceDeclFile=StringIO() + istart = lines + interfaceDeclFile = StringIO() while 1: - (jline,_,lines)=stream.nextFortranLine() + (jline, _, lines) = stream.nextFortranLine() if interfaceEndRe.match(jline): - iend=lines + iend = lines break interfaceDeclFile.writelines(lines) - interfaceDeclFile=StringIO(interfaceDeclFile.getvalue()) - iroutines=[] + interfaceDeclFile = StringIO(interfaceDeclFile.getvalue()) + iroutines = [] while 1: - iroutine=parseRoutine(interfaceDeclFile) + iroutine = parseRoutine(interfaceDeclFile) if not iroutine['kind']: - if len(iroutines)==0: + if len(iroutines) == 0: interfaceDeclFile.seek(0) - raise SyntaxError("error parsing interface:"+ + raise SyntaxError("error parsing interface:" + repr(interfaceDeclFile.read())) - iroutines[-1]['postRoutine'].extend(iroutine['preRoutine']) + iroutines[-1]['postRoutine'].extend( + iroutine['preRoutine']) break iroutines.append(iroutine) for iroutine in iroutines: - routine['interfaceCount']+=1 - decl={'type':'z_interface%02d'%(routine['interfaceCount']), - 'parameters':None, - 'attributes':[], - 'vars':[iroutine['name']], - 'iroutine':iroutine, - 'istart':istart, - 'iend':iend - } + routine['interfaceCount'] += 1 + decl = {'type': 'z_interface%02d' % (routine['interfaceCount']), + 'parameters': None, + 'attributes': [], + 'vars': [iroutine['name']], + 'iroutine': iroutine, + 'istart': istart, + 'iend': iend + } routine['parsedDeclarations'].append(decl) elif useParseRe.match(jline): routine['use'].append("".join(lines)) else: break routine['declarations'].append("".join(lines)) - if (len(routine['parsedDeclarations'])==0 and len(routine['use'])==0 and - not re.match(" *implicit +none *$",jline,re.IGNORECASE)): + if (len(routine['parsedDeclarations']) == 0 and len(routine['use']) == 0 and + not re.match(" *implicit +none *$", jline, re.IGNORECASE)): routine['preDeclComments'].append("".join(lines)) elif comments: routine['declComments'].append(comments) - containsRe=re.compile(r" *contains *$",re.IGNORECASE) + containsRe = re.compile(r" *contains *$", re.IGNORECASE) - while len(lines)>0: + while len(lines) > 0: if endRe.match(jline): - routine['end']=lines + routine['end'] = lines break routine['strippedCore'].append(jline) routine['core'].append("".join(lines)) if containsRe.match(lines[0]): break - m=includeRe.match(lines[0]) + m = includeRe.match(lines[0]) if m: try: - subF=file(m.group('file')) + subF = file(m.group('file')) subStream = InputStream(subF) while 1: - (subjline,_,sublines)=subStream.nextFortranLine() + (subjline, _, sublines) = subStream.nextFortranLine() if not sublines: break routine['strippedCore'].append(subjline) subF.close() except: import traceback - print("error trying to follow include ",m.group('file')) + print("error trying to follow include ", m.group('file')) print("warning this might lead to the removal of used variables") traceback.print_exc() - (jline,_,lines)=stream.nextFortranLine() + (jline, _, lines) = stream.nextFortranLine() return routine -def findWord(word,text,options=re.IGNORECASE): + +def findWord(word, text, options=re.IGNORECASE): """Returns the position of word in text or -1 if not found. A match is valid only if it is a whole word (i.e. findWord('try','retry') returns false)""" - wordRe=re.compile("(?100000: + ii += 1 + if ii > 100000: raise Error("could not enforce all constraints") - m=varRe.match(declarations[idecl2]['vars'][ivar2]) - if (ivar==0 and - findWord(m.group('var').lower(),typeParam)!=-1): - declarations.insert(idecl2+1,declarations[idecl]) + m = varRe.match(declarations[idecl2]['vars'][ivar2]) + if (ivar == 0 and + findWord(m.group('var').lower(), typeParam) != -1): + declarations.insert( + idecl2 + 1, declarations[idecl]) del declarations[idecl] - ivar=0 - moved=1 + ivar = 0 + moved = 1 break - if rest and findWord(m.group('var').lower(),rest)!=-1: - if len(declarations[idecl]['vars'])>1: - newDecl={} + if rest and findWord(m.group('var').lower(), rest) != -1: + if len(declarations[idecl]['vars']) > 1: + newDecl = {} newDecl.update(declarations[idecl]) - newDecl['vars']=[declarations[idecl]['vars'][ivar]] - declarations.insert(idecl2+1,newDecl) + newDecl['vars'] = [ + declarations[idecl]['vars'][ivar]] + declarations.insert(idecl2 + 1, newDecl) del declarations[idecl]['vars'][ivar] else: - declarations.insert(idecl2+1, + declarations.insert(idecl2 + 1, declarations[idecl]) del declarations[idecl] - ivar=0 - moved=1 + ivar = 0 + moved = 1 break if moved: break - if not moved: ivar+=1 - idecl+=1 + if not moved: + ivar += 1 + idecl += 1 - for i in range(len(declarations)-1,0,-1): - if (declarations[i]['normalizedType'].lower()== - declarations[i-1]['normalizedType'].lower()): - declarations[i-1]['vars'].extend(declarations[i]['vars']) + for i in range(len(declarations) - 1, 0, -1): + if (declarations[i]['normalizedType'].lower() == + declarations[i - 1]['normalizedType'].lower()): + declarations[i - 1]['vars'].extend(declarations[i]['vars']) del declarations[i] + def sortDeclarations(declarations): """sorts, compacts declarations and respects dependencies normalizedType has to be defined for the declarations""" - declarations.sort(lambda x,y:cmp(x['normalizedType'].lower(), - y['normalizedType'].lower())) + declarations.sort(lambda x, y: cmp(x['normalizedType'].lower(), + y['normalizedType'].lower())) - for i in range(len(declarations)-1,0,-1): - if (declarations[i]['normalizedType'].lower()== - declarations[i-1]['normalizedType'].lower()): - declarations[i-1]['vars'].extend(declarations[i]['vars']) + for i in range(len(declarations) - 1, 0, -1): + if (declarations[i]['normalizedType'].lower() == + declarations[i - 1]['normalizedType'].lower()): + declarations[i - 1]['vars'].extend(declarations[i]['vars']) del declarations[i] for decl in declarations: - decl['vars'].sort(lambda x,y:cmp(x.lower(),y.lower())) + decl['vars'].sort(lambda x, y: cmp(x.lower(), y.lower())) enforceDeclDependecies(declarations) + def writeRoutine(routine, outFile): """writes the given routine to outFile""" outFile.writelines(routine["preRoutine"]) @@ -422,55 +454,57 @@ def writeRoutine(routine, outFile): outFile.writelines(routine["end"]) outFile.writelines(routine["postRoutine"]) -def writeInCols(dLine,indentCol,maxCol,indentAtt,file): + +def writeInCols(dLine, indentCol, maxCol, indentAtt, file): """writes out the strings (trying not to cut them) in dLine up to maxCol indenting each newline with indentCol. The '&' of the continuation line is at maxCol. indentAtt is the actual intent, and the new indent is returned""" - strRe=re.compile(r"('[^'\n]*'|\"[^\"\n]*\")") - nonWordRe=re.compile(r"(\(/|/\)|[^-+a-zA-Z0-9_.])") - maxSize=maxCol-indentCol-1 - tol=min(maxSize/6,6)+indentCol + strRe = re.compile(r"('[^'\n]*'|\"[^\"\n]*\")") + nonWordRe = re.compile(r"(\(/|/\)|[^-+a-zA-Z0-9_.])") + maxSize = maxCol - indentCol - 1 + tol = min(maxSize / 6, 6) + indentCol for fragment in dLine: - if indentAtt+len(fragment)0: - decl = " "*indentSize*2+d['type'] - if d['parameters']: # do not drop empty parameter lists? + if len(d['vars']) > 0: + decl = " " * indentSize * 2 + d['type'] + if d['parameters']: # do not drop empty parameter lists? decl += d['parameters'] if d['attributes']: for a in d['attributes']: @@ -481,279 +515,298 @@ def writeCompactDeclaration(declaration,file): for var in d['vars']: cur_len = sum([len(l) for l in dLine]) if(len(dLine) > 1 and cur_len + len(var) > 600): - writeInCols(dLine,3*indentSize,decllinelength,0,file) + writeInCols(dLine, 3 * indentSize, decllinelength, 0, file) file.write("\n") dLine = [decl] if(len(dLine) > 1): dLine[-1] += ", " dLine.append(var) - writeInCols(dLine,3*indentSize,decllinelength,0,file) + writeInCols(dLine, 3 * indentSize, decllinelength, 0, file) file.write("\n") -def writeExtendedDeclaration(declaration,file): + +def writeExtendedDeclaration(declaration, file): """Writes a declaration in a nicer way (using more space)""" - d=declaration - if len(d['vars'])==0: return + d = declaration + if len(d['vars']) == 0: + return if d.has_key('iroutine'): file.writelines(d['istart']) - writeRoutine(d['iroutine'],file) + writeRoutine(d['iroutine'], file) file.writelines(d['iend']) else: - dLine=[] - dLine.append(" "*indentSize*2+d['type']) - if d['parameters']: # do not drop empty parameter lists? + dLine = [] + dLine.append(" " * indentSize * 2 + d['type']) + if d['parameters']: # do not drop empty parameter lists? dLine.append(d['parameters']) if d['attributes']: for a in d['attributes']: - dLine[-1:]=[dLine[-1]+", "] + dLine[-1:] = [dLine[-1] + ", "] dLine.append(a) - indentAtt=writeInCols(dLine,3*indentSize,decloffset+1 + 2*indentSize,0,file) - file.write(" "*(decloffset+2*indentSize-indentAtt)) + indentAtt = writeInCols(dLine, 3 * indentSize, + decloffset + 1 + 2 * indentSize, 0, file) + file.write(" " * (decloffset + 2 * indentSize - indentAtt)) file.write(" :: ") - indentAtt=decloffset+8 + indentAtt = decloffset + 8 - dLine=[] + dLine = [] for var in d['vars'][:-1]: - dLine.append(var+", ") + dLine.append(var + ", ") dLine.append(d['vars'][-1]) - writeInCols(dLine,decloffset+4+2*indentSize,decllinelength,indentAtt,file) + writeInCols(dLine, decloffset + 4 + 2 * indentSize, + decllinelength, indentAtt, file) file.write("\n") -def writeDeclarations(parsedDeclarations,file): + +def writeDeclarations(parsedDeclarations, file): """Writes the declarations to the given file""" for d in parsedDeclarations: - maxLenVar=0 - totalLen=0 + maxLenVar = 0 + totalLen = 0 for v in d['vars']: - maxLenVar=max(maxLenVar,len(v)) - totalLen+=len(v) - if maxLenVar>30 or totalLen>decllinelength-4: - writeCompactDeclaration(d,file) + maxLenVar = max(maxLenVar, len(v)) + totalLen += len(v) + if maxLenVar > 30 or totalLen > decllinelength - 4: + writeCompactDeclaration(d, file) else: - writeExtendedDeclaration(d,file) + writeExtendedDeclaration(d, file) -def cleanDeclarations(routine,logFile=sys.stdout): + +def cleanDeclarations(routine, logFile=sys.stdout): """cleans up the declaration part of the given parsed routine removes unused variables""" global rVar - containsRe=re.compile(r" *contains *$",re.IGNORECASE) + containsRe = re.compile(r" *contains *$", re.IGNORECASE) if routine['core']: if containsRe.match(routine['core'][-1]): - logFile.write("*** routine %s contains other routines ***\n*** declarations not cleaned ***\n"% - (routine['name'])) + logFile.write("*** routine %s contains other routines ***\n*** declarations not cleaned ***\n" % + (routine['name'])) return - commentToRemoveRe=re.compile(r" *! *(?:interface|arguments|parameters|locals?|\** *local +variables *\**|\** *local +parameters *\**) *$",re.IGNORECASE) - nullifyRe=re.compile(r" *nullify *\(([^()]+)\) *\n?",re.IGNORECASE|re.MULTILINE) + commentToRemoveRe = re.compile( + r" *! *(?:interface|arguments|parameters|locals?|\** *local +variables *\**|\** *local +parameters *\**) *$", re.IGNORECASE) + nullifyRe = re.compile( + r" *nullify *\(([^()]+)\) *\n?", re.IGNORECASE | re.MULTILINE) - if not routine['kind']: return + if not routine['kind']: + return if (routine['core']): - if re.match(" *type *[a-zA-Z_]+ *$",routine['core'][0],re.IGNORECASE): - logFile.write("*** routine %s contains local types, not fully cleaned ***\n"% - (routine['name'])) - if re.match(" *import+ *$",routine['core'][0],re.IGNORECASE): - logFile.write("*** routine %s contains import, not fully cleaned ***\n"% - (routine['name'])) - if re.search("^#","".join(routine['declarations']),re.MULTILINE): - logFile.write("*** routine %s declarations contain preprocessor directives ***\n*** declarations not cleaned ***\n"%( + if re.match(" *type *[a-zA-Z_]+ *$", routine['core'][0], re.IGNORECASE): + logFile.write("*** routine %s contains local types, not fully cleaned ***\n" % + (routine['name'])) + if re.match(" *import+ *$", routine['core'][0], re.IGNORECASE): + logFile.write("*** routine %s contains import, not fully cleaned ***\n" % + (routine['name'])) + if re.search("^#", "".join(routine['declarations']), re.MULTILINE): + logFile.write("*** routine %s declarations contain preprocessor directives ***\n*** declarations not cleaned ***\n" % ( routine['name'])) return try: - rest="".join(routine['strippedCore']).lower() - nullifys=",".join(nullifyRe.findall(rest)) - rest=nullifyRe.sub("",rest) - paramDecl=[] - decls=[] + rest = "".join(routine['strippedCore']).lower() + nullifys = ",".join(nullifyRe.findall(rest)) + rest = nullifyRe.sub("", rest) + paramDecl = [] + decls = [] for d in routine['parsedDeclarations']: - d['normalizedType']=d['type'] + d['normalizedType'] = d['type'] if d['parameters']: - d['normalizedType']+=d['parameters'] + d['normalizedType'] += d['parameters'] if (d["attributes"]): - d['attributes'].sort(lambda x,y:cmp(x.lower(),y.lower())) - d['normalizedType']+=', ' - d['normalizedType']+=', '.join(d['attributes']) - if "parameter" in map(str.lower,d['attributes']): + d['attributes'].sort(lambda x, y: cmp(x.lower(), y.lower())) + d['normalizedType'] += ', ' + d['normalizedType'] += ', '.join(d['attributes']) + if "parameter" in map(str.lower, d['attributes']): paramDecl.append(d) else: decls.append(d) sortDeclarations(paramDecl) sortDeclarations(decls) - has_routinen=0 - pos_routinep=-1 + has_routinen = 0 + pos_routinep = -1 for d in paramDecl: for i in xrange(len(d['vars'])): - v=d['vars'][i] - m=varRe.match(v) - lowerV=m.group("var").lower() - if lowerV=="routinen": - has_routinen=1 - d['vars'][i]="routineN = '"+routine['name']+"'" - elif lowerV=="routinep": - pos_routinep=i - d['vars'][i]="routineP = moduleN//':'//routineN" - if not has_routinen and pos_routinep>=0: - d['vars'].insert(pos_routinep,"routineN = '"+routine['name']+"'") - + v = d['vars'][i] + m = varRe.match(v) + lowerV = m.group("var").lower() + if lowerV == "routinen": + has_routinen = 1 + d['vars'][i] = "routineN = '" + routine['name'] + "'" + elif lowerV == "routinep": + pos_routinep = i + d['vars'][i] = "routineP = moduleN//':'//routineN" + if not has_routinen and pos_routinep >= 0: + d['vars'].insert( + pos_routinep, "routineN = '" + routine['name'] + "'") if routine['arguments']: - routine['lowercaseArguments']=map(lambda x:x.lower(),routine['arguments']) + routine['lowercaseArguments'] = map( + lambda x: x.lower(), routine['arguments']) else: - routine['lowercaseArguments']=[] - if routine['result']: routine['lowercaseArguments'].append(routine['result'].lower()) - argDeclDict={} - localDecl=[] + routine['lowercaseArguments'] = [] + if routine['result']: + routine['lowercaseArguments'].append(routine['result'].lower()) + argDeclDict = {} + localDecl = [] for d in decls: - localD={} + localD = {} localD.update(d) - localD['vars']=[] - argD=None + localD['vars'] = [] + argD = None for v in d['vars']: - m=varRe.match(v) - lowerV=m.group("var").lower() + m = varRe.match(v) + lowerV = m.group("var").lower() if lowerV in routine['lowercaseArguments']: - argD={} + argD = {} argD.update(d) - argD['vars']=[v] + argD['vars'] = [v] if argDeclDict.has_key(lowerV): raise SyntaxError( - "multiple declarations not supported. var="+v+ - " declaration="+str(d)+"routine="+routine['name']) - argDeclDict[lowerV]=argD + "multiple declarations not supported. var=" + v + + " declaration=" + str(d) + "routine=" + routine['name']) + argDeclDict[lowerV] = argD else: - pos=findWord(lowerV,rest) - if (pos!=-1): + pos = findWord(lowerV, rest) + if (pos != -1): localD['vars'].append(v) else: - if findWord(lowerV,nullifys)!=-1: - if not rmNullify(lowerV,routine['core']): + if findWord(lowerV, nullifys) != -1: + if not rmNullify(lowerV, routine['core']): raise SyntaxError( - "could not remove nullify of "+lowerV+ - " as expected, routine="+routine['name']) + "could not remove nullify of " + lowerV + + " as expected, routine=" + routine['name']) logFile.write("removed var %s in routine %s\n" % - (lowerV,routine['name'])) - rVar+=1 + (lowerV, routine['name'])) + rVar += 1 if (len(localD['vars'])): localDecl.append(localD) - argDecl=[] + argDecl = [] for arg in routine['lowercaseArguments']: if argDeclDict.has_key(arg): argDecl.append(argDeclDict[arg]) else: - print("warning, implicitly typed argument '",arg,"' in routine",routine['name']) - if routine['kind'].lower()=='function': - aDecl=argDecl[:-1] + print("warning, implicitly typed argument '", + arg, "' in routine", routine['name']) + if routine['kind'].lower() == 'function': + aDecl = argDecl[:-1] else: - aDecl=argDecl + aDecl = argDecl - # try to have arg/param/local, but checks for dependencies arg/param and param/local + # try to have arg/param/local, but checks for dependencies arg/param + # and param/local argDecl.extend(paramDecl) enforceDeclDependecies(argDecl) - splitPos=0 - for i in xrange(len(argDecl)-1,-1,-1): - if not 'parameter' in map(str.lower,argDecl[i]['attributes']): - splitPos=i+1 + splitPos = 0 + for i in xrange(len(argDecl) - 1, -1, -1): + if not 'parameter' in map(str.lower, argDecl[i]['attributes']): + splitPos = i + 1 break - paramDecl=argDecl[splitPos:] - argDecl=argDecl[:splitPos] + paramDecl = argDecl[splitPos:] + argDecl = argDecl[:splitPos] paramDecl.extend(localDecl) enforceDeclDependecies(paramDecl) - splitPos=0 - for i in xrange(len(paramDecl)-1,-1,-1): - if 'parameter' in map(str.lower,paramDecl[i]['attributes']): - splitPos=i+1 + splitPos = 0 + for i in xrange(len(paramDecl) - 1, -1, -1): + if 'parameter' in map(str.lower, paramDecl[i]['attributes']): + splitPos = i + 1 break - localDecl=paramDecl[splitPos:] - paramDecl=paramDecl[:splitPos] + localDecl = paramDecl[splitPos:] + paramDecl = paramDecl[:splitPos] - newDecl=StringIO() + newDecl = StringIO() for comment in routine['preDeclComments']: if not commentToRemoveRe.match(comment): newDecl.write(comment) newDecl.writelines(routine['use']) - writeDeclarations(argDecl,newDecl) + writeDeclarations(argDecl, newDecl) if argDecl and paramDecl: newDecl.write("\n") - writeDeclarations(paramDecl,newDecl) + writeDeclarations(paramDecl, newDecl) if (argDecl or paramDecl) and localDecl: newDecl.write("\n") - writeDeclarations(localDecl,newDecl) + writeDeclarations(localDecl, newDecl) if argDecl or paramDecl or localDecl: newDecl.write("\n") - wrote=0 + wrote = 0 for comment in routine['declComments']: if not commentToRemoveRe.match(comment): newDecl.write(comment) newDecl.write("\n") - wrote=1 + wrote = 1 if wrote: newDecl.write("\n") - routine['declarations']=[newDecl.getvalue()] + routine['declarations'] = [newDecl.getvalue()] except: if routine.has_key('name'): - logFile.write("**** exception cleaning routine "+routine['name']+" ****") - logFile.write("parsedDeclartions="+str(routine['parsedDeclarations'])) + logFile.write("**** exception cleaning routine " + + routine['name'] + " ****") + logFile.write("parsedDeclartions=" + + str(routine['parsedDeclarations'])) raise -def rmNullify(var,strings): - removed=0 - var=var.lower() - nullifyRe=re.compile(r" *nullify *\(", re.IGNORECASE) - nullify2Re=re.compile(r"(?P *nullify *\()(?P[^()!&]+)\)",re.IGNORECASE) - for i in xrange(len(strings)-1,-1,-1): - line=strings[i] - comments=[] - if nullifyRe.match(line) and findWord(var,line)!=-1: - core="" - comments=[] +def rmNullify(var, strings): + removed = 0 + var = var.lower() + nullifyRe = re.compile(r" *nullify *\(", re.IGNORECASE) + nullify2Re = re.compile( + r"(?P *nullify *\()(?P[^()!&]+)\)", re.IGNORECASE) + + for i in xrange(len(strings) - 1, -1, -1): + line = strings[i] + comments = [] + if nullifyRe.match(line) and findWord(var, line) != -1: + core = "" + comments = [] for l in line.splitlines(): - pos=l.find("&") - pos2=l.find("!") - if pos==-1: - if pos2==-1: - core+=l + pos = l.find("&") + pos2 = l.find("!") + if pos == -1: + if pos2 == -1: + core += l else: - core+=l[:pos2] - comments.append(l[pos2:]+"\n") + core += l[:pos2] + comments.append(l[pos2:] + "\n") else: - core+=l[:pos] - if pos2!=-1: - comments.append(l[pos2:]+"\n") - m=nullify2Re.match(core) + core += l[:pos] + if pos2 != -1: + comments.append(l[pos2:] + "\n") + m = nullify2Re.match(core) if not m: - raise SyntaxError("could not match nullify to "+repr(core)+ - "in"+repr(line)) - allVars=[] - vars=m.group("vars") - v= map(string.strip,vars.split(",")) - removedNow=0 - for j in xrange(len(v)-1,-1,-1): - if findWord(var,v[j].lower())!=-1: + raise SyntaxError("could not match nullify to " + repr(core) + + "in" + repr(line)) + allVars = [] + vars = m.group("vars") + v = map(string.strip, vars.split(",")) + removedNow = 0 + for j in xrange(len(v) - 1, -1, -1): + if findWord(var, v[j].lower()) != -1: del v[j] - removedNow=1 + removedNow = 1 if removedNow: - if len(v)==0: + if len(v) == 0: if not comments: del strings[i] else: - strings[i]="".join(comments) + strings[i] = "".join(comments) else: - for j in xrange(len(v)-1): - v[j]+=", " - v[-1]+=")" - newS=StringIO() - v.insert(0,m.group("nullif")) - writeInCols(v,len(v[0])-len(v[0].lstrip())+5,77,0,newS) + for j in xrange(len(v) - 1): + v[j] += ", " + v[-1] += ")" + newS = StringIO() + v.insert(0, m.group("nullif")) + writeInCols(v, len(v[0]) - + len(v[0].lstrip()) + 5, 77, 0, newS) newS.write("\n") if comments: for c in comments: newS.write(c) - strings[i]=newS.getvalue() - removed+=1 + strings[i] = newS.getvalue() + removed += 1 return removed + def parseUse(inFile): """Parses the use statements in inFile The parsing stops at the first non use statement. @@ -762,33 +815,36 @@ def parseUse(inFile): '! comment1\\n!comment2...\\n', 'last line (the line that stopped the parsing)') """ - lineNr=0 - preComments=[] - modules=[] - origLines=[] - commonUses="" + lineNr = 0 + preComments = [] + modules = [] + origLines = [] + commonUses = "" stream = InputStream(inFile) while 1: - (jline,comment_list,lines)=stream.nextFortranLine() + (jline, comment_list, lines) = stream.nextFortranLine() comments = '\n'.join(_ for _ in comment_list if _) - lineNr=lineNr+len(lines) - if not lines: break + lineNr = lineNr + len(lines) + if not lines: + break origLines.append("".join(lines)) # parse use - m=useParseRe.match(jline) + m = useParseRe.match(jline) if m: if comments: - print("jline",jline,"lines",lines) - useAtt={'module':m.group('module'),'comments':[]} + print("jline", jline, "lines", lines) + useAtt = {'module': m.group('module'), 'comments': []} if m.group('only'): - useAtt['only']=map(string.strip, - string.split(m.group('imports'),',')) + useAtt['only'] = map(string.strip, + string.split(m.group('imports'), ',')) else: - useAtt['renames']=map(string.strip, - string.split(m.group('imports'),',')) - if useAtt['renames']==[""]: del useAtt['renames'] - if comments : useAtt['comments'].append(comments) + useAtt['renames'] = map(string.strip, + string.split(m.group('imports'), ',')) + if useAtt['renames'] == [""]: + del useAtt['renames'] + if comments: + useAtt['comments'].append(comments) # add use to modules modules.append(useAtt) elif jline and not jline.isspace(): @@ -796,155 +852,171 @@ def parseUse(inFile): else: if comments and commonUsesRe.match(comments): commonUses += "".join(lines) - elif len(modules)==0: + elif len(modules) == 0: preComments.append(("".join(lines))) elif comments: modules[-1]['comments'].append(comments) - return {'modules':modules,'preComments':preComments,'commonUses':commonUses, - 'postLine':"".join(lines),'origLines':origLines[:-1]} + return {'modules': modules, 'preComments': preComments, 'commonUses': commonUses, + 'postLine': "".join(lines), 'origLines': origLines[:-1]} + def normalizeModules(modules): """Sorts the modules and their export and removes duplicates. renames aren't sorted correctly""" # orders modules - modules.sort(lambda x,y:cmp(x['module'],y['module']) ) - for i in range(len(modules)-1,0,-1): - if modules[i]['module']==modules[i-1]['module']: - if not (modules[i-1].has_key('only') and + modules.sort(lambda x, y: cmp(x['module'], y['module'])) + for i in range(len(modules) - 1, 0, -1): + if modules[i]['module'] == modules[i - 1]['module']: + if not (modules[i - 1].has_key('only') and modules[i].has_key('only')): - raise SyntaxError('rejoining of module '+ - str(modules[i]['module'])+ + raise SyntaxError('rejoining of module ' + + str(modules[i]['module']) + ' failed as at least one of the use is not a use ...,only:') - modules[i-1]['only'].extend(modules[i]['only']) + modules[i - 1]['only'].extend(modules[i]['only']) del modules[i] # orders imports for m in modules: if m.has_key('only'): m['only'].sort() - for i in range(len(m['only'])-1,0,-1): - if m['only'][i-1]==m['only'][i]: del m['only'][i] + for i in range(len(m['only']) - 1, 0, -1): + if m['only'][i - 1] == m['only'][i]: + del m['only'][i] -def writeUses(modules,outFile): + +def writeUses(modules, outFile): """Writes the use declaration using a long or short form depending on how many only statements there are""" for m in modules: - if m.has_key('only') and len(m['only'])>8: - writeUseShort(m,outFile) + if m.has_key('only') and len(m['only']) > 8: + writeUseShort(m, outFile) else: - writeUseLong(m,outFile) + writeUseLong(m, outFile) -def writeUseLong(m,outFile): + +def writeUseLong(m, outFile): """Writes a use declaration in a nicer, but longer way""" if m.has_key('only'): - outFile.write(indentSize*' ' + "USE "+m['module']+","+ - string.rjust('ONLY: ',38-len(m['module']))) - if m['only']: outFile.write(m['only'][0]) - for i in range(1,len(m['only'])): - outFile.write(",&\n"+string.ljust("",43+indentSize)+m['only'][i]) + outFile.write(indentSize * ' ' + "USE " + m['module'] + "," + + string.rjust('ONLY: ', 38 - len(m['module']))) + if m['only']: + outFile.write(m['only'][0]) + for i in range(1, len(m['only'])): + outFile.write(",&\n" + string.ljust("", 43 + + indentSize) + m['only'][i]) else: - outFile.write(indentSize*' ' + "USE "+m['module']) + outFile.write(indentSize * ' ' + "USE " + m['module']) if m.has_key('renames') and m['renames']: - outFile.write(","+string.ljust("",38)+ + outFile.write("," + string.ljust("", 38) + m['renames'][0]) - for i in range(1,len(m['renames'])): - outFile.write(",&\n"+string.ljust("",43+indentSize)+m['renames'][i]) + for i in range(1, len(m['renames'])): + outFile.write(",&\n" + string.ljust("", 43 + + indentSize) + m['renames'][i]) if m['comments']: outFile.write("\n") outFile.write('\n'.join(m['comments'])) outFile.write("\n") -def writeUseShort(m,file): + +def writeUseShort(m, file): """Writes a use declaration in a compact way""" - uLine=[] + uLine = [] if m.has_key('only'): - file.write(indentSize*' ' + "USE "+m['module']+","+ - string.rjust('ONLY: &\n',40-len(m['module']))) + file.write(indentSize * ' ' + "USE " + m['module'] + "," + + string.rjust('ONLY: &\n', 40 - len(m['module']))) for k in m['only'][:-1]: - uLine.append(k+", ") + uLine.append(k + ", ") uLine.append(m['only'][-1]) - uLine[0]=" "*(5+indentSize)+uLine[0] + uLine[0] = " " * (5 + indentSize) + uLine[0] elif m.has_key('renames') and m['renames']: - uLine.append(indentSize*' ' +"USE "+m['module']+", ") + uLine.append(indentSize * ' ' + "USE " + m['module'] + ", ") for k in m['renames'][:-1]: - uLine.append(k+", ") + uLine.append(k + ", ") uLine.append(m['renames'][-1]) else: - uLine.append(indentSize*' ' +"USE "+m['module']) - writeInCols(uLine,5+indentSize,decllinelength,0,file) + uLine.append(indentSize * ' ' + "USE " + m['module']) + writeInCols(uLine, 5 + indentSize, decllinelength, 0, file) if m['comments']: file.write("\n") file.write('\n'.join(m['comments'])) file.write("\n") + def prepareImplicitUses(modules): """Transforms a modulesDict into an implictUses (dictionary of module names each containing a dictionary with the only, and the special key '_WHOLE_' wich is true if the whole mosule is implicitly present""" - mods={} + mods = {} for m in modules: - m_name=m['module'].lower() + m_name = m['module'].lower() if (not mods.has_key(m_name)): - mods[m['module']]={'_WHOLE_':0} - m_att=mods[m_name] + mods[m['module']] = {'_WHOLE_': 0} + m_att = mods[m_name] if m.has_key('only'): for k in m['only']: - m=localNameRe.match(k) + m = localNameRe.match(k) if not m: - raise SyntaxError('could not parse use only:'+repr(k)) - impAtt=m.group('localName').lower() - m_att[impAtt]=1 + raise SyntaxError('could not parse use only:' + repr(k)) + impAtt = m.group('localName').lower() + m_att[impAtt] = 1 else: - m_att['_WHOLE_']=1 + m_att['_WHOLE_'] = 1 return mods -def cleanUse(modulesDict,rest,implicitUses=None,logFile=sys.stdout): + +def cleanUse(modulesDict, rest, implicitUses=None, logFile=sys.stdout): """Removes the unneded modules (the ones that are not used in rest)""" global rUse - exceptions={} - modules=modulesDict['modules'] - rest=rest.lower() - for i in range(len(modules)-1,-1,-1): - m_att={} - m_name=modules[i]['module'].lower() + exceptions = {} + modules = modulesDict['modules'] + rest = rest.lower() + for i in range(len(modules) - 1, -1, -1): + m_att = {} + m_name = modules[i]['module'].lower() if implicitUses and implicitUses.has_key(m_name): - m_att=implicitUses[m_name] + m_att = implicitUses[m_name] if m_att.has_key('_WHOLE_') and m_att['_WHOLE_']: - rUse+=1 - logFile.write("removed USE of module "+m_name+"\n") + rUse += 1 + logFile.write("removed USE of module " + m_name + "\n") del modules[i] elif modules[i].has_key("only"): - els=modules[i]['only'] - for j in range(len(els)-1,-1,-1): - m=localNameRe.match(els[j]) + els = modules[i]['only'] + for j in range(len(els) - 1, -1, -1): + m = localNameRe.match(els[j]) if not m: - raise SyntaxError('could not parse use only:'+repr(els[j])) - impAtt=m.group('localName').lower() + raise SyntaxError( + 'could not parse use only:' + repr(els[j])) + impAtt = m.group('localName').lower() if m_att.has_key(impAtt): - rUse+=1 - logFile.write("removed USE "+m_name+", only: "+repr(els[j])+"\n") + rUse += 1 + logFile.write("removed USE " + m_name + + ", only: " + repr(els[j]) + "\n") del els[j] elif not exceptions.has_key(impAtt): - if findWord(impAtt,rest)==-1: - rUse+=1 - logFile.write("removed USE "+m_name+", only: "+repr(els[j])+"\n") + if findWord(impAtt, rest) == -1: + rUse += 1 + logFile.write("removed USE " + m_name + + ", only: " + repr(els[j]) + "\n") del els[j] - if len(modules[i]['only'])==0: + if len(modules[i]['only']) == 0: if modules[i]['comments']: modulesDict['preComments'].extend( - map(lambda x:x+"\n",modules[i]['comments'])) + map(lambda x: x + "\n", modules[i]['comments'])) del modules[i] -def resetModuleN(moduleName,lines): + +def resetModuleN(moduleName, lines): "resets the moduleN variable to the module name in the lines lines" - moduleNRe=re.compile(r".*:: *moduleN *= *(['\"])[a-zA-Z_0-9]+\1", - flags=re.IGNORECASE) + moduleNRe = re.compile(r".*:: *moduleN *= *(['\"])[a-zA-Z_0-9]+\1", + flags=re.IGNORECASE) for i in xrange(len(lines)): - lines[i]=moduleNRe.sub( - " CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = '"+moduleName+"'", + lines[i] = moduleNRe.sub( + " CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = '" + + moduleName + "'", lines[i]) -def rewriteFortranFile(inFile,outFile,indent,decl_linelength,decl_offset,logFile=sys.stdout,orig_filename=None): + +def rewriteFortranFile(inFile, outFile, indent, decl_linelength, decl_offset, logFile=sys.stdout, orig_filename=None): """rewrites the use statements and declarations of inFile to outFile. It sorts them and removes the repetitions.""" import os.path @@ -952,83 +1024,90 @@ def rewriteFortranFile(inFile,outFile,indent,decl_linelength,decl_offset,logFile global indentSize global decloffset global decllinelength - indentSize=indent - decloffset=decl_offset - decllinelength=decl_linelength + indentSize = indent + decloffset = decl_offset + decllinelength = decl_linelength - moduleRe=re.compile(r" *(?:module|program) +(?P[a-zA-Z_][a-zA-Z_0-9]*) *(?:!.*)?$", - flags=re.IGNORECASE) - coreLines=[] + moduleRe = re.compile(r" *(?:module|program) +(?P[a-zA-Z_][a-zA-Z_0-9]*) *(?:!.*)?$", + flags=re.IGNORECASE) + coreLines = [] while 1: - line=inFile.readline() - if not line: break - if line[0]=='#': + line = inFile.readline() + if not line: + break + if line[0] == '#': coreLines.append(line) outFile.write(line) - m=moduleRe.match(line) + m = moduleRe.match(line) if m: - if not orig_filename: orig_filename=inFile.name - fn = os.path.basename(orig_filename).rsplit(".",1)[0] - if (m.group('moduleName')!=fn) : - raise SyntaxError("Module name is different from filename ("+ - m.group('moduleName')+"!="+fn+")") + if not orig_filename: + orig_filename = inFile.name + fn = os.path.basename(orig_filename).rsplit(".", 1)[0] + if (m.group('moduleName') != fn): + raise SyntaxError("Module name is different from filename (" + + m.group('moduleName') + "!=" + fn + ")") break try: - modulesDict=parseUse(inFile) - routines=[] + modulesDict = parseUse(inFile) + routines = [] coreLines.append(modulesDict['postLine']) - routine=parseRoutine(inFile) + routine = parseRoutine(inFile) coreLines.extend(routine['preRoutine']) if m: - resetModuleN(m.group('moduleName'),routine['preRoutine']) + resetModuleN(m.group('moduleName'), routine['preRoutine']) routines.append(routine) while routine['kind']: - routine=parseRoutine(inFile) + routine = parseRoutine(inFile) routines.append(routine) - map(lambda x:cleanDeclarations(x,logFile),routines) + map(lambda x: cleanDeclarations(x, logFile), routines) for routine in routines: coreLines.extend(routine['declarations']) coreLines.extend(routine['strippedCore']) - rest="".join(coreLines) - nonStPrep=0 + rest = "".join(coreLines) + nonStPrep = 0 for line in modulesDict['origLines']: - if (re.search('^#',line) and not commonUsesRe.match(line)): - print('noMatch',repr(line)) - nonStPrep=1 + if (re.search('^#', line) and not commonUsesRe.match(line)): + print('noMatch', repr(line)) + nonStPrep = 1 if nonStPrep: - logFile.write("*** use statements contains preprocessor directives, not cleaning ***\n") + logFile.write( + "*** use statements contains preprocessor directives, not cleaning ***\n") outFile.writelines(modulesDict['origLines']) else: - implicitUses=None + implicitUses = None if modulesDict['commonUses']: try: - inc_fn = commonUsesRe.match(modulesDict['commonUses']).group(1) - inc_absfn = os.path.join(os.path.dirname(inFile.name), inc_fn) - f=file(inc_absfn) - implicitUsesRaw=parseUse(f) + inc_fn = commonUsesRe.match( + modulesDict['commonUses']).group(1) + inc_absfn = os.path.join( + os.path.dirname(inFile.name), inc_fn) + f = file(inc_absfn) + implicitUsesRaw = parseUse(f) f.close() - implicitUses=prepareImplicitUses(implicitUsesRaw['modules']) + implicitUses = prepareImplicitUses( + implicitUsesRaw['modules']) except: print ("ERROR trying to parse use statements contained in common", "uses precompiler file ", inc_absfn) raise - cleanUse(modulesDict,rest,implicitUses=implicitUses,logFile=logFile) + cleanUse(modulesDict, rest, + implicitUses=implicitUses, logFile=logFile) normalizeModules(modulesDict['modules']) outFile.writelines(modulesDict['preComments']) - writeUses(modulesDict['modules'],outFile) + writeUses(modulesDict['modules'], outFile) outFile.write(modulesDict['commonUses']) if modulesDict['modules']: outFile.write('\n') outFile.write(modulesDict['postLine']) for routine in routines: - writeRoutine(routine,outFile) + writeRoutine(routine, outFile) except: import traceback - logFile.write('-'*60+"\n") + logFile.write('-' * 60 + "\n") traceback.print_exc(file=logFile) - logFile.write('-'*60+"\n") + logFile.write('-' * 60 + "\n") - logFile.write("Processing file '"+inFile.name+"'\n") + logFile.write("Processing file '" + inFile.name + "'\n") raise -#EOF +# EOF diff --git a/tools/formatting/reformatFortranFile.py b/tools/formatting/reformatFortranFile.py index a50002dfa1..67c0bc6a43 100644 --- a/tools/formatting/reformatFortranFile.py +++ b/tools/formatting/reformatFortranFile.py @@ -472,14 +472,16 @@ def format_single_fline(f_line, whitespace, linebreak_pos, ampersand_sep, filena line_ftd = '' pos_prev = -1 for pos, char in CharFilter(enumerate(line)): - is_decl = line[pos:].lstrip().startswith('::') or line[:pos].rstrip().endswith('::') + is_decl = line[pos:].lstrip().startswith('::') or line[ + :pos].rstrip().endswith('::') if char == ' ': - if line_ftd and (re.search(r'[\w"]',line_ftd[-1]) or is_decl): # remove double spaces + # remove double spaces + if line_ftd and (re.search(r'[\w"]', line_ftd[-1]) or is_decl): line_ftd = line_ftd + char else: - if line_ftd and line_ftd[-1]==' ' and (not re.search(r'[\w"]',char) and not is_decl): - line_ftd = line_ftd[:-1] # remove spaces except between words - line_ftd = line_ftd + line[pos_prev+1:pos+1] + if line_ftd and line_ftd[-1] == ' ' and (not re.search(r'[\w"]', char) and not is_decl): + line_ftd = line_ftd[:-1] # remove spaces except between words + line_ftd = line_ftd + line[pos_prev + 1:pos + 1] pos_prev = pos line = line_ftd @@ -615,8 +617,8 @@ def format_single_fline(f_line, whitespace, linebreak_pos, ampersand_sep, filena # format ':' for labels for newre in NEW_SCOPE_RE[0:2]: - if newre.search(line) and re.search(SOL_STR+r"\w+\s*:", line): - line = ': '.join(_.strip() for _ in line.split(':',1)) + if newre.search(line) and re.search(SOL_STR + r"\w+\s*:", line): + line = ': '.join(_.strip() for _ in line.split(':', 1)) if not auto_format: line = line_orig @@ -894,7 +896,8 @@ if __name__ == '__main__': try: print("reformatting", fileName) infile = open(fileName, 'r') - outfile = open(os.path.join(outDir, os.path.basename(fileName)), 'w') + outfile = open(os.path.join( + outDir, os.path.basename(fileName)), 'w') reformat_ffile(infile, outfile) except: print("error for file", fileName) diff --git a/tools/formatting/replacer.py b/tools/formatting/replacer.py index b7dcfa823e..29c6104486 100644 --- a/tools/formatting/replacer.py +++ b/tools/formatting/replacer.py @@ -1,14 +1,15 @@ import re import sys -repl={ - 'routine_name':'routineN', - 'module_name':'moduleN' - } -specialRepl=None +repl = { + 'routine_name': 'routineN', + 'module_name': 'moduleN' +} +specialRepl = None # { re.compile(r"(.*:: *moduleN) *= *(['\"])[a-zA-Z_0-9]+\2",flags=re.IGNORECASE):r"character(len=*), parameter :: moduleN = '__MODULE_NAME__'" } -def replaceWords(infile,outfile,replacements=repl, + +def replaceWords(infile, outfile, replacements=repl, specialReplacements=specialRepl, logFile=sys.stdout): """Replaces the words in infile writing the output to outfile. @@ -16,24 +17,23 @@ def replaceWords(infile,outfile,replacements=repl, replacements is a dictionary with the words to replace. specialReplacements is a dictionary with general regexp replacements. """ - lineNr=0 - nonWordRe=re.compile(r"(\W+)") + lineNr = 0 + nonWordRe = re.compile(r"(\W+)") while 1: - line= infile.readline() - lineNr=lineNr+1 - if not line: break + line = infile.readline() + lineNr = lineNr + 1 + if not line: + break if specialReplacements: for subs in specialReplacements.keys(): - line=subs.sub(specialReplacements[subs],line) + line = subs.sub(specialReplacements[subs], line) - tokens=nonWordRe.split(line) + tokens = nonWordRe.split(line) for token in tokens: if replacements.has_key(token): outfile.write(replacements[token]) else: outfile.write(token) -#EOF - - +# EOF diff --git a/tools/prettify.py b/tools/prettify.py index 9aee478427..a8fed4d0f9 100755 --- a/tools/prettify.py +++ b/tools/prettify.py @@ -17,63 +17,70 @@ from formatting import addSynopsis from formatting import reformatFortranFile -operatorsStr=r"\.(?:and|eqv?|false|g[et]|l[et]|n(?:e(?:|qv)|ot)|or|true)\." +operatorsStr = r"\.(?:and|eqv?|false|g[et]|l[et]|n(?:e(?:|qv)|ot)|or|true)\." -keywordsStr="(?:a(?:llocat(?:able|e)|ssign(?:|ment))|c(?:a(?:ll|se)|haracter|lose|o(?:m(?:mon|plex)|nt(?:ains|inue))|ycle)|d(?:ata|eallocate|imension|o(?:|uble))|e(?:lse(?:|if|where)|n(?:d(?:|do|file|if)|try)|quivalence|x(?:it|ternal))|f(?:or(?:all|mat)|unction)|goto|i(?:f|mplicit|n(?:clude|quire|t(?:e(?:ger|nt|rface)|rinsic)))|logical|module|n(?:amelist|one|ullify)|o(?:nly|p(?:en|erator|tional))|p(?:a(?:rameter|use)|ointer|r(?:ecision|i(?:nt|vate)|o(?:cedure|gram))|ublic)|re(?:a[dl]|cursive|sult|turn|wind)|s(?:ave|e(?:lect|quence)|top|ubroutine)|t(?:arget|hen|ype)|use|w(?:h(?:ere|ile)|rite))" +keywordsStr = "(?:a(?:llocat(?:able|e)|ssign(?:|ment))|c(?:a(?:ll|se)|haracter|lose|o(?:m(?:mon|plex)|nt(?:ains|inue))|ycle)|d(?:ata|eallocate|imension|o(?:|uble))|e(?:lse(?:|if|where)|n(?:d(?:|do|file|if)|try)|quivalence|x(?:it|ternal))|f(?:or(?:all|mat)|unction)|goto|i(?:f|mplicit|n(?:clude|quire|t(?:e(?:ger|nt|rface)|rinsic)))|logical|module|n(?:amelist|one|ullify)|o(?:nly|p(?:en|erator|tional))|p(?:a(?:rameter|use)|ointer|r(?:ecision|i(?:nt|vate)|o(?:cedure|gram))|ublic)|re(?:a[dl]|cursive|sult|turn|wind)|s(?:ave|e(?:lect|quence)|top|ubroutine)|t(?:arget|hen|ype)|use|w(?:h(?:ere|ile)|rite))" -intrinsic_procStr=r"(?:a(?:bs|c(?:har|os)|djust[lr]|i(?:mag|nt)|ll(?:|ocated)|n(?:int|y)|s(?:in|sociated)|tan2?)|b(?:it_size|test)|c(?:eiling|har|mplx|o(?:njg|sh?|unt)|shift)|d(?:ate_and_time|ble|i(?:gits|m)|ot_product|prod)|e(?:oshift|psilon|xp(?:|onent))|f(?:loor|raction)|huge|i(?:a(?:char|nd)|b(?:clr|its|set)|char|eor|n(?:dex|t)|or|shftc?)|kind|l(?:bound|en(?:|_trim)|g[et]|l[et]|og(?:|10|ical))|m(?:a(?:tmul|x(?:|exponent|loc|val))|erge|in(?:|exponent|loc|val)|od(?:|ulo)|vbits)|n(?:earest|int|ot)|p(?:ack|r(?:e(?:cision|sent)|oduct))|r(?:a(?:dix|n(?:dom_(?:number|seed)|ge))|e(?:peat|shape)|rspacing)|s(?:ca(?:le|n)|e(?:lected_(?:int_kind|real_kind)|t_exponent)|hape|i(?:gn|nh?|ze)|p(?:acing|read)|qrt|um|ystem_clock)|t(?:anh?|iny|r(?:ans(?:fer|pose)|im))|u(?:bound|npack)|verify)(?= *\()" +intrinsic_procStr = r"(?:a(?:bs|c(?:har|os)|djust[lr]|i(?:mag|nt)|ll(?:|ocated)|n(?:int|y)|s(?:in|sociated)|tan2?)|b(?:it_size|test)|c(?:eiling|har|mplx|o(?:njg|sh?|unt)|shift)|d(?:ate_and_time|ble|i(?:gits|m)|ot_product|prod)|e(?:oshift|psilon|xp(?:|onent))|f(?:loor|raction)|huge|i(?:a(?:char|nd)|b(?:clr|its|set)|char|eor|n(?:dex|t)|or|shftc?)|kind|l(?:bound|en(?:|_trim)|g[et]|l[et]|og(?:|10|ical))|m(?:a(?:tmul|x(?:|exponent|loc|val))|erge|in(?:|exponent|loc|val)|od(?:|ulo)|vbits)|n(?:earest|int|ot)|p(?:ack|r(?:e(?:cision|sent)|oduct))|r(?:a(?:dix|n(?:dom_(?:number|seed)|ge))|e(?:peat|shape)|rspacing)|s(?:ca(?:le|n)|e(?:lected_(?:int_kind|real_kind)|t_exponent)|hape|i(?:gn|nh?|ze)|p(?:acing|read)|qrt|um|ystem_clock)|t(?:anh?|iny|r(?:ans(?:fer|pose)|im))|u(?:bound|npack)|verify)(?= *\()" -ompDir=r"(?:atomic|barrier|c(?:apture|ritical)|do|end|flush|if|master|num_threads|ordered|parallel|read|s(?:ection(?:|s)|ingle)|t(?:ask(?:|wait|yield)|hreadprivate)|update|w(?:orkshare|rite)|!\$omp)" +ompDir = r"(?:atomic|barrier|c(?:apture|ritical)|do|end|flush|if|master|num_threads|ordered|parallel|read|s(?:ection(?:|s)|ingle)|t(?:ask(?:|wait|yield)|hreadprivate)|update|w(?:orkshare|rite)|!\$omp)" -ompClause=r"(?:a|co(?:llapse|py(?:in|private))|default|fi(?:nal|rstprivate)|i(?:and|eor|or)|lastprivate|m(?:ax|ergeable|in)|n(?:one|owait)|ordered|private|reduction|shared|untied|\.(?:and|eqv|neqv|or)\.)" +ompClause = r"(?:a|co(?:llapse|py(?:in|private))|default|fi(?:nal|rstprivate)|i(?:and|eor|or)|lastprivate|m(?:ax|ergeable|in)|n(?:one|owait)|ordered|private|reduction|shared|untied|\.(?:and|eqv|neqv|or)\.)" -ompEnv=r"omp_(?:dynamic|max_active_levels|n(?:ested|um_threads)|proc_bind|s(?:tacksize|chedule)|thread_limit|wait_policy)" +ompEnv = r"omp_(?:dynamic|max_active_levels|n(?:ested|um_threads)|proc_bind|s(?:tacksize|chedule)|thread_limit|wait_policy)" + +# FIXME: does not correctly match operator '.op.' if it is not separated +# by whitespaces. +toUpcaseRe = re.compile("(?" + operatorsStr + + "|" + keywordsStr + "|" + intrinsic_procStr + + ")(?![A-Za-z0-9_%])", flags=re.IGNORECASE) +toUpcaseOMPRe = re.compile("(?" + + ompDir + "|" + ompClause + "|" + ompEnv + + ")(?![A-Za-z0-9_%])", flags=re.IGNORECASE) +linePartsRe = re.compile("(?P[^\"'!]*)(?P!.*)?" + + "(?P(?P[\"']).*?(?P=qchar))?") -# FIXME: does not correctly match operator '.op.' if it is not separated by whitespaces. -toUpcaseRe=re.compile("(?"+operatorsStr+ - "|"+ keywordsStr +"|"+ intrinsic_procStr + - ")(?![A-Za-z0-9_%])",flags=re.IGNORECASE) -toUpcaseOMPRe=re.compile("(?" - +ompDir+"|"+ompClause+"|"+ompEnv + - ")(?![A-Za-z0-9_%])",flags=re.IGNORECASE) -linePartsRe=re.compile("(?P[^\"'!]*)(?P!.*)?"+ - "(?P(?P[\"']).*?(?P=qchar))?") def upcaseStringKeywords(line): """Upcases the fortran keywords, operators and intrinsic routines in line""" - res="" - start=0 - while start= max_pretty_iter: - raise RuntimeError("Prettify did not converge in", max_pretty_iter, "steps.") + raise RuntimeError( + "Prettify did not converge in", max_pretty_iter, "steps.") except: - logFile.write("error processing file '"+infile.name+"'\n") + logFile.write("error processing file '" + infile.name + "'\n") raise + def prettfyInplace(fileName, bkDir, **kwargs): """Same as prettify, but inplace, replaces only if needed""" if not os.path.exists(bkDir): os.mkdir(bkDir) if not os.path.isdir(bkDir): - raise Error("bk-dir must be a directory, was "+bkDir) - infile=open(fileName,'r') - outfile=prettifyFile(infile=infile, **kwargs) - if (infile==outfile): + raise Error("bk-dir must be a directory, was " + bkDir) + infile = open(fileName, 'r') + outfile = prettifyFile(infile=infile, **kwargs) + if (infile == outfile): return infile.seek(0) outfile.seek(0) - same=1 + same = 1 while 1: - l1=outfile.readline() - l2=infile.readline() - if (l1!=l2): - same=0 + l1 = outfile.readline() + l2 = infile.readline() + if (l1 != l2): + same = 0 break if not l1: break if (not same): - bkName=os.path.join(bkDir,os.path.basename(fileName)) - bName=bkName - i=0 + bkName = os.path.join(bkDir, os.path.basename(fileName)) + bName = bkName + i = 0 while os.path.exists(bkName): - i+=1 - bkName=bName+"."+str(i) + i += 1 + bkName = bName + "." + str(i) infile.seek(0) - bkFile=file(bkName,"w") + bkFile = file(bkName, "w") while 1: - l1=infile.readline() - if not l1: break + l1 = infile.readline() + if not l1: + break bkFile.write(l1) bkFile.close() outfile.seek(0) - newFile=file(fileName,'w') + newFile = file(fileName, 'w') while 1: - l1=outfile.readline() - if not l1: break + l1 = outfile.readline() + if not l1: + break newFile.write(l1) newFile.close() infile.close() outfile.close() + def main(): # future defaults - defaultsDict={'upcase':1,'normalize-use':1,'omp-upcase':1, - 'decl-linelength':100, 'decl-offset':50, - 'reformat':1, 'indent':3, 'whitespace':1, - 'replace':1, 'interface-dir':None, - 'backup-dir':'preprettify'} + defaultsDict = {'upcase': 1, 'normalize-use': 1, 'omp-upcase': 1, + 'decl-linelength': 100, 'decl-offset': 50, + 'reformat': 1, 'indent': 3, 'whitespace': 1, + 'replace': 1, 'interface-dir': None, + 'backup-dir': 'preprettify'} - usageDesc=("usage:\n"+sys.argv[0]+ """ + usageDesc = ("usage:\n" + sys.argv[0] + """ [--[no-]upcase] [--[no-]normalize-use] [--[no-]omp-upcase] [--[no-]replace] [--[no-]reformat] --indent=3 --whitespace=1 [--interface-dir=~/cp2k/obj/platform/target] [--help] [--backup-dir=bk_dir] file1 [file2 ...] @@ -246,37 +259,40 @@ def main(): If the interface direcory is given updates also the synopsis. (FIXME: ???) Defaults: - """+str(defaultsDict)) + """ + str(defaultsDict)) - replace=None + replace = None if "--help" in sys.argv: print(usageDesc) sys.exit(0) - args=[] + args = [] for arg in sys.argv[1:]: - m=re.match(r"--(no-)?(normalize-use|upcase|omp-upcase|replace|reformat)",arg) + m = re.match( + r"--(no-)?(normalize-use|upcase|omp-upcase|replace|reformat)", arg) if m: - defaultsDict[m.groups()[1]]=not m.groups()[0] + defaultsDict[m.groups()[1]] = not m.groups()[0] else: - m=re.match(r"--(indent|whitespace|decl-linelength|decl-offset)=(.*)",arg) + m = re.match( + r"--(indent|whitespace|decl-linelength|decl-offset)=(.*)", arg) if m: - defaultsDict[m.groups()[0]]=int(m.groups()[1]) + defaultsDict[m.groups()[0]] = int(m.groups()[1]) else: - m=re.match(r"--(interface-dir|backup-dir)=(.*)",arg) + m = re.match(r"--(interface-dir|backup-dir)=(.*)", arg) if m: - path=os.path.abspath(os.path.expanduser(m.groups()[1])) - defaultsDict[m.groups()[0]]=path + path = os.path.abspath(os.path.expanduser(m.groups()[1])) + defaultsDict[m.groups()[0]] = path else: if arg.startswith('--'): - print('unknown option',arg) + print('unknown option', arg) else: args.append(arg) - if len(args)<1: + if len(args) < 1: print(usageDesc) else: - bkDir=defaultsDict['backup-dir'] + bkDir = defaultsDict['backup-dir'] if not os.path.exists(bkDir): - # Another parallel running instance might just have created the dir. + # Another parallel running instance might just have created the + # dir. try: os.mkdir(bkDir) except: @@ -285,37 +301,41 @@ def main(): print("bk-dir must be a directory") print(usageDesc) else: - failure=0 + failure = 0 for fileName in args: if not os.path.isfile(fileName): - print("file",fileName,"does not exists!") + print("file", fileName, "does not exists!") else: try: prettfyInplace(fileName, bkDir=bkDir, logFile=sys.stdout, - normalize_use=defaultsDict['normalize-use'], - decl_linelength=defaultsDict['decl-linelength'], - decl_offset=defaultsDict['decl-offset'], - reformat=defaultsDict['reformat'], - indent=defaultsDict['indent'], - whitespace=defaultsDict['whitespace'], - upcase_keywords=defaultsDict['upcase'], - upcase_omp=defaultsDict['omp-upcase'], - interfaces_dir=defaultsDict['interface-dir'], - replace=defaultsDict['replace']) + normalize_use=defaultsDict[ + 'normalize-use'], + decl_linelength=defaultsDict[ + 'decl-linelength'], + decl_offset=defaultsDict['decl-offset'], + reformat=defaultsDict['reformat'], + indent=defaultsDict['indent'], + whitespace=defaultsDict['whitespace'], + upcase_keywords=defaultsDict['upcase'], + upcase_omp=defaultsDict['omp-upcase'], + interfaces_dir=defaultsDict[ + 'interface-dir'], + replace=defaultsDict['replace']) except: - failure+=1 + failure += 1 import traceback - sys.stdout.write('-'*60+"\n") + sys.stdout.write('-' * 60 + "\n") traceback.print_exc(file=sys.stdout) - sys.stdout.write('-'*60+"\n") - sys.stdout.write("Processing file '"+fileName+"'\n") - sys.exit(failure>0) + sys.stdout.write('-' * 60 + "\n") + sys.stdout.write( + "Processing file '" + fileName + "'\n") + sys.exit(failure > 0) -#=============================================================================== +#========================================================================= if(__name__ == '__main__'): - if(len(sys.argv)==2 and sys.argv[-1]=="--selftest"): - pass #TODO implement selftest + if(len(sys.argv) == 2 and sys.argv[-1] == "--selftest"): + pass # TODO implement selftest else: main() -#EOF +# EOF