Salome HOME
premiere version
[tools/eficas.git] / Traducteur / movemocle.py
1 # -*- coding: iso-8859-1 -*-
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
21 import logging
22 import removemocle
23 import inseremocle
24 from parseur import lastparen
25 from dictErreurs import jdcSet
26 debug=0
27
28 #-----------------------------------------------------
29 def moveMotCleFromFactToFather(jdc,command,fact,mocle):
30 #-----------------------------------------------------
31 # exemple type : IMPR_GENE
32
33     if command not in jdcSet : return
34     boolChange=0
35     commands= jdc.root.childNodes[:]
36     commands.reverse()
37     for c in commands:
38         if c.name != command:continue
39         boolchange_c=0
40         for mc in c.childNodes:
41             if mc.name != fact:continue
42             l=mc.childNodes[:]
43             for ll in l:
44                 for n in ll.childNodes:
45                     if n.name != mocle:continue
46                     # test boolchange_c :il faut le faire une seule fois par commande sinon duplication du mot clé
47                     if boolchange_c != 0 :continue
48                     if debug : print "Changement de place :", n.name, n.lineno, n.colno
49                     MonTexte=n.getText(jdc);
50                     boolChange=1
51                     boolchange_c=1
52                     inseremocle.insereMotCle(jdc,c,MonTexte)
53                     logging.info("Changement de place  %s ligne %s ",n.name, n.lineno)
54
55     if boolChange : jdc.reset(jdc.getSource())
56     removemocle.removeMotCleInFact(jdc,command,fact,mocle)
57
58
59 #----------------------------------------------------------------------------
60 def moveMotCleFromFactToFactMulti(jdc,oper,factsource,mocle,liste_factcible):
61 #----------------------------------------------------------------------------
62 # exemple type STAT_NON_LINE et RESI_INTER_RELA
63     for factcible in liste_factcible :
64        moveMotCleFromFactToFact(jdc,oper,factsource,mocle,factcible)
65     removemocle.removeMotCleInFact(jdc,oper,factsource,mocle)
66
67
68 #----------------------------------------------------------------------------
69 def moveMotCleFromFactToFact(jdc,oper,factsource,mocle,factcible):
70 #----------------------------------------------------------------------------
71     if oper not in jdcSet : return
72     if debug : print "moveMotCleFromFactToFact pour " ,oper,factsource,mocle,factcible
73     boolChange=0
74     commands= jdc.root.childNodes[:]
75     commands.reverse()
76     for c in commands:
77         if c.name != oper : continue
78         cible=None
79         for mc in c.childNodes:
80            if mc.name != factcible : 
81               continue
82            else :
83               cible=mc
84               break
85         if cible==None :
86            if debug : print "Pas de changement pour ", oper, " ", factsource, " ",mocle, "cible non trouvée"
87            continue
88
89         for mc in c.childNodes:
90            source=None
91            if mc.name != factsource:
92               continue
93            else :
94               source=mc
95               break
96         if source==None :
97            if debug : print "Pas de changement pour ", oper, " ", factsource, " ",mocle, "source non trouvée"
98            continue
99
100         if debug : print "Changement pour ", oper, " ", factsource, " ",mocle, "cible et source trouvées"
101         l=source.childNodes[:]
102         for ll in l:
103            for n in ll.childNodes:
104               if n.name != mocle:continue
105               MonTexte=n.getText(jdc);
106               inseremocle.insereMotCleDansFacteur(jdc,cible,MonTexte)
107               boolChange=1
108               logging.info("Changement de place   %s ligne %s vers %s",n.name, n.lineno, cible.name)
109     if boolChange : jdc.reset(jdc.getSource())
110
111
112
113
114 #------------------------------------------------------
115 def moveMotClefInOperToFact(jdc,oper,mocle,factcible):
116 #------------------------------------------------------
117 # Attention le cas type est THETA_OLD dans calc_G
118
119     if oper not in jdcSet : return
120     if debug : print "movemocleinoper pour " ,oper,mocle,factcible
121     boolChange=9
122     commands= jdc.root.childNodes[:]
123     commands.reverse()
124     for c in commands:
125         if c.name != oper : continue
126         cible=None
127         for mc in c.childNodes:
128            if mc.name != factcible : 
129               continue
130            else :
131               cible=mc
132               break
133         if cible==None :
134            if debug : print "Pas de changement pour ", oper, " ", factcible, " ", "cible non trouvée"
135            continue
136
137         source=None
138         for mc in c.childNodes:
139            if mc.name != mocle:
140               continue
141            else :
142               source=mc
143               break
144         if source==None :
145            if debug : print "Pas de changement pour ", oper, " ", mocle, " source non trouvée"
146            continue
147         MonTexte=source.getText(jdc);
148         boolChange=1
149         inseremocle.insereMotCleDansFacteur(jdc,cible,MonTexte)
150     if boolChange : jdc.reset(jdc.getSource())
151     removemocle.removeMotCle(jdc,oper,mocle)
152
153 #------------------------------------------------------
154 def copyMotClefInOperToFact(jdc,oper,mocle,factcible):
155 #------------------------------------------------------
156
157     if oper not in jdcSet : return
158     if debug : print "movemocleinoper pour " ,oper,mocle,factcible
159     boolChange=9
160     commands= jdc.root.childNodes[:]
161     commands.reverse()
162     for c in commands:
163         if c.name != oper : continue
164         cible=None
165         for mc in c.childNodes:
166            if mc.name != factcible : 
167               continue
168            else :
169               cible=mc
170               break
171         if cible==None :
172            if debug : print "Pas de changement pour ", oper, " ", factcible, " ", "cible non trouvée"
173            continue
174
175         source=None
176         for mc in c.childNodes:
177            if mc.name != mocle:
178               continue
179            else :
180               source=mc
181               break
182         if source==None :
183            if debug : print "Pas de changement pour ", oper, " ", mocle, " source non trouvée"
184            continue
185         MonTexte=source.getText(jdc);
186         boolChange=1
187         inseremocle.insereMotCleDansFacteur(jdc,cible,MonTexte)
188     if boolChange : jdc.reset(jdc.getSource())
189     
190 #----------------------------------------------------------------------
191 def moveMCFToCommand(jdc,command,factsource,commandcible,factcible):
192 #----------------------------------------------------------------------
193 # exemple CONTACT en 10
194 # CONTACT devient commande DEFI_CONTACT/ZONE 
195
196     if command not in jdcSet : return
197     boolChange=0
198     commands= jdc.root.childNodes[:]
199     commands.reverse()
200     for c in commands:
201        if c.name != command  : continue
202        for mcF in c.childNodes:
203            if mcF.name != factsource : continue
204            l=mcF.getText(jdc)
205            texte=l.replace(factsource,factcible)
206            texte='xxxx='+commandcible+'('+texte+')\n'
207            jdc.splitLine(c.lineno,0)
208            jdc.addLine(texte,c.lineno)
209            logging.info("Deplacement de %s dans %s ligne %s",factsource,commandcible,c.lineno)
210            boolChange=1
211     if boolChange :
212         jdc.reset(jdc.getSource())
213         jdcSet.add(commandcible)
214
215 #-----------------------------------------------------
216 def FusionMotCleToFact(jdc,command,liste_mc,factcible,defaut=0):
217 #-----------------------------------------------------
218     if command  not in jdcSet : return
219     boolChange=0
220     commands= jdc.root.childNodes[:]
221     commands.reverse()
222     for c in commands:
223        if c.name != command  : continue
224        list_val=[]
225        trouveUnMC=0
226        for mc in c.childNodes:
227            if mc.name not in liste_mc : continue
228            val=mc.getText(jdc).split("=")[1].split(",")[0]
229            list_val.append(val)
230            trouveUnMC=1
231        if trouveUnMC :
232            TexteMC=factcible+"=("
233            for val in list_val : TexteMC=TexteMC+val+","
234            TexteMC=TexteMC[:-1]+"),"
235            inseremocle.insereMotCle(jdc,c,TexteMC)
236            jdc.reset(jdc.getSource())
237            boolChange=1
238     if boolChange :
239         jdc.reset(jdc.getSource())
240         for mc in liste_mc : 
241            removemocle.removeMotCle(jdc,command,mc)
242            jdc.reset(jdc.getSource())
243
244 #--------------------------------------------------------------------
245 def EclaMotCleToFact(jdc,command,motcle,mot1,mot2,defaut=0):
246 #--------------------------------------------------------------------------
247 #  exemple STA10 pesanteur devient MCF avec eclatement des valeurs dans les MC
248 # On suppose que le MC est sur une seule ligne
249     if command  not in jdcSet : return
250     boolChange=0
251     for c in jdc.root.childNodes:
252        if c.name != command  : continue
253        trouveUnMC=0
254        for mc in c.childNodes:
255            if mc.name != motcle : continue
256            trouveUnMC=1
257            TexteMC=mc.getText(jdc)
258            indexLigneGlob=mc.lineno-1
259            MaLigneGlob=jdc.getLines()[indexLigneGlob]
260            Ligne=TexteMC.split('(')[1].split(')')[0].split(',')
261            motcle1=mot1+"="+Ligne[0]
262            motcle2=mot2+"=("+Ligne[1]+','+Ligne[2]+','+Ligne[3]+')'
263            texte=motcle+'=_F('+motcle1+','+motcle2+')'
264            num=lastparen(TexteMC)
265            Nouveau=MaLigneGlob.replace(TexteMC[0:num],texte)
266            jdc.getLines()[indexLigneGlob]=Nouveau
267            logging.info("Transformation de %s dans %s ligne %s",motcle,command,c.lineno)
268            boolChange=1
269     if boolChange : jdc.reset(jdc.getSource())