Skip to content
Snippets Groups Projects
Commit b668d8d5 authored by RODIER Quentin's avatar RODIER Quentin
Browse files

Quentin 14/04/2022: Correct indent for #ifdef, multiple IF lines and add comments

parent a7216ad2
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3 #!/usr/bin/env python3
def detectMNH_expand(f):
fin = open(f,'r') def detectMNH_expand(f):
fout = open(f+'_EXPAND','w') # Adds MNH EXPAND comment before and after $mnh_expand$
contentbyline = fin.readlines() fin = open(f,'r')
fout = open(f+'_EXPAND','w')
for i in contentbyline: contentbyline = fin.readlines()
if "!$mnh_expand" in i:
fout.writelines("! $MNH EXPAND$ !\n") for i in contentbyline:
fout.writelines(i) if "!$mnh_expand" in i:
elif "!$mnh_end_expand" in i: fout.writelines("! $MNH EXPAND$ !\n")
fout.writelines(i) fout.writelines(i)
fout.writelines("! $MNH END EXPAND$ !\n") elif "!$mnh_end_expand" in i:
else: fout.writelines(i)
fout.writelines(i) fout.writelines("! $MNH END EXPAND$ !\n")
else:
fout.close() fout.writelines(i)
fin.close()
fout.close()
def count_blank(text): fin.close()
count=0
for t in text: def count_blank(text):
if t == " ": # Count number of space blank before a first character of a line
count=count+1 count=0
else: for t in text:
break if t == " ":
return count count=count+1
else:
def check_indent(n,indent_score,worktext): break
nblank=count_blank(worktext) return count
rawtext=worktext[nblank:]
correcttext=' '*((indent_score)*1) + rawtext def check_indent(n,indent_score,worktext):
return correcttext # Correct the indentation with respect to the indent_score
nblank=count_blank(worktext)
def firstcharacnotblank(string): rawtext=worktext[nblank:]
for i in string: correcttext=' '*((indent_score)*2) + rawtext
if i == " ": pass return correcttext
else: return i
def firstchar(string):
def correct_indent(f): # Return the first non-blank character of a string
fin = open(f,'r') for i in string:
fout = open(f+'_CORRECT_INDENT','w') if i == " ": pass
contentbyline = fin.readlines() else: return i
ncurrline=0 def first7char(string):
indent_score=0 # Return the 7 first non-blank character of a string
expand_score=0 nb_blank=0
for i in contentbyline: for i in string:
if "! $MNH EXPAND$ !" in i: if i == " ": nb_blank=nb_blank+1
expand_score = expand_score + 1 else: return string[nb_blank:nb_blank+7]
textwrite = ""
elif "! $MNH END EXPAND$ !" in i:
expand_score = expand_score - 1 def correct_indent(f):
textwrite = "" import sys
# Correct indentation only in between $MNH EXPAND$ and $MNH END EXPAND$ # Correct the indentation between MNH EXPAND comment
elif expand_score >= 1: # Does not change the indentation outside MNH EXPAND > ... < MNH END EXPAND
# Do not indent comment lines within mnh_expand # Does not indent comment lines
if firstcharacnotblank(i) == "!": # Handles IF in two lines (e.g IF(... &
textwrite=i # ......) THEN
elif indent_score > 0 and ("DO J" not in i and "ENDDO" not in i and "END IF" not in i and "ENDIF" not in i and "THEN" not in i and "ELSE" not in i): # TODO : handles more than two lines of & with IF
textwrite=check_indent(ncurrline,indent_score,i) # TODO : do not correct indentation for & within an regular line
elif "ELSE" in i: # To improve : the indentation correction is weird (strict) if the indentation before
indent_score = indent_score+1 # the MNH EXPAND comments is not respected (over-indentation)
textwrite=check_indent(ncurrline,indent_score,i) fin = open(f,'r')
indent_score = indent_score-1 fout = open(f+'_CORRECT_INDENT','w')
elif "THEN" in i: contentbyline = fin.readlines()
textwrite=check_indent(ncurrline,indent_score,i)
indent_score = indent_score+1 ncurrline=0
elif "DO J" in i: indent_score=0
textwrite=check_indent(ncurrline,indent_score,i) expand_score=0
indent_score = indent_score+1 passNextLine = {'Pass':False, 'Reason':"" }
elif ("ENDDO" in i) or ("END DO" in i): for i in contentbyline:
indent_score = indent_score-1 i7=first7char(i)
textwrite=check_indent(ncurrline,indent_score,i) if passNextLine['Pass']: #Second line with & for if or #if(n)def
elif ("END IF" in i) or ("ENDIF" in i): textwrite=i
indent_score = indent_score-1 passNextLine['Pass']=False
textwrite=check_indent(ncurrline,indent_score,i) if passNextLine['Reason'] == "IF":
else: indent_score = indent_score+1
textwrite=i elif passNextLine['Reason'] == "#ifdef":
else: # not EXPAND lines nor within mnh_expand textwrite=check_indent(ncurrline,indent_score,i)
if "THEN" in i: elif passNextLine['Reason'] == "#else":
indent_score = indent_score+1 textwrite=check_indent(ncurrline,indent_score,i)
elif "DO J" in i: # ONLY IF present in between #ifdef is handled
indent_score = indent_score+1 # If more test is needed (present in the fortran code), duplicate test here
elif ("ENDDO" in i) or ("END DO" in i): if "IF" in i7 and "THEN" in i:
indent_score = indent_score-1 indent_score = indent_score+1
elif ("END IF" in i) or ("ENDIF" in i): else:
indent_score = indent_score-1 sys.exit("Reason for passing the line not defined")
textwrite=i elif "! $MNH EXPAND$ !" in i:
ncurrline=ncurrline+1 expand_score = expand_score + 1
fout.writelines(textwrite) textwrite = ""
elif "! $MNH END EXPAND$ !" in i:
fout.close() expand_score = expand_score - 1
fin.close() textwrite = ""
# Ignore comment lines
if __name__ == "__main__": elif firstchar(i) == "!":
import argparse textwrite=i
import sys # Correct indentation only in between $MNH EXPAND$ and $MNH END EXPAND$
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' ) elif expand_score >= 1:
value = argparse.ArgumentParser() if indent_score > 0 and ("DO J" not in i and "ENDDO" not in i and "END DO" not in i and "END IF" not in i and "ENDIF" not in i and "THEN" not in i and "ELSE" not in i and "#if" not in i and "#else" not in i and "#endif" not in i):
parser.add_argument('file1', metavar='file1', type=str, help="file to check") textwrite=check_indent(ncurrline,indent_score,i)
parser.add_argument('action', metavar='action', type=str, help="action (indent or detect") # #ifdef handling = pass to next lines with no indent
args = parser.parse_args() elif "#if" in i7: #ifdef or ifndef
if "indent" in args.action: print(i)
sys.exit(correct_indent(args.file1)) textwrite=i
elif "detect" in args.action: passNextLine['Pass'],passNextLine['Reason']= (True, "#ifdef")
sys.exit(detectMNH_expand(args.file1)) elif "#else" in i7:
else: textwrite=i
sys.exit("Error : action should be indent or detect. Nothing has been done") passNextLine['Pass'],passNextLine['Reason']= (True, "#else")
elif '#endif' in i7:
textwrite=i
elif "ELSE" in i7: #ELSE or ELSEIF
indent_score = indent_score-1
textwrite=check_indent(ncurrline,indent_score,i)
indent_score = indent_score+1
elif "IF" in i7 and "&" in i: #IF on two lines #TODO on > 2 lines
textwrite=i
passNextLine['Pass'],passNextLine['Reason']= (True, "IF")
elif "IF" in i7 and "THEN" in i: #exclude IF in one line (without THEN)
textwrite=check_indent(ncurrline,indent_score,i)
indent_score = indent_score+1
elif "DO J" in i7:
textwrite=check_indent(ncurrline,indent_score,i)
indent_score = indent_score+1
elif ("ENDDO" in i7) or ("END DO" in i7):
indent_score = indent_score-1
textwrite=check_indent(ncurrline,indent_score,i)
elif ("END IF" in i7) or ("ENDIF" in i7):
indent_score = indent_score-1
textwrite=check_indent(ncurrline,indent_score,i)
else:
textwrite=i
else: # not EXPAND lines nor within mnh_expand
if "ELSE" in i7: #ELSE or ELSEIF
pass #no increase in indent
elif "IF" in i7 and "&" in i: #IF on two lines #TODO on > 2 lines
passNextLine['Pass'],passNextLine['Reason']= (True, "IF")
elif "IF" in i7 and "THEN" in i: #exclude IF in one line (without THEN)
indent_score = indent_score+1
elif "DO J" in i7:
indent_score = indent_score+1
elif ("ENDDO" in i7) or ("END DO" in i7):
indent_score = indent_score-1
elif ("END IF" in i7) or ("ENDIF" in i7):
indent_score = indent_score-1
textwrite=i
ncurrline=ncurrline+1
fout.writelines(textwrite)
fout.close()
fin.close()
if __name__ == "__main__":
import argparse
import sys
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" in args.action:
sys.exit(correct_indent(args.file1))
elif "detect" in args.action:
sys.exit(detectMNH_expand(args.file1))
else:
sys.exit("Error : action should be indent or detect. Nothing has been done")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment