Newer
Older

RODIER Quentin
committed
#!/usr/bin/env python3
def detectMNH_expand(f):
# Adds MNH EXPAND comment before and after $mnh_expand$
fin = open(f, 'r')
fout = open(f+'_EXPAND', 'w')

RODIER Quentin
committed
contentbyline = fin.readlines()

RODIER Quentin
committed
for i in contentbyline:
if "!$mnh_expand" in i:
fout.writelines("! $MNH EXPAND$ !\n")

RODIER Quentin
committed
elif "!$mnh_end_expand" in i:
fout.writelines(i)
fout.writelines("! $MNH END EXPAND$ !\n")

RODIER Quentin
committed
fout.writelines(i)

RODIER Quentin
committed
fout.close()
fin.close()

RODIER Quentin
committed
def count_blank(text):
# Count number of space blank before a first character of a line
count = 0

RODIER Quentin
committed
for t in text:
if t == " ":

RODIER Quentin
committed
else:
break
return count
def check_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

RODIER Quentin
committed
return correcttext

RODIER Quentin
committed
def firstchar(string):
# Return the first non-blank character of a string

RODIER Quentin
committed
for i in string:
if i == " ":
pass
else:
return i

RODIER Quentin
committed
def first7char(string):
# Return the 7 first non-blank character of a string
nb_blank = 0

RODIER Quentin
committed
for i in string:
if i == " ":
nb_blank = nb_blank+1
else:
return string[nb_blank:nb_blank+7]

RODIER Quentin
committed
def correct_indent(f):
import sys
# 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(... &

RODIER Quentin
committed
# 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')

RODIER Quentin
committed
contentbyline = fin.readlines()
ncurrline = 0
indent_score = 0
expand_score = 0
passNextLine = {'Pass': False, 'Reason': ""}

RODIER Quentin
committed
for i in contentbyline:
i7 = first7char(i)
if passNextLine['Pass']: # Second line with & for if or #if(n)def
textwrite = i
passNextLine['Pass'] = False

RODIER Quentin
committed
if passNextLine['Reason'] == "IF":
indent_score = indent_score+1
elif passNextLine['Reason'] == "#ifdef":
textwrite = check_indent(ncurrline, indent_score, i)

RODIER Quentin
committed
elif passNextLine['Reason'] == "#else":
textwrite = check_indent(ncurrline, indent_score, i)

RODIER Quentin
committed
# ONLY IF present in between #ifdef is handled
# If more test is needed (present in the fortran code), duplicate test here
if "IF" in i7 and "THEN" in i:
indent_score = indent_score+1
else:
sys.exit("Reason for passing the line not defined")

RODIER Quentin
committed
elif "! $MNH EXPAND$ !" in i:
expand_score = expand_score + 1
textwrite = ""
elif "! $MNH END EXPAND$ !" in i:
expand_score = expand_score - 1
textwrite = ""
# Ignore comment lines
elif firstchar(i) == "!":

RODIER Quentin
committed
# Correct indentation only in between $MNH EXPAND$ and $MNH END EXPAND$
elif expand_score >= 1:
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):
textwrite = check_indent(ncurrline, indent_score, i)

RODIER Quentin
committed
# #ifdef handling = pass to next lines with no indent
elif "#if" in i7: # ifdef or ifndef

RODIER Quentin
committed
print(i)
textwrite = i
passNextLine['Pass'], passNextLine['Reason'] = (True, "#ifdef")

RODIER Quentin
committed
elif "#else" in i7:
textwrite = i
passNextLine['Pass'], passNextLine['Reason'] = (True, "#else")

RODIER Quentin
committed
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")
# exclude IF in one line (without THEN)
elif "IF" in i7 and "THEN" in i:
textwrite = check_indent(ncurrline, indent_score, i)

RODIER Quentin
committed
indent_score = indent_score+1
elif "DO J" in i7:
textwrite = check_indent(ncurrline, indent_score, i)

RODIER Quentin
committed
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)

RODIER Quentin
committed
elif ("END IF" in i7) or ("ENDIF" in i7):
indent_score = indent_score-1
textwrite = check_indent(ncurrline, indent_score, i)

RODIER Quentin
committed
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")
# exclude IF in one line (without THEN)
elif "IF" in i7 and "THEN" in i:
indent_score = indent_score+1

RODIER Quentin
committed
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

RODIER Quentin
committed
fout.writelines(textwrite)

RODIER Quentin
committed
fout.close()
fin.close()

RODIER Quentin
committed
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')

RODIER Quentin
committed
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")

RODIER Quentin
committed
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")