Salome HOME
pb de check box
[tools/eficas.git] / Traducteur / inseremocle.py
index b8d345b40070b52975c994e6898986d8ee4e188b..8e517dbd05b6d536d3abffec70d54567224ff941 100644 (file)
 # -*- coding: utf-8 -*-
+# Copyright (C) 2007-2013   EDF R&D
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+#
 import logging
-from parseur import FactNode
+from Traducteur.parseur import FactNode
+from Traducteur.load import jdcSet 
+from Traducteur.dictErreurs import EcritErreur
 import string
-debug=1
+from Traducteur import regles
+debug=0
 
 
 #-----------------------------------
-def inseremotcle(jdc,recepteur,texte):
+def insereMotCle(jdc,recepteur,texte):
 #-----------------------------------
 # appelle la methode selon la classe 
 # du recepteur
 
+    if recepteur.name  not in jdcSet : return
     if recepteur.__class__.__name__ == "Command" :
        if debug : print " Ajout de ", texte, "dans la commande : " ,recepteur.name 
-       inseremotcleincommand(jdc,recepteur,texte)
+       insereMotCleDansCommande(jdc,recepteur,texte)
        return
 
 
 #--------------------------------------------
-def inseremotcleincommand(jdc,command,texte):
+def insereMotCleDansCommande(jdc,command,texte):
 #---------------------------------------------
 # insere le texte comme 1er mot cle
 # de la commande
-    if debug : print "inseremotcle ", texte , " dans ", command.name
-    numcol=chercheDebut1mot(jdc,command)
+    if command.name  not in jdcSet : return
+    if debug : print "insereMotCle ", texte , " dans ", command.name
+    numcol=chercheDebut1Mot(jdc,command)
     if numcol > 0 :
        jdc.splitLine(command.lineno,numcol)
-    debut=chercheAlignement(jdc,command)
-    texte=debut+texte+"\n"
+    indice = -1
+    while texte[indice] == " " or texte[indice] == "\n": 
+       indice = indice -1
+    if texte[indice] != "," : texte=texte+","
+    texteinfo=texte
+    texte=texte+'\n'
     jdc.addLine(texte,command.lineno) 
+    logging.info("Insertion de : %s ligne %d", texteinfo,command.lineno)
     if numcol > 0 :            # Les mots clefs etaient sur la même ligne
         jdc.joinLineandNext(command.lineno)
 
-#---------------------------------------------
-def inseremotcleinfacteur(jdc,facteur,texte):
-#-------------------------------------------------
-    if debug : print "inseremotcle ", texte , " dans ", facteur.name
-    ancien=jdc.getLine(facteur.lineno )
-    # On va chercher la dernier ) pour ajouter avant
+#-------------------------------------------------------------
+def insereMotCleDansFacteur(jdc,facteur,texte,plusieursFois=True):
+#----------------------------------------------------------------
+    if debug : print "insereMotCle ", texte , " dans ", facteur.name
+
+    if texte[-1] == "\n" : texte=texte[0:-1] 
+    ancien=jdc.getLine(facteur.lineno)
+
+    # On va chercher la derniere ) pour ajouter avant
     # on va verifier s il il y a un , avant
