Salome HOME
Version cmt-01
[tools/eficas.git] / Traducteur / changeValeur.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2012   EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 import logging
21 from dictErreurs import EcritErreur
22 from dictErreurs import jdcSet
23 from renamemocle import decaleLignesdeNBlancs
24 from removemocle import removeMotCleInFact
25
26
27 #--------------------------------------------------------------------------
28 def ChangementValeur(jdc,command,motcle,DictNouvVal,liste=(),defaut=0):
29 #--------------------------------------------------------------------------
30     if command  not in jdcSet : return
31     boolChange=0
32     for c in jdc.root.childNodes:
33        if c.name != command  : continue
34        trouveUnMC=0
35        for mc in c.childNodes:
36           if mc.name != motcle : continue
37           trouveUnMC=1
38           TexteMC=mc.getText(jdc)
39           liste_ligne_MC=TexteMC.splitlines()
40           indexLigneGlob=mc.lineno-1
41           indexTexteMC=0
42           while indexLigneGlob < mc.endline  :
43              if indexTexteMC > len(liste_ligne_MC)-1 : break
44              MaLigneGlob=jdc.getLines()[indexLigneGlob]
45              MaLigneTexte=liste_ligne_MC[indexTexteMC]
46              for Valeur in DictNouvVal.keys() :
47                 trouve=MaLigneTexte.find(Valeur)
48                 if trouve > -1 :
49                    debut=MaLigneGlob.find(motcle)
50                    if debut==-1 : debut=0
51                    Nouveau=MaLigneGlob[debut:].replace(Valeur,DictNouvVal[Valeur])
52                    Nouveau=MaLigneGlob[0:debut]+Nouveau
53                    jdc.getLines()[indexLigneGlob]=Nouveau
54                    MaLigneTexte=Nouveau # raccourci honteux mais ...
55                    MaLigneGlob=Nouveau
56                    if Valeur in liste :
57                       EcritErreur((command,motcle,Valeur),indexLigneGlob)
58                    else :
59                       logging.info("Changement de %s par %s dans %s ligne %d",Valeur,DictNouvVal[Valeur],command,indexLigneGlob)
60                    boolChange=1
61              indexLigneGlob=indexLigneGlob+1
62              indexTexteMC=indexTexteMC+1
63        if (trouveUnMC == 0) and ( defaut == 1):
64           EcritErreur((command,motcle,"DEFAUT"),c.lineno)
65     if boolChange : jdc.reset(jdc.getSource())
66              
67 #--------------------------------------------------------------------------------
68 def ChangementValeurDsMCF(jdc,command,fact,motcle,DictNouvVal,liste=(),defaut=0):
69 #--------------------------------------------------------------------------------
70
71     if command  not in jdcSet : return
72     boolChange=0
73     for c in jdc.root.childNodes:
74        if c.name != command  : continue
75        for mcF in c.childNodes:
76           if mcF.name != fact : continue
77           l=mcF.childNodes[:]
78           l.reverse()
79           for ll in l:
80              trouveUnMC=0
81              for mc in ll.childNodes:
82                 if mc.name != motcle:continue
83                 trouveUnMC=1
84                 TexteMC=mc.getText(jdc)
85                 liste_ligne_MC=TexteMC.splitlines()
86                 indexLigneGlob=mc.lineno-1
87                 indexTexteMC=0
88                 while indexLigneGlob < mc.endline  :
89                    if indexTexteMC > len(liste_ligne_MC)-1 : break
90                    MaLigneGlob=jdc.getLines()[indexLigneGlob]
91                    MaLigneTexte=liste_ligne_MC[indexTexteMC]
92                    for Valeur in DictNouvVal.keys() :
93                       trouve=MaLigneTexte.find(Valeur)
94                       if trouve > -1 :
95                          debut=MaLigneGlob.find(motcle)
96                          if debut==-1 : debut=0
97                          Nouveau=MaLigneGlob[debut:].replace(Valeur,DictNouvVal[Valeur])
98                          Nouveau=MaLigneGlob[0:debut]+Nouveau
99                          jdc.getLines()[indexLigneGlob]=Nouveau
100                          MaLigneTexte=Nouveau # raccourci honteux mais ...
101                          MaLigneGlob=Nouveau
102                          if Valeur in liste :
103                             EcritErreur((command,fact,motcle,Valeur),indexLigneGlob)
104                          else :
105                             logging.info("Changement de %s par %s dans %s ligne %d",Valeur,DictNouvVal[Valeur],command,indexLigneGlob)
106                    boolChange=1
107                    indexLigneGlob=indexLigneGlob+1
108                    indexTexteMC=indexTexteMC+1
109              if (trouveUnMC == 0) and ( defaut == 1):
110                 logging.warning("OPTION  (defaut) de CALCG à verifier ligne %s" ,c.lineno )                     
111                 EcritErreur((command,fact,motcle,"DEFAUT"),c.lineno)
112     if boolChange : jdc.reset(jdc.getSource())
113              
114 #---------------------------------------------------------------------------------------
115 def ChangementValeurDsMCFAvecAvertissement(jdc, command, fact,motcle,DictNouvVal,liste):
116 #---------------------------------------------------------------------------------------
117     if command  not in jdcSet : return
118     defaut=0
119     if liste[-1] == "defaut" : 
120        defaut=1
121     ChangementValeurDsMCF(jdc,command,fact,motcle,DictNouvVal,liste,defaut)
122
123 #--------------------------------------------------------------------------
124 def ChangementValeurAvecAvertissement(jdc, command,motcle,DictNouvVal,liste):
125 #--------------------------------------------------------------------------
126     if command  not in jdcSet : return
127     defaut=0
128     if liste[-1] == "defaut" : 
129        defaut=1
130     ChangementValeur(jdc,command,motcle,DictNouvVal,liste,defaut)
131
132 #--------------------------------------------------------------------------
133 def SuppressionValeurs(jdc, command,motcle,liste):
134 #--------------------------------------------------------------------------
135
136     if command not in jdcSet : return
137     boolChange=0
138     for c in jdc.root.childNodes:
139        if c.name != command  : continue
140        for mc in c.childNodes:
141           if mc.name != motcle : continue
142           indexLigneGlob=mc.lineno-1
143           while indexLigneGlob < mc.endline-1  :
144              MaLigneTexte = jdc.getLines()[indexLigneGlob]
145              MaLigne=MaLigneTexte
146              for Valeur in liste :
147                 debutMC =MaLigne.find(motcle)
148                 if debutMC ==-1 : debutMC=0
149                 debut1=MaLigne[0:debutMC]
150                 chercheLigne=MaLigne[debutMC:]
151                 trouve=chercheLigne.find(Valeur)
152                 premier=0
153                 if trouve > 1 : #on a au moins une quote
154                    debut=debut1 + chercheLigne[0:trouve-1]
155                    index = -1
156                    while (-1 * index) < len(debut) :
157                       if (debut[index] == "(")  :
158                          premier = 1
159                          if index == -1 :
160                             index=len(debut)
161                          else :
162                             index=index+1
163                          break
164                       if (debut[index] == "," ) : 
165                           break
166                       if (debut[index] != " " ) :
167                          assert(0)
168                       index = index -1
169                    debLigne = debut[0:index]
170                    fin=trouve+len(Valeur)+1
171                    if premier == 1 and chercheLigne[fin] == ',': fin = fin + 1 # on supprime la ,
172                    finLigne = chercheLigne[fin:]
173                    MaLigne_tmp=debLigne+finLigne
174                    # Traitement ligne commancant par ,
175                    if len(MaLigne_tmp.strip()) > 0 :
176                       if MaLigne_tmp.strip()[0]==',' :
177                          MaLigne=MaLigne_tmp.strip()[1:]
178                       else :
179                          MaLigne=MaLigne_tmp[0:]
180                    else :
181                       MaLigne=MaLigne_tmp[0:]
182                    boolChange=1
183                 jdc.getLines()[indexLigneGlob]=MaLigne
184              indexLigneGlob=indexLigneGlob+1
185     if boolChange : jdc.reset(jdc.getSource())
186
187 #----------------------------------------------
188 def AppelleMacroSelonValeurConcept(jdc,macro,genea):
189 #----------------------------------------------
190     if macro  not in jdcSet : return
191     boolChange=0
192     fact=genea[0]
193     motcle=genea[1]
194     chaine="CO"
195     for c in jdc.root.childNodes:
196        if c.name != macro  : continue
197        for mcF in c.childNodes:
198           if mcF.name != fact : continue
199           l=mcF.childNodes[:]
200           l.reverse()
201           for ll in l:
202              trouveValeur=0
203              for mc in ll.childNodes:
204                 if mc.name != motcle:continue
205                 TexteMC=mc.getText(jdc)
206                 liste_ligne_MC=TexteMC.splitlines()
207                 indexLigneGlob=mc.lineno-2
208                 trouveTexteMC=0
209                 trouveegal=0
210                 trouvechaine=0
211                 trouveparent=0
212                 trouvequote=0
213                 while indexLigneGlob < mc.endline  :
214                    indexLigneGlob=indexLigneGlob+1
215                    MaLigneTexte=jdc.getLines()[indexLigneGlob]
216
217                    # on commence par chercher TABLE par exemple
218                    # si on ne trouve pas on passe a la ligne suivante
219                    if ( trouveTexteMC == 0 ) :
220                        indice=MaLigneTexte.find(motcle)
221                        if indice < 0 : continue
222                        trouveTexteMC=1
223                    else :
224                       indice=0
225
226                    # on cherche =
227                    aChercher=MaLigneTexte[indice:]
228                    if (trouveegal == 0 ):
229                        indice=aChercher.find("=")
230                        if indice < 0 : continue
231                        trouveegal = 1
232                    else :
233                        indice = 0
234
235                    # on cherche CO
236                    aChercher2=aChercher[indice:]
237                    if (trouvechaine == 0 ):
238                        indice=aChercher2.find(chaine)
239                        if indice < 0 : continue
240                        trouvechaine = 1
241                    else :
242                        indice = 0
243
244                    #on cherche (
245                    aChercher3=aChercher2[indice:]
246                    if (trouveparent == 0 ):
247                        indice=aChercher3.find('(')
248                        if indice < 0 : continue
249                        trouveparent = 1
250                    else :
251                        indice = 0
252                  
253                    #on cherche la '
254                    aChercher4=aChercher3[indice:]
255                    if (trouvequote == 0 ):
256                        indice=aChercher4.find("'")
257                        indice2=aChercher4.find('"')
258                        if (indice < 0) and (indice2 < 0): continue
259                        if (indice < 0) : indice=indice2
260                        trouvequote = 1
261                    else :
262                        indice = 0
263
264                    trouveValeur=1
265                    aChercher5=aChercher4[indice+1:]
266                    indice=aChercher5.find("'")
267                    if indice < 0 :  indice=aChercher5.find('"')
268                    valeur=aChercher5[:indice]
269                    break
270                     
271              if trouveValeur==0 :
272                   logging.error("Pb de traduction pour MACR_LIGNE_COUPE : Pas de nom de Concept identifiable")
273                   return
274              
275              if boolChange :
276                   jdc.reset(jdc.getSource())
277                   logging.error("Pb du traduction pour MACR_LIGNE_COUPE : Deux noms de Concept possibles")
278                   return
279
280              boolChange=1
281              ligneaTraiter=jdc.getLines()[c.lineno-1]
282              debut=ligneaTraiter[0:c.colno]
283              suite=valeur+"="
284              fin=ligneaTraiter[c.colno:]
285              ligne=debut+suite+fin
286              jdc.getLines()[c.lineno-1]=ligne
287              nbBlanc=len(valeur)+1
288              if c.lineno < c.endline:
289                 decaleLignesdeNBlancs(jdc,c.lineno,c.endline-1,nbBlanc)
290     if boolChange : jdc.reset(jdc.getSource())
291
292 #----------------------------------------------
293 def ChangeTouteValeur(jdc,command,motcle,DictNouvVal,liste=(),defaut=0):
294 #----------------------------------------------
295     if macro  not in jdcSet : return
296     boolChange=0