Skip to content
Snippets Groups Projects
Commit da6a9964 authored by RIETTE Sébastien's avatar RIETTE Sébastien
Browse files

Merge branch 'main' of github.com:SebastienRietteMTO/PHYEX_tools into main

parents 3dc755cd 63f81614
No related branches found
No related tags found
No related merge requests found
#!/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')
contentbyline = fin.readlines()
for i in contentbyline:
if "!$mnh_expand" in i:
fout.writelines("! $MNH EXPAND$ !\n")
fout.writelines(i)
elif "!$mnh_end_expand" in i:
fout.writelines(i)
fout.writelines("! $MNH END EXPAND$ !\n")
else:
fout.writelines(i)
fout.close()
fin.close()
def count_blank(text):
# Count number of space blank before a first character of a line
count = 0
for t in text:
if t == " ":
count = count+1
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
return correcttext
def firstchar(string):
# Return the first non-blank character of a string
for i in string:
if i == " ":
pass
else:
return i
def first7char(string):
# Return the 7 first non-blank character of a string
nb_blank = 0
for i in string:
if i == " ":
nb_blank = nb_blank+1
else:
return string[nb_blank:nb_blank+7]
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(... &
# ......) 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': ""}
for i in contentbyline:
i7 = first7char(i)
if passNextLine['Pass']: # Second line with & for if or #if(n)def
textwrite = i
passNextLine['Pass'] = False
if passNextLine['Reason'] == "IF":
indent_score = indent_score+1
elif passNextLine['Reason'] == "#ifdef":
textwrite = check_indent(ncurrline, indent_score, i)
elif passNextLine['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" in i7 and "THEN" in i:
indent_score = indent_score+1
else:
sys.exit("Reason for passing the line not defined")
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) == "!":
textwrite = i
# 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)
# #ifdef handling = pass to next lines with no indent
elif "#if" in i7: # ifdef or ifndef
print(i)
textwrite = i
passNextLine['Pass'], passNextLine['Reason'] = (True, "#ifdef")
elif "#else" in i7:
textwrite = i
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")
# exclude IF in one line (without THEN)
elif "IF" in i7 and "THEN" in i:
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")
# exclude IF in one line (without THEN)
elif "IF" in i7 and "THEN" in i:
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")
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 30 22:32:56 2022
@author: Quentin
"""
def count_blank(text):
count=0
for t in text:
if t == " ":
count=count+1
else:
break
return count
def check_indent(n,indent_score,worktext):
nblank=count_blank(worktext)
rawtext=worktext[nblank:]
correcttext=' '*((indent_score)*1) + rawtext
return correcttext
filein = open('mode_tke_eps_sources.f90','r')
fileout = open('new.F90','w')
contentbyline = filein.readlines()
ncurrline=0
indent_score=0
for i in contentbyline:
#Si la ligne est un commentaire (commence par !) : next (ne pas indenter)
#
# ligne de calcul sans ajout d'indentation (DO, ENDDDO, IF, ENDIF)
if indent_score > 0 and ("DO J" not in i and "ENDDO" not in i):
textwrite=check_indent(ncurrline,indent_score,i)
elif "DO J" in i:
textwrite=check_indent(ncurrline,indent_score,i)
indent_score = indent_score+1
elif "ENDDO" in i:
indent_score = indent_score-1
textwrite=check_indent(ncurrline,indent_score,i)
#Ajouter le case IF et ENDIF qui ajoute de lindentation
else:
textwrite=i
ncurrline=ncurrline+1
fileout.writelines(textwrite)
fileout.close()
filein.close()
\ No newline at end of file
...@@ -13,6 +13,8 @@ set -e ...@@ -13,6 +13,8 @@ set -e
repository_https=https://github.com/QuentinRodier/PHYEX.git repository_https=https://github.com/QuentinRodier/PHYEX.git
repository_ssh=git@github.com:QuentinRodier/PHYEX.git repository_ssh=git@github.com:QuentinRodier/PHYEX.git
PHYEXTOOLSDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
###### COMMAND LINE ARGUMENTS ###### COMMAND LINE ARGUMENTS
function usage { function usage {
echo "Usage: $0 [-h] [-c CHECKOUT_POINT] [-m MODEL] [-D OPTION [-D OPTION [...]]]] \\" echo "Usage: $0 [-h] [-c CHECKOUT_POINT] [-m MODEL] [-D OPTION [-D OPTION [...]]]] \\"
...@@ -165,8 +167,10 @@ if [ -n "${mnh_expand_options-}" ]; then ...@@ -165,8 +167,10 @@ if [ -n "${mnh_expand_options-}" ]; then
function apply_mnh_expand () { function apply_mnh_expand () {
if grep mnh_expand $1 > /dev/null 2>&1 ; then if grep mnh_expand $1 > /dev/null 2>&1 ; then
[ $verbose -gt 1 ] && echo "Applying mnh_expand on $1" [ $verbose -gt 1 ] && echo "Applying mnh_expand on $1"
mnh_expand -DMNH_EXPAND_NOCPP $mnh_expand_options $1 > tempo_mnh_expand $PHYEXTOOLSDIR/correct_indent.py $1 "detect"
mv tempo_mnh_expand $1 mnh_expand -DMNH_EXPAND_NOCPP $mnh_expand_options $1_EXPAND > tempo_mnh_expand
$PHYEXTOOLSDIR/correct_indent.py tempo_mnh_expand "indent"
mv tempo_mnh_expand_CORRECT_INDENT $1
fi fi
} }
if [ -n "${model-}" ]; then if [ -n "${model-}" ]; then
......
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