Salome HOME
merge avec les devloppts de l ete
[tools/eficas.git] / Traducteur / inseremocle.py
index f8307f5e1ed910dc37be11a40195644ad85f097f..f53adf39e564feb5ebbecb06e95b03c3813633e5 100644 (file)
@@ -1,10 +1,27 @@
 # -*- 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 dictErreurs import jdcSet
-from dictErreurs import EcritErreur
-import string
-import regles
+from Traducteur.parseur import FactNode
+from Traducteur.load import jdcSet 
+from Traducteur.dictErreurs import EcritErreur
+from Traducteur import regles
 debug=0
 
 
@@ -32,7 +49,7 @@ def insereMotCleDansCommande(jdc,command,texte):
     if numcol > 0 :
        jdc.splitLine(command.lineno,numcol)
     indice = -1
-    while texte[indice] == " " : 
+    while texte[indice] == " " or texte[indice] == "\n"
        indice = indice -1
     if texte[indice] != "," : texte=texte+","
     texteinfo=texte
@@ -42,9 +59,9 @@ def insereMotCleDansCommande(jdc,command,texte):
     if numcol > 0 :            # Les mots clefs etaient sur la même ligne
         jdc.joinLineandNext(command.lineno)
 
-#---------------------------------------------
-def insereMotCleDansFacteur(jdc,facteur,texte):
-#-------------------------------------------------
+#-------------------------------------------------------------
+def insereMotCleDansFacteur(jdc,facteur,texte,plusieursFois=True):
+#----------------------------------------------------------------
     if debug : print "insereMotCle ", texte , " dans ", facteur.name
 
     if texte[-1] == "\n" : texte=texte[0:-1] 
@@ -67,71 +84,60 @@ def insereMotCleDansFacteur(jdc,facteur,texte):
     texte=texte+"\n"
   
     ligneaCouper=facteur.lineno
-    trouve=0
-    trouveF=0
-    trouveP=0
     while ligneaCouper < facteur.endline + 1 :
-       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:]
-          
-       trouve = trouveP * trouveF
-       if trouve : break
-       ligneaCouper=ligneaCouper+1
-       ancien=jdc.getLine(ligneaCouper)
-         
-    if trouve :
-       debut=indiceDeCoupe + 3
-       jdc.splitLine(ligneaCouper,debut)
-    else :
-       print "Le traducteur ne sait pas faire"
-       assert 0
+        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)
 
-    # enleve les blancs en debut de texte
-    i = 0
-    while i < len(texte) :
-      if texte[i] != " " : break
-      i = i +1
+            # Gestion du cas particulier du mot clef facteur vide
+            if facteur.childNodes == []:
+                jdc.joinLineandNext(facteur.lineno)
 
-    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
 
 
 #-----------------------------------
@@ -204,6 +210,14 @@ def chercheOperInsereFacteurSiRegle(jdc,nomcommande,nouveau,liste_regles, estunF
     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):
@@ -231,12 +245,10 @@ def AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,ensemble=regles.SansRegle
     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
-          l=mcF.childNodes[:]
-          l.reverse()
-          boolChange=1
-          insereMotCleDansFacteur(jdc,mcF,texte)
+            if mcF.name != fact : continue
+            if ensemble.verif(c) == 0 : continue
+            boolChange=1
+            insereMotCleDansFacteur(jdc,mcF,texte)
     if boolChange : jdc.reset(jdc.getSource())
 
 #-------------------------------------------------------------------------------------------
@@ -247,3 +259,31 @@ def AjouteMotClefDansFacteurSiRegle(jdc,commande,fact,nouveau,liste_regles,estun
     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())