Salome HOME
chgt date copyright
[tools/eficas.git] / Traducteur / changeValeur.py
index 859342528aa7fd32a20bf00a59d5468f635f607b..07a040c752c929f9cd3ba8359e315d1b125b68dc 100644 (file)
@@ -1,9 +1,29 @@
 # -*- coding: utf-8 -*-
+# Copyright (C) 2007-2017   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
+#
+from Traducteur.utils import lineToDict
 import logging
-from dictErreurs import EcritErreur
-from dictErreurs import jdcSet
-from renamemocle import decaleLignesdeNBlancs
-from removemocle import removeMotCleInFact
+from Traducteur.dictErreurs import EcritErreur
+from Traducteur.load import jdcSet 
+from Traducteur.renamemocle import decaleLignesdeNBlancs
+from Traducteur.removemocle import removeMotCleInFact
+from Traducteur import regles
 
 
 #--------------------------------------------------------------------------
@@ -25,9 +45,14 @@ def ChangementValeur(jdc,command,motcle,DictNouvVal,liste=(),defaut=0):
              if indexTexteMC > len(liste_ligne_MC)-1 : break
              MaLigneGlob=jdc.getLines()[indexLigneGlob]
              MaLigneTexte=liste_ligne_MC[indexTexteMC]
-             for Valeur in DictNouvVal.keys() :
-                trouve=MaLigneTexte.find(Valeur)
-                if trouve > -1 :
+             for Valeur in DictNouvVal :
+                MaLigneTexteDict=lineToDict(MaLigneTexte)
+                trouvecol=MaLigneTexte.find(Valeur)
+                if trouvecol > -1:
+                    trouve=(Valeur==MaLigneTexteDict[trouvecol])
+                else:
+                    trouve=False
+                if trouve:
                    debut=MaLigneGlob.find(motcle)
                    if debut==-1 : debut=0
                   Nouveau=MaLigneGlob[debut:].replace(Valeur,DictNouvVal[Valeur])
@@ -47,52 +72,66 @@ def ChangementValeur(jdc,command,motcle,DictNouvVal,liste=(),defaut=0):
     if boolChange : jdc.reset(jdc.getSource())
              
 #--------------------------------------------------------------------------------
-def ChangementValeurDsMCF(jdc,command,fact,motcle,DictNouvVal,liste=(),defaut=0):
+def ChangementValeurDsMCF(jdc,command,fact,motcle,DictNouvVal,liste=(),ensemble=regles.SansRegle,defaut=0):
 #--------------------------------------------------------------------------------
 
     if command  not in jdcSet : return
     boolChange=0
     for c in jdc.root.childNodes:
-       if c.name != command  : continue
-       for mcF in c.childNodes:
-          if mcF.name != fact : continue
-          l=mcF.childNodes[:]
-          l.reverse()
-          for ll in l:
-             trouveUnMC=0
-             for mc in ll.childNodes:
-                if mc.name != motcle:continue
-                trouveUnMC=1
-                TexteMC=mc.getText(jdc)
-                liste_ligne_MC=TexteMC.splitlines()
-                indexLigneGlob=mc.lineno-1
-                indexTexteMC=0
-                while indexLigneGlob < mc.endline  :
-                   if indexTexteMC > len(liste_ligne_MC)-1 : break
-                   MaLigneGlob=jdc.getLines()[indexLigneGlob]
-                   MaLigneTexte=liste_ligne_MC[indexTexteMC]
-                   for Valeur in DictNouvVal.keys() :
-                      trouve=MaLigneTexte.find(Valeur)
-                      if trouve > -1 :
-                         debut=MaLigneGlob.find(motcle)
-                         if debut==-1 : debut=0
-                        Nouveau=MaLigneGlob[debut:].replace(Valeur,DictNouvVal[Valeur])
-                         Nouveau=MaLigneGlob[0:debut]+Nouveau
-                         jdc.getLines()[indexLigneGlob]=Nouveau
-                         MaLigneTexte=Nouveau # raccourci honteux mais ...
-                         MaLigneGlob=Nouveau
-                         if Valeur in liste :
-                            EcritErreur((command,fact,motcle,Valeur),indexLigneGlob)
-                         else :
-                            logging.info("Changement de %s par %s dans %s ligne %d",Valeur,DictNouvVal[Valeur],command,indexLigneGlob)
-                   boolChange=1
-                   indexLigneGlob=indexLigneGlob+1
-                   indexTexteMC=indexTexteMC+1
-             if (trouveUnMC == 0) and ( defaut == 1):
-                logging.warning("OPTION  (defaut) de CALCG à verifier ligne %s" ,c.lineno )                     
-                EcritErreur((command,fact,motcle,"DEFAUT"),c.lineno)
+        if c.name != command  : continue
+        for mcF in c.childNodes:
+            if mcF.name != fact : continue
+            l=mcF.childNodes[:]
+            l.reverse()
+            for ll in l:
+                trouveUnMC=0
+                for mc in ll.childNodes:
+                    if mc.name != motcle:continue                    
+                    if ensemble.verif(c) == 0 : continue
+                    trouveUnMC=1
+                    TexteMC=mc.getText(jdc)
+                    liste_ligne_MC=TexteMC.splitlines()
+                    indexLigneGlob=mc.lineno-1
+                    indexTexteMC=0
+                    while indexLigneGlob < mc.endline  :
+                        if indexTexteMC > len(liste_ligne_MC)-1 : break
+                        MaLigneGlob=jdc.getLines()[indexLigneGlob]
+                        MaLigneTexte=liste_ligne_MC[indexTexteMC]
+                        for Valeur in DictNouvVal :
+                            MaLigneTexteDict=lineToDict(MaLigneTexte)
+                            trouvecol=MaLigneTexte.find(Valeur)
+                            if trouvecol > -1:
+                                trouve=(Valeur==MaLigneTexteDict[trouvecol])
+                            else:
+                                trouve=False
+                            if trouve:
+                                debut=MaLigneGlob.find(motcle)
+                                if debut==-1 : debut=0
+                                Nouveau=MaLigneGlob[debut:].replace(Valeur,DictNouvVal[Valeur])
+                                Nouveau=MaLigneGlob[0:debut]+Nouveau
+                                jdc.getLines()[indexLigneGlob]=Nouveau
+                                MaLigneTexte=Nouveau # raccourci honteux mais ...
+                                MaLigneGlob=Nouveau
+                                if Valeur in liste :
+                                   EcritErreur((command,fact,motcle,Valeur),indexLigneGlob)
+                                else :
+                                   logging.info("Changement de %s par %s dans %s ligne %d",Valeur,DictNouvVal[Valeur],command,indexLigneGlob)
+                        boolChange=1
+                        indexLigneGlob=indexLigneGlob+1
+                        indexTexteMC=indexTexteMC+1
+                if (trouveUnMC == 0) and ( defaut == 1):
+                   logging.warning("OPTION  (defaut) de CALCG à verifier ligne %s" ,c.lineno )                     
+                   EcritErreur((command,fact,motcle,"DEFAUT"),c.lineno)
     if boolChange : jdc.reset(jdc.getSource())
              
