From 2d34e8e586f2a55937dfde078881e51ab903b14d Mon Sep 17 00:00:00 2001
From: Quentin Rodier <quentin.rodier@meteo.fr>
Date: Thu, 14 Apr 2022 23:54:38 +0200
Subject: [PATCH] Quentin 14/04/2022: Correct indentation script and incorpore
 in prep_code

---
 correct_indent.py                   | 109 ++++++++++++++++++++++++++++
 mnh_expand/parser_fortran_python.py |  49 -------------
 prep_code.sh                        |   8 +-
 3 files changed, 115 insertions(+), 51 deletions(-)
 create mode 100755 correct_indent.py
 delete mode 100644 mnh_expand/parser_fortran_python.py

diff --git a/correct_indent.py b/correct_indent.py
new file mode 100755
index 000000000..5c95f933f
--- /dev/null
+++ b/correct_indent.py
@@ -0,0 +1,109 @@
+#!/usr/bin/env python3
+def detectMNH_expand(f):
+    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=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
+
+def firstcharacnotblank(string):
+    for i in string:
+        if i == " ": pass
+        else: return i
+
+def correct_indent(f):
+    fin = open(f,'r')
+    fout  = open(f+'_CORRECT_INDENT','w')
+    contentbyline = fin.readlines()
+    
+    ncurrline=0
+    indent_score=0
+    expand_score=0
+    for i in contentbyline:
+        if "! $MNH EXPAND$ !" in i:
+            expand_score = expand_score + 1
+            textwrite = ""
+        elif "! $MNH END EXPAND$ !" in i:
+            expand_score = expand_score - 1
+            textwrite = ""
+        # Correct indentation only in between $MNH EXPAND$ and $MNH END EXPAND$
+        elif expand_score >= 1:
+            # Do not indent comment lines within mnh_expand
+            if firstcharacnotblank(i) == "!":
+                textwrite=i
+            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):
+                textwrite=check_indent(ncurrline,indent_score,i)
+            elif "ELSE" in i:
+                indent_score = indent_score+1        
+                textwrite=check_indent(ncurrline,indent_score,i)
+                indent_score = indent_score-1
+            elif "THEN" in i:
+                textwrite=check_indent(ncurrline,indent_score,i)
+                indent_score = indent_score+1        
+            elif "DO J" in i:
+                textwrite=check_indent(ncurrline,indent_score,i)
+                indent_score = indent_score+1
+            elif ("ENDDO" in i) or ("END DO" in i):
+                indent_score = indent_score-1
+                textwrite=check_indent(ncurrline,indent_score,i)
+            elif ("END IF" in i) or ("ENDIF" in i):
+                indent_score = indent_score-1
+                textwrite=check_indent(ncurrline,indent_score,i)       
+            else:
+                textwrite=i
+        else: # not EXPAND lines nor within mnh_expand
+            if "THEN" in i:
+                indent_score = indent_score+1        
+            elif "DO J" in i:
+                indent_score = indent_score+1
+            elif ("ENDDO" in i) or ("END DO" in i):
+                indent_score = indent_score-1
+            elif ("END IF" in i) or ("ENDIF" in i):
+                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")
diff --git a/mnh_expand/parser_fortran_python.py b/mnh_expand/parser_fortran_python.py
deleted file mode 100644
index c4bf4d66b..000000000
--- a/mnh_expand/parser_fortran_python.py
+++ /dev/null
@@ -1,49 +0,0 @@
-# -*- 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
diff --git a/prep_code.sh b/prep_code.sh
index 3c3cb1ba0..b5a476399 100755
--- a/prep_code.sh
+++ b/prep_code.sh
@@ -13,6 +13,8 @@ set -e
 repository_https=https://github.com/QuentinRodier/PHYEX.git
 repository_ssh=git@github.com:QuentinRodier/PHYEX.git
 
+PHYEXTOOLSDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+
 ###### COMMAND LINE ARGUMENTS
 function usage {
   echo "Usage: $0 [-h] [-c CHECKOUT_POINT] [-m MODEL] [-D OPTION [-D OPTION [...]]]] \\"
@@ -157,8 +159,10 @@ if [ -n "${mnh_expand_options-}" ]; then
   function apply_mnh_expand () {
     if grep mnh_expand $1 > /dev/null 2>&1 ; then
       [ $verbose -gt 1 ] && echo "Applying mnh_expand on $1"
-      mnh_expand -DMNH_EXPAND_NOCPP $mnh_expand_options $1 > tempo_mnh_expand
-      mv tempo_mnh_expand $1
+      $PHYEXTOOLSDIR/correct_indent.py $1 "detect"
+      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
   }
   if [ -n "${model-}" ]; then
-- 
GitLab