-    ligne,col,boolvirgule=chercheDerniereParenthese(jdc,facteur)
-    if col > 0 :
-       jdc.splitLine(ligne,col)
-    if boolvirgule == 0 :
-       jdc.addLine(",\n",ligne)
-       jdc.joinLineandNext(ligne)
-    debut=ancien.find("_F") + 3
-    aligne=debut*" "
-    # enleve les blancs en debut de texte
-    i = 0
-    while i < len(texte) :
-      if texte[i] != " " : break
-      i = i +1
-    texte=aligne+texte+"\n"
-    jdc.addLine(texte,ligne)
-    jdc.joinLineandNext(ligne+1)
-
-#---------------------------------------
-def chercheDerniereParenthese(jdc,facteur):
-#---------------------------------------
-    ligne=facteur.endline-1
-    col=-1
-    boolvirgule=0
-    trouveParent=0
-    while ( trouveParent == 0) :
-       texte=jdc.getLine(ligne)
-       col=texte.rfind(")")
-       if col < 0 :
-          ligne=ligne-1
-       else :
-          trouveParent=1
-    indice=col -1
-    while ( indice > -1 and texte[indice] == " " ):
-          indice = indice -1
-    if texte[indice]=="," :
-       boolvirgule = 1
-    return (ligne,col,boolvirgule)
+    # si le texte ne finit pas par une ","
+    # on en met une
+
+    indice = -1
+    while texte[indice] == " " : 
+       indice = indice -1
+    if texte[indice] != "," : 
+       texte=texte+","
+    if (texte.find("#") > -1) and (texte.find("#") < texte.find(",")) :
+          texte=texte+"\n,"
+          
+    texteinfo=texte
+    texte=texte+"\n"
+  
+    ligneaCouper=facteur.lineno
+    while ligneaCouper < facteur.endline + 1 :
+        trouve=0
+        trouveF=0
+        trouveP=0
+        indiceDeCoupe=0
+        while  ancien.find("_F") > 0 :
+            longueur=len(ancien)
+            indice=ancien.find("_F")
+            indiceParcours=0
+            # pour ne pas tenir compte des autres noms 
+            # Attention si 2 MCF sur la meme ligne (la 1ere)
+            if trouveF == 0 :
+                if ((ligneaCouper!=facteur.lineno) or ((ancien.find(facteur.name) < indice ) or (ancien.find(facteur.name) < 0))) :
+                   trouveF=1
+                   indiceParcours=indice + 2
+            # attention pour regler DEFI_FONCTION .. 
+                else :
+                   indiceDeCoupe=indiceDeCoupe+indice+2
+                   ancien=ancien[indice +2:]
+                   continue
+            if trouveF == 1 :
+                indiceDeCoupe=indiceDeCoupe+indice
+    #            print "indice de Parcours" ,indiceParcours
+    #            print ancien[indiceParcours] 
+    #            print ancien[indiceParcours+1] 
+    #            print ancien[indiceParcours+2] 
+                while  indiceParcours < longueur :
+                    if ancien[indiceParcours] == "(" :
+                        trouveP=1
+    #                    print "trouve"
+                        break
+                    if ancien[indiceParcours] != " " :
+                        trouveP=0
+    #                    print "mouv"
+                        break
+                    indiceParcours = indiceParcours+1
+            trouve = trouveP * trouveF
+            if trouve : break
+            ancien=ancien[indice+1:]
+        if trouve :
+            debut=indiceDeCoupe + 3
+            if(jdc.getLine(ligneaCouper)[debut:]!="\n"):
+                jdc.splitLine(ligneaCouper,debut)
+            jdc.addLine(texte,ligneaCouper)
+            jdc.joinLineandNext(ligneaCouper)
+            logging.info("Insertion de %s ligne %d", texteinfo,ligneaCouper)
+
+            # Gestion du cas particulier du mot clef facteur vide
+            if facteur.childNodes == []:
+                jdc.joinLineandNext(facteur.lineno)
+
+        ligneaCouper=ligneaCouper+1
+        ancien=jdc.getLine(ligneaCouper)
+        if not plusieursFois and trouve : break
+
 
 #-----------------------------------
-def chercheDebut1mot(jdc,command):
+def chercheDebut1Mot(jdc,command):
 #-----------------------------------
 # Retourne le numero de colonne si le 1er mot clef est 
 # sur la meme ligne que le mot clef facteur
@@ -90,12 +154,12 @@ def chercheDebut1mot(jdc,command):
        if node1.lineno == command.lineno :
           debut=node1.colno
     else:
-       debut=chercheDebutfacteur(jdc,command) 
+       debut=chercheDebutFacteur(jdc,command) 
     if debut == -1 and debug : print "attention!!! pb pour trouver le debut dans ", command
     return debut
 
 #-----------------------------------
-def chercheDebutfacteur(jdc,facteur):
+def chercheDebutFacteur(jdc,facteur):
 #-----------------------------------
     debut=-1
     ligne=jdc.getLines()[facteur.lineno]
@@ -104,7 +168,6 @@ def chercheDebutfacteur(jdc,facteur):
     return debut
     
 
-
 #-----------------------------------
 def chercheAlignement(jdc,command):
 #-----------------------------------
@@ -115,4 +178,113 @@ def chercheAlignement(jdc,command):
     nbBlanc=node1.colno
     return " "*nbBlanc
 