+#--------------------------------------------------------------------------------
+def ChangementValeurDsMCFSiRegle(jdc,command,fact,motcle,DictNouvVal,liste_regles,defaut=0):
+#--------------------------------------------------------------------------------
+    if command not in jdcSet : return
+    mesRegles=regles.ensembleRegles(liste_regles)
+    liste=()
+    ChangementValeurDsMCF(jdc,command,fact,motcle,DictNouvVal,liste,mesRegles,defaut)
+             
 #---------------------------------------------------------------------------------------
 def ChangementValeurDsMCFAvecAvertissement(jdc, command, fact,motcle,DictNouvVal,liste):
 #---------------------------------------------------------------------------------------
@@ -122,7 +161,7 @@ def SuppressionValeurs(jdc, command,motcle,liste):
        for mc in c.childNodes:
           if mc.name != motcle : continue
           indexLigneGlob=mc.lineno-1
-          while indexLigneGlob < mc.endline  :
+          while indexLigneGlob < mc.endline-1  :
              MaLigneTexte = jdc.getLines()[indexLigneGlob]
              MaLigne=MaLigneTexte
              for Valeur in liste :
@@ -132,8 +171,8 @@ def SuppressionValeurs(jdc, command,motcle,liste):
                 chercheLigne=MaLigne[debutMC:]
                 trouve=chercheLigne.find(Valeur)
                 premier=0
-                if trouve > 0 : 
-                   debut=debut1 + chercheLigne[0:trouve]
+                if trouve > 1 : #on a au moins une quote
+                   debut=debut1 + chercheLigne[0:trouve-1]
                    index = -1
                    while (-1 * index) < len(debut) :
                       if (debut[index] == "(")  :
@@ -149,10 +188,18 @@ def SuppressionValeurs(jdc, command,motcle,liste):
                          assert(0)
                       index = index -1
                    debLigne = debut[0:index]
-                   fin=trouve+len(Valeur)
-                   if premier == 1 : fin = fin + 1 # on supprime la ,
+                   fin=trouve+len(Valeur)+1
+                   if premier == 1 and chercheLigne[fin] == ',': fin = fin + 1 # on supprime la ,
                    finLigne = chercheLigne[fin:]
-                   MaLigne=debLigne+finLigne
+                   MaLigne_tmp=debLigne+finLigne
+                   # Traitement ligne commancant par ,
+                   if len(MaLigne_tmp.strip()) > 0 :
+                      if MaLigne_tmp.strip()[0]==',' :
+                         MaLigne=MaLigne_tmp.strip()[1:]
+                      else :
+                         MaLigne=MaLigne_tmp[0:]
+                   else :
+                      MaLigne=MaLigne_tmp[0:]
                    boolChange=1
                 jdc.getLines()[indexLigneGlob]=MaLigne
              indexLigneGlob=indexLigneGlob+1