parser=argparse.ArgumentParser(description='Detecte les balises !$mnh_expand et !$mnh_end_expand et ajoute un commentaire avant et après pour corriger l\'indentation par la suite avec correct_indentation.py')
value=argparse.ArgumentParser()
parser.add_argument('file1',metavar='file1',type=str,help="file to check")
parser.add_argument('action',metavar='action',type=str,help="action (indent or detect")
args=parser.parse_args()
if"indent"inargs.action:
sys.exit(correct_indent(args.file1))
elif"detect"inargs.action:
sys.exit(detectMNH_expand(args.file1))
else:
sys.exit("Error : action should be indent or detect. Nothing has been done")
#!/usr/bin/env python3
defdetectMNH_expand(f):
# Adds MNH EXPAND comment before and after $mnh_expand$
fin=open(f,'r')
fout=open(f+'_EXPAND','w')
contentbyline=fin.readlines()
foriincontentbyline:
if"!$mnh_expand"ini:
fout.writelines("! $MNH EXPAND$ !\n")
fout.writelines(i)
elif"!$mnh_end_expand"ini:
fout.writelines(i)
fout.writelines("! $MNH END EXPAND$ !\n")
else:
fout.writelines(i)
fout.close()
fin.close()
defcount_blank(text):
# Count number of space blank before a first character of a line
count=0
fortintext:
ift=="":
count=count+1
else:
break
returncount
defcheck_indent(n,indent_score,worktext):
# Correct the indentation with respect to the indent_score
nblank=count_blank(worktext)
rawtext=worktext[nblank:]
correcttext=''*((indent_score)*2)+rawtext
returncorrecttext
deffirstchar(string):
# Return the first non-blank character of a string
foriinstring:
ifi=="":pass
else:returni
deffirst7char(string):
# Return the 7 first non-blank character of a string
nb_blank=0
foriinstring:
ifi=="":nb_blank=nb_blank+1
else:returnstring[nb_blank:nb_blank+7]
defcorrect_indent(f):
importsys
# Correct the indentation between MNH EXPAND comment
# Does not change the indentation outside MNH EXPAND > ... < MNH END EXPAND
# Does not indent comment lines
# Handles IF in two lines (e.g IF(... &
# ......) THEN
# TODO : handles more than two lines of & with IF
# TODO : do not correct indentation for & within an regular line
# To improve : the indentation correction is weird (strict) if the indentation before
# the MNH EXPAND comments is not respected (over-indentation)
fin=open(f,'r')
fout=open(f+'_CORRECT_INDENT','w')
contentbyline=fin.readlines()
ncurrline=0
indent_score=0
expand_score=0
passNextLine={'Pass':False,'Reason':""}
foriincontentbyline:
i7=first7char(i)
ifpassNextLine['Pass']:#Second line with & for if or #if(n)def
textwrite=i
passNextLine['Pass']=False
ifpassNextLine['Reason']=="IF":
indent_score=indent_score+1
elifpassNextLine['Reason']=="#ifdef":
textwrite=check_indent(ncurrline,indent_score,i)
elifpassNextLine['Reason']=="#else":
textwrite=check_indent(ncurrline,indent_score,i)
# ONLY IF present in between #ifdef is handled
# If more test is needed (present in the fortran code), duplicate test here
if"IF"ini7and"THEN"ini:
indent_score=indent_score+1
else:
sys.exit("Reason for passing the line not defined")
elif"! $MNH EXPAND$ !"ini:
expand_score=expand_score+1
textwrite=""
elif"! $MNH END EXPAND$ !"ini:
expand_score=expand_score-1
textwrite=""
# Ignore comment lines
eliffirstchar(i)=="!":
textwrite=i
# Correct indentation only in between $MNH EXPAND$ and $MNH END EXPAND$
elif"IF"ini7and"THEN"ini:#exclude IF in one line (without THEN)
indent_score=indent_score+1
elif"DO J"ini7:
indent_score=indent_score+1
elif ("ENDDO"ini7)or("END DO"ini7):
indent_score=indent_score-1
elif ("END IF"ini7)or("ENDIF"ini7):
indent_score=indent_score-1
textwrite=i
ncurrline=ncurrline+1
fout.writelines(textwrite)
fout.close()
fin.close()
if__name__=="__main__":
importargparse
importsys
parser=argparse.ArgumentParser(description='Detecte les balises !$mnh_expand et !$mnh_end_expand et ajoute un commentaire avant et après pour corriger l\'indentation par la suite avec correct_indentation.py')
value=argparse.ArgumentParser()
parser.add_argument('file1',metavar='file1',type=str,help="file to check")
parser.add_argument('action',metavar='action',type=str,help="action (indent or detect")
args=parser.parse_args()
if"indent"inargs.action:
sys.exit(correct_indent(args.file1))
elif"detect"inargs.action:
sys.exit(detectMNH_expand(args.file1))
else:
sys.exit("Error : action should be indent or detect. Nothing has been done")