+#---------------------------------------------------------------------------------------------------------
+def chercheOperInsereFacteur(jdc,nomcommande,nouveau,ensemble=regles.SansRegle, estunFacteur=1, erreur=0):
+#--------------------------------------------------------------------------------------------------------
+# Cherche l oper
+# cree le texte
+# appelle insereMotCle pour ajouter le texte
+#
+    boolChange=0
+    if estunFacteur : 
+      texte=nouveau+"=_F(),"
+    else :
+      texte=nouveau
+    if nomcommande  not in jdcSet : return
+    commands= jdc.root.childNodes[:]
+    commands.reverse()
+    for c in commands:
+        if c.name != nomcommande:continue
+        if ensemble.verif(c) == 0 : continue
+        if erreur : EcritErreur((nomcommande,nouveau),c.lineno)
+        boolChange=1
+        insereMotCle(jdc,c,texte)
+    if boolChange : jdc.reset(jdc.getSource())
+
+#----------------------------------------------------------------------------------------
+def chercheOperInsereFacteurSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
+#----------------------------------------------------------------------------------------
+# Cherche l oper
+# cree le texte
+# appelle insereMotCle pour ajouter le texte
+#
+    if nomcommande  not in jdcSet : return
+    mesRegles=regles.ensembleRegles(liste_regles)
+    chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
+
+#----------------------------------------------------------------------------------------
+def chercheOperInsereMotCleSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=0):
+#----------------------------------------------------------------------------------------
+    if nomcommande  not in jdcSet : return
+    mesRegles=regles.ensembleRegles(liste_regles)
+    chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
+
+    
+#---------------------------------------------------------------------------------------------------------
+def chercheOperInsereFacteurSiRegleAvecAvertissement(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
+#---------------------------------------------------------------------------------------------------------
+    if nomcommande  not in jdcSet : return
+    mesRegles=regles.ensembleRegles(liste_regles)
+    chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur,erreur=1)
+
+#-------------------------------------------------------------------------------------------------
+def AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,ensemble=regles.SansRegle, estunFacteur=0):
+#-------------------------------------------------------------------------------------------------
+# Cherche la commande
+# Cherche le MCF
+# cree le texte
+# appelle insereMotCle pour ajouter le texte
+#
+    if commande  not in jdcSet : return
+    if estunFacteur : 
+      texte=nouveau+"=_F(),"
+    else :
+      texte=nouveau
+    commands= jdc.root.childNodes[:]
+    commands.reverse()
+    boolChange=0
+    for c in commands:
+        if c.name != commande : continue
+        for mcF in c.childNodes:
+            if mcF.name != fact : continue
+            if ensemble.verif(c) == 0 : continue
+            boolChange=1
+            insereMotCleDansFacteur(jdc,mcF,texte)
+    if boolChange : jdc.reset(jdc.getSource())
+
+#-------------------------------------------------------------------------------------------
+def AjouteMotClefDansFacteurSiRegle(jdc,commande,fact,nouveau,liste_regles,estunFacteur=0):
+#-------------------------------------------------------------------------------------------
+#
+    if commande  not in jdcSet : return
+    mesRegles=regles.ensembleRegles(liste_regles)
+    AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,mesRegles,estunFacteur)
 
+#-------------------------------------------------------------------------------------------
+def AjouteMotClefDansFacteurCourantSiRegle(jdc,commande,fact,nouveau,liste_regles):
+#-------------------------------------------------------------------------------------------
+#
+    if commande  not in jdcSet : return
+    ensemble=regles.ensembleRegles(liste_regles)
+    commands= jdc.root.childNodes[:]
+    commands.reverse()
+    boolChange=0
+    for c in commands:
+        if c.name != commande : continue
+        for mcF in c.childNodes:
+          if mcF.name != fact : continue
+          l=mcF.childNodes[:]
+          l.reverse()
+          for ll in l:
+             if ensemble.verif(ll) == 0 : continue
+             boolChange=1
+             n=ll.childNodes[0]
+             ligneaCouper=n.lineno-1
+             numcol=n.colno
+             jdc.splitLine(ligneaCouper+1,numcol)
+             texte=nouveau+",\n"
+             jdc.addLine(texte,ligneaCouper+1)
+             logging.info("Insertion de %s dans %s : ligne %d", nouveau,c.name,ligneaCouper+1)
+             if numcol > 0 :    
+                 jdc.joinLineandNext(ligneaCouper+1)
+    if boolChange : jdc.reset(jdc.getSource())