mirror of
https://github.com/cp2k/cp2k.git
synced 2026-07-28 14:15:19 -04:00
make prettify scripts pep8 conformant (autopep8).
svn-origin-rev: 16771
This commit is contained in:
parent
0cf264ff92
commit
3440211ba0
5 changed files with 807 additions and 683 deletions
|
|
@ -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')
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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("(?<![A-Za-z0-9_%#])(?<!% )(?P<toUpcase>" + operatorsStr +
|
||||
"|" + keywordsStr + "|" + intrinsic_procStr +
|
||||
")(?![A-Za-z0-9_%])", flags=re.IGNORECASE)
|
||||
toUpcaseOMPRe = re.compile("(?<![A-Za-z0-9_%#])(?P<toUpcase>"
|
||||
+ ompDir + "|" + ompClause + "|" + ompEnv +
|
||||
")(?![A-Za-z0-9_%])", flags=re.IGNORECASE)
|
||||
linePartsRe = re.compile("(?P<commands>[^\"'!]*)(?P<comment>!.*)?" +
|
||||
"(?P<string>(?P<qchar>[\"']).*?(?P=qchar))?")
|
||||
|
||||
# FIXME: does not correctly match operator '.op.' if it is not separated by whitespaces.
|
||||
toUpcaseRe=re.compile("(?<![A-Za-z0-9_%#])(?<!% )(?P<toUpcase>"+operatorsStr+
|
||||
"|"+ keywordsStr +"|"+ intrinsic_procStr +
|
||||
")(?![A-Za-z0-9_%])",flags=re.IGNORECASE)
|
||||
toUpcaseOMPRe=re.compile("(?<![A-Za-z0-9_%#])(?P<toUpcase>"
|
||||
+ompDir+"|"+ompClause+"|"+ompEnv +
|
||||
")(?![A-Za-z0-9_%])",flags=re.IGNORECASE)
|
||||
linePartsRe=re.compile("(?P<commands>[^\"'!]*)(?P<comment>!.*)?"+
|
||||
"(?P<string>(?P<qchar>[\"']).*?(?P=qchar))?")
|
||||
|
||||
def upcaseStringKeywords(line):
|
||||
"""Upcases the fortran keywords, operators and intrinsic routines
|
||||
in line"""
|
||||
res=""
|
||||
start=0
|
||||
while start<len(line):
|
||||
m=linePartsRe.match(line[start:])
|
||||
if not m: raise SyntaxError("Syntax error, open string")
|
||||
res=res+toUpcaseRe.sub(lambda match: match.group("toUpcase").upper(),
|
||||
m.group("commands"))
|
||||
res = ""
|
||||
start = 0
|
||||
while start < len(line):
|
||||
m = linePartsRe.match(line[start:])
|
||||
if not m:
|
||||
raise SyntaxError("Syntax error, open string")
|
||||
res = res + toUpcaseRe.sub(lambda match: match.group("toUpcase").upper(),
|
||||
m.group("commands"))
|
||||
if m.group("comment"):
|
||||
res=res+m.group("comment")
|
||||
res = res + m.group("comment")
|
||||
if m.group("string"):
|
||||
res=res+m.group("string")
|
||||
start=start+m.end()
|
||||
res = res + m.group("string")
|
||||
start = start + m.end()
|
||||
return res
|
||||
|
||||
|
||||
def upcaseOMP(line):
|
||||
"""Upcases OpenMP stuff."""
|
||||
return toUpcaseOMPRe.sub(lambda match: match.group("toUpcase").upper(),line)
|
||||
return toUpcaseOMPRe.sub(lambda match: match.group("toUpcase").upper(), line)
|
||||
|
||||
def upcaseKeywords(infile,outfile,upcase_omp,logFile=sys.stdout):
|
||||
|
||||
def upcaseKeywords(infile, outfile, upcase_omp, logFile=sys.stdout):
|
||||
"""Writes infile to outfile with all the fortran keywords upcased"""
|
||||
while 1:
|
||||
line=infile.readline()
|
||||
if not line: break
|
||||
line = infile.readline()
|
||||
if not line:
|
||||
break
|
||||
line = upcaseStringKeywords(line)
|
||||
if upcase_omp:
|
||||
if normalizeFortranFile.ompDirRe.match(line):
|
||||
line=upcaseOMP(line)
|
||||
line = upcaseOMP(line)
|
||||
outfile.write(line)
|
||||
|
||||
|
||||
def prettifyFile(infile, normalize_use, decl_linelength, decl_offset,
|
||||
reformat, indent, whitespace, upcase_keywords,
|
||||
upcase_omp, interfaces_dir,replace, logFile):
|
||||
upcase_omp, interfaces_dir, replace, logFile):
|
||||
"""prettifyes the fortran source in infile into a temporary file that is
|
||||
returned. It can be the same as infile.
|
||||
if normalize_use normalizes the use statements (defaults to true)
|
||||
|
|
@ -84,9 +91,9 @@ def prettifyFile(infile, normalize_use, decl_linelength, decl_offset,
|
|||
to false)
|
||||
|
||||
does not close the input file"""
|
||||
ifile=infile
|
||||
orig_filename=infile.name
|
||||
tmpfile=None
|
||||
ifile = infile
|
||||
orig_filename = infile.name
|
||||
tmpfile = None
|
||||
max_pretty_iter = 5
|
||||
n_pretty_iter = 0
|
||||
|
||||
|
|
@ -97,127 +104,133 @@ def prettifyFile(infile, normalize_use, decl_linelength, decl_offset,
|
|||
ifile.seek(0)
|
||||
try:
|
||||
if replace:
|
||||
tmpfile2=os.tmpfile()
|
||||
replacer.replaceWords(ifile,tmpfile2,logFile=logFile)
|
||||
tmpfile2 = os.tmpfile()
|
||||
replacer.replaceWords(ifile, tmpfile2, logFile=logFile)
|
||||
tmpfile2.seek(0)
|
||||
if tmpfile:
|
||||
tmpfile.close()
|
||||
tmpfile=tmpfile2
|
||||
ifile=tmpfile
|
||||
if reformat: # reformat needs to be done first
|
||||
tmpfile2=os.tmpfile()
|
||||
reformatFortranFile.reformat_ffile(ifile,tmpfile2,logFile=logFile,
|
||||
indent_size=indent,whitespace=whitespace,
|
||||
orig_filename=orig_filename)
|
||||
tmpfile = tmpfile2
|
||||
ifile = tmpfile
|
||||
if reformat: # reformat needs to be done first
|
||||
tmpfile2 = os.tmpfile()
|
||||
reformatFortranFile.reformat_ffile(ifile, tmpfile2, logFile=logFile,
|
||||
indent_size=indent, whitespace=whitespace,
|
||||
orig_filename=orig_filename)
|
||||
tmpfile2.seek(0)
|
||||
if tmpfile:
|
||||
tmpfile.close()
|
||||
tmpfile=tmpfile2
|
||||
ifile=tmpfile
|
||||
tmpfile = tmpfile2
|
||||
ifile = tmpfile
|
||||
if normalize_use:
|
||||
tmpfile2=os.tmpfile()
|
||||
normalizeFortranFile.rewriteFortranFile(ifile,tmpfile2,indent,
|
||||
tmpfile2 = os.tmpfile()
|
||||
normalizeFortranFile.rewriteFortranFile(ifile, tmpfile2, indent,
|
||||
decl_linelength, decl_offset,
|
||||
logFile,orig_filename=orig_filename)
|
||||
logFile, orig_filename=orig_filename)
|
||||
tmpfile2.seek(0)
|
||||
if tmpfile:
|
||||
tmpfile.close()
|
||||
tmpfile=tmpfile2
|
||||
ifile=tmpfile
|
||||
tmpfile = tmpfile2
|
||||
ifile = tmpfile
|
||||
if upcase_keywords:
|
||||
tmpfile2=os.tmpfile()
|
||||
upcaseKeywords(ifile,tmpfile2,upcase_omp,logFile)
|
||||
tmpfile2 = os.tmpfile()
|
||||
upcaseKeywords(ifile, tmpfile2, upcase_omp, logFile)
|
||||
tmpfile2.seek(0)
|
||||
if tmpfile:
|
||||
tmpfile.close()
|
||||
tmpfile=tmpfile2
|
||||
ifile=tmpfile
|
||||
tmpfile = tmpfile2
|
||||
ifile = tmpfile
|
||||
if interfaces_dir:
|
||||
fileName=os.path.basename(infile.name)
|
||||
fileName=fileName[:fileName.rfind(".")]
|
||||
fileName = os.path.basename(infile.name)
|
||||
fileName = fileName[:fileName.rfind(".")]
|
||||
try:
|
||||
interfaceFile=open(os.path.join(interfaces_dir,
|
||||
fileName+".int"),"r")
|
||||
interfaceFile = open(os.path.join(interfaces_dir,
|
||||
fileName + ".int"), "r")
|
||||
except:
|
||||
logFile.write("error opening file "+
|
||||
logFile.write("error opening file " +
|
||||
os.path.join(interfaces_dir,
|
||||
fileName+".int")+"\n")
|
||||
logFile.write("skipping addSynopsis step for "+fileName+"\n")
|
||||
interfaceFile=None
|
||||
fileName + ".int") + "\n")
|
||||
logFile.write(
|
||||
"skipping addSynopsis step for " + fileName + "\n")
|
||||
interfaceFile = None
|
||||
if interfaceFile:
|
||||
tmpfile2=os.tmpfile()
|
||||
addSynopsis.addSynopsisToFile(interfaceFile,ifile,
|
||||
tmpfile2,logFile=logFile)
|
||||
tmpfile2 = os.tmpfile()
|
||||
addSynopsis.addSynopsisToFile(interfaceFile, ifile,
|
||||
tmpfile2, logFile=logFile)
|
||||
tmpfile2.seek(0)
|
||||
if tmpfile:
|
||||
tmpfile.close()
|
||||
tmpfile=tmpfile2
|
||||
ifile=tmpfile
|
||||
tmpfile = tmpfile2
|
||||
ifile = tmpfile
|
||||
hash_next = md5()
|
||||
hash_next.update(ifile.read())
|
||||
ifile.seek(0)
|
||||
if hash_prev.digest() == hash_next.digest():
|
||||
return ifile
|
||||
elif n_pretty_iter >= 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue