Salome HOME
chgt date copyright
[tools/eficas.git] / Traducteur / changeValeur.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017   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 from Traducteur.utils import lineToDict
21 import logging
22 from Traducteur.dictErreurs import EcritErreur
23 from Traducteur.load import jdcSet 
24 from Traducteur.renamemocle import decaleLignesdeNBlancs
25 from Traducteur.removemocle import removeMotCleInFact
26 from Traducteur import regles
27
28
29 #--------------------------------------------------------------------------
30 def ChangementValeur(jdc,command,motcle,DictNouvVal,liste=(),defaut=0):
31 #--------------------------------------------------------------------------
32     if command  not in jdcSet : return
33     boolChange=0
34     for c in jdc.root.childNodes:
35        if c.name != command  : continue
36        trouveUnMC=0
37        for mc in c.childNodes:
38           if mc.name != motcle : continue
39           trouveUnMC=1
40           TexteMC=mc.getText(jdc)
41           liste_ligne_MC=TexteMC.splitlines()
42           indexLigneGlob=mc.lineno-1
43           indexTexteMC=0
44           while indexLigneGlob < mc.endline  :
45              if indexTexteMC > len(liste_ligne_MC)-1 : break
46              MaLigneGlob=jdc.getLines()[indexLigneGlob]
47              MaLigneTexte=liste_ligne_MC[indexTexteMC]
48              for Valeur in DictNouvVal :
49                 MaLigneTexteDict=lineToDict(MaLigneTexte)
50                 trouvecol=MaLigneTexte.find(Valeur)
51                 if trouvecol > -1:
52                     trouve=(Valeur==MaLigneTexteDict[trouvecol])
53                 else:
54                     trouve=False
55                 if trouve:
56                    debut=MaLigneGlob.find(motcle)
57                    if debut==-1 : debut=0
58                    Nouveau=MaLigneGlob[debut:].replace(Valeur,DictNouvVal[Valeur])
59                    Nouveau=MaLigneGlob[0:debut]+Nouveau
60                    jdc.getLines()[indexLigneGlob]=Nouveau
61                    MaLigneTexte=Nouveau # raccourci honteux mais ...
62                    MaLigneGlob=Nouveau
63                    if Valeur in liste :
64                       EcritErreur((command,motcle,Valeur),indexLigneGlob)
65                    else :
66                       logging.info("Changement de %s par %s dans %s ligne %d",Valeur,DictNouvVal[Valeur],command,indexLigneGlob)
67                    boolChange=1
68              indexLigneGlob=indexLigneGlob+1
69              indexTexteMC=indexTexteMC+1
70        if (trouveUnMC == 0) and ( defaut == 1):
71           EcritErreur((command,motcle,"DEFAUT"),c.lineno)
72     if boolChange : jdc.reset(jdc.getSource())
73              
74 #--------------------------------------------------------------------------------
75 def ChangementValeurDsMCF(jdc,command,fact,motcle,DictNouvVal,liste=(),ensemble=regles.SansRegle,defaut=0):
76 #--------------------------------------------------------------------------------
77
78     if command  not in jdcSet : return
79     boolChange=0
80     for c in jdc.root.childNodes:
81         if c.name != command  : continue
82         for mcF in c.childNodes:
83             if mcF.name != fact : continue
84             l=mcF.childNodes[:]
85             l.reverse()
86             for ll in l:
87                 trouveUnMC=0
88                 for mc in ll.childNodes:
89                     if mc.name != motcle:continue                    
90                     if ensemble.verif(c) == 0 : continue
91                     trouveUnMC=1
92                     TexteMC=mc.getText(jdc)
93                     liste_ligne_MC=TexteMC.splitlines()
94                     indexLigneGlob=mc.lineno-1
95                     indexTexteMC=0
96                     while indexLigneGlob < mc.endline  :
97                         if indexTexteMC > len(liste_ligne_MC)-1 : break
98                         MaLigneGlob=jdc.getLines()[indexLigneGlob]
99                         MaLigneTexte=liste_ligne_MC[indexTexteMC]
100                         for Valeur in DictNouvVal :
101                             MaLigneTexteDict=lineToDict(MaLigneTexte)
102                             trouvecol=MaLigneTexte.find(Valeur)
103                             if trouvecol > -1:
104                                 trouve=(Valeur==MaLigneTexteDict[trouvecol])
105                             else:
106                                 trouve=False
107                             if trouve:
108                                 debut=MaLigneGlob.find(motcle)
109                                 if debut==-1 : debut=0
110                                 Nouveau=MaLigneGlob[debut:].replace(Valeur,DictNouvVal[Valeur])
111                                 Nouveau=MaLigneGlob[0:debut]+Nouveau
112                                 jdc.getLines()[indexLigneGlob]=Nouveau
113                                 MaLigneTexte=Nouveau # raccourci honteux mais ...
114                                 MaLigneGlob=Nouveau
115                                 if Valeur in liste :
116                                    EcritErreur((command,fact,motcle,Valeur),indexLigneGlob)
117                                 else :
118                                    logging.info("Changement de %s par %s dans %s ligne %d",Valeur,DictNouvVal[Valeur],command,indexLigneGlob)
119                         boolChange=1
120                         indexLigneGlob=indexLigneGlob+1
121                         indexTexteMC=indexTexteMC+1
122                 if (trouveUnMC == 0) and ( defaut == 1):
123                    logging.warning("OPTION  (defaut) de CALCG à verifier ligne %s" ,c.lineno )                     
124                    EcritErreur((command,fact,motcle,"DEFAUT"),c.lineno)
125     if boolChange : jdc.reset(jdc.getSource())
126              
127 #--------------------------------------------------------------------------------
128 def ChangementValeurDsMCFSiRegle(jdc,command,fact,motcle,DictNouvVal,liste_regles,defaut=0):
129 #--------------------------------------------------------------------------------
130     if command not in jdcSet : return
131     mesRegles=regles.ensembleRegles(liste_regles)
132     liste=()
133     ChangementValeurDsMCF(jdc,command,fact,motcle,DictNouvVal,liste,mesRegles,defaut)
134              
135 #---------------------------------------------------------------------------------------
136 def ChangementValeurDsMCFAvecAvertissement(jdc, command, fact,motcle,DictNouvVal,liste):
137 #---------------------------------------------------------------------------------------
138     if command  not in jdcSet : return
139     defaut=0
140     if liste[-1] == "defaut" : 
141        defaut=1
142     ChangementValeurDsMCF(jdc,command,fact,motcle,DictNouvVal,liste,defaut)
143
144 #--------------------------------------------------------------------------
145 def ChangementValeurAvecAvertissement(jdc, command,motcle,DictNouvVal,liste):
146 #--------------------------------------------------------------------------
147     if command  not in jdcSet : return
148     defaut=0
149     if liste[-1] == "defaut" : 
150        defaut=1
151     ChangementValeur(jdc,command,motcle,DictNouvVal,liste,defaut)
152
153 #--------------------------------------------------------------------------
154 def SuppressionValeurs(jdc, command,motcle,liste):
155 #--------------------------------------------------------------------------
156
157     if command not in jdcSet : return
158     boolChange=0
159     for c in jdc.root.childNodes:
160        if c.name != command  : continue
161        for mc in c.childNodes:
162           if mc.name != motcle : continue
163           indexLigneGlob=mc.lineno-1
164           while indexLigneGlob < mc.endline-1  :
165              MaLigneTexte = jdc.getLines()[indexLigneGlob]
166              MaLigne=MaLigneTexte
167              for Valeur in liste :
168                 debutMC =MaLigne.find(motcle)
169                 if debutMC ==-1 : debutMC=0
170                 debut1=MaLigne[0:debutMC]
171                 chercheLigne=MaLigne[debutMC:]
172                 trouve=chercheLigne.find(Valeur)
173                 premier=0
174                 if trouve > 1 : #on a au moins une quote
175                    debut=debut1 + chercheLigne[0:trouve-1]
176                    index = -1
177                    while (-1 * index) < len(debut) :
178                       if (debut[index] == "(")  :
179                          premier = 1
180                          if index == -1 :
181                             index=len(debut)
182                          else :
183                             index=index+1
184                          break
185                       if (debut[index] == "," ) : 
186                           break
187                       if (debut[index] != " " ) :
188                          assert(0)
189                       index = index -1
190                    debLigne = debut[0:index]
191                    fin=trouve+len(Valeur)+1
192                    if premier == 1 and chercheLigne[fin] == ',': fin = fin + 1 # on supprime la ,
193                    finLigne = chercheLigne[fin:]
194                    MaLigne_tmp=debLigne+finLigne
195                    # Traitement ligne commancant par ,
196                    if len(MaLigne_tmp.strip()) > 0 :
197                       if MaLigne_tmp.strip()[0]==',' :
198                          MaLigne=MaLigne_tmp.strip()[1:]
199                       else :
200                          MaLigne=MaLigne_tmp[0:]
201                    else :
202                       MaLigne=MaLigne_tmp[0:]
203                    boolChange=1
204                 jdc.getLines()[indexLigneGlob]=MaLigne
205              indexLigneGlob=indexLigneGlob+1
206     if boolChange : jdc.reset(jdc.getSource())
207
208 #----------------------------------------------
209 def AppelleMacroSelonValeurConcept(jdc,macro,genea):
210 #----------------------------------------------
211     if macro  not in jdcSet : return
212     boolChange=0
213     fact=genea[0]
214     motcle=genea[1]
215     chaine="CO"
216     for c in jdc.root.childNodes:
217        if c.name != macro  : continue
218        for mcF in c.childNodes:
219           if mcF.name != fact : continue
220           l=mcF.childNodes[:]
221           l.reverse()
222           for ll in l:
223              trouveValeur=0
224              for mc in ll.childNodes:
225                 if mc.name != motcle:continue
226                 TexteMC=mc.getText(jdc)
227                 liste_ligne_MC=TexteMC.splitlines()
228                 indexLigneGlob=mc.lineno-2
229                 trouveTexteMC=0
230                 trouveegal=0
231                 trouvechaine=0
232                 trouveparent=0
233                 trouvequote=0
234                 while indexLigneGlob < mc.endline  :
235                    indexLigneGlob=indexLigneGlob+1
236                    MaLigneTexte=jdc.getLines()[indexLigneGlob]
237
238                    # on commence par chercher TABLE par exemple
239                    # si on ne trouve pas on passe a la ligne suivante
240                    if ( trouveTexteMC == 0 ) :
241                        indice=MaLigneTexte.find(motcle)
242                        if indice < 0 : continue
243                        trouveTexteMC=1
244                    else :
245                       indice=0
246
247                    # on cherche =
248                    aChercher=MaLigneTexte[indice:]
249                    if (trouveegal == 0 ):
250                        indice=aChercher.find("=")
251                        if indice < 0 : continue
252                        trouveegal = 1
253                    else :
254                        indice = 0
255
256                    # on cherche CO
257                    aChercher2=aChercher[indice:]
258                    if (trouvechaine == 0 ):
259                        indice=aChercher2.find(chaine)
260                        if indice < 0 : continue
261                        trouvechaine = 1
262                    else :
263                        indice = 0
264
265                    #on cherche (
266                    aChercher3=aChercher2[indice:]
267                    if (trouveparent == 0 ):
268                        indice=aChercher3.find('(')
269                        if indice < 0 : continue
270                        trouveparent = 1
271                    else :
272                        indice = 0
273                  
274                    #on cherche la '
275                    aChercher4=aChercher3[indice:]
276                    if (trouvequote == 0 ):
277                        indice=aChercher4.find("'")
278                        indice2=aChercher4.find('"')
279                        if (indice < 0) and (indice2 < 0): continue
280                        if (indice < 0) : indice=indice2
281                        trouvequote = 1
282                    else :
283                        indice = 0
284
285                    trouveValeur=1
286                    aChercher5=aChercher4[indice+1:]
287                    indice=aChercher5.find("'")
288                    if indice < 0 :  indice=aChercher5.find('"')
289                    valeur=aChercher5[:indice]
290                    break
291                     
292              if trouveValeur==0 :
293                   logging.error("Pb de traduction pour MACR_LIGNE_COUPE : Pas de nom de Concept identifiable")
294                   return
295              
296              if boolChange :
297                   jdc.reset(jdc.getSource())
298                   logging.error("Pb du traduction pour MACR_LIGNE_COUPE : Deux noms de Concept possibles")
299                   return
300
301              boolChange=1
302              ligneaTraiter=jdc.getLines()[c.lineno-1]
303              debut=ligneaTraiter[0:c.colno]
304              suite=valeur+"="
305              fin=ligneaTraiter[c.colno:]
306              ligne=debut+suite+fin
307              jdc.getLines()[c.lineno-1]=ligne
308              nbBlanc=len(valeur)+1
309              if c.lineno < c.endline:
310                 decaleLignesdeNBlancs(jdc,c.lineno,c.endline-1,nbBlanc)
311     if boolChange : jdc.reset(jdc.getSource())
312
313 #----------------------------------------------
314 def ChangeTouteValeur(jdc,command,motcle,DictNouvVal,liste=(),defaut=0):
315 #----------------------------------------------
316     if macro  not in jdcSet : return
317     boolChange=0