Salome HOME
reindent + copyright + merge manuel avec la V9_dev sauf repertoires metier
[tools/eficas.git] / Traducteur / movemocle.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2021   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 from Traducteur import removemocle
23 from Traducteur import inseremocle
24 from Traducteur.parseur import lastParen
25 from Traducteur.load 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     removemocle.removeMotCleInFact(jdc,oper,factsource,mocle)
111
112
113
114
115 #-----------------------------------------------------------------------
116 def moveMotClefInOperToFact(jdc,oper,mocle,factcible,plusieursFois=True):
117 #-----------------------------------------------------------------------
118 # Attention le cas type est THETA_OLD dans calc_G
119
120     if oper not in jdcSet : return
121     if debug : print "movemocleinoper pour " ,oper,mocle,factcible
122     boolChange=9
123     commands= jdc.root.childNodes[:]
124     commands.reverse()
125     for c in commands:
126         if c.name != oper : continue
127         cible=None
128         for mc in c.childNodes:
129             if mc.name != factcible :
130                 continue
131             else :
132                 cible=mc
133                 break
134         if cible==None :
135             if debug : print "Pas de changement pour ", oper, " ", factcible, " ", "cible non trouvée"
136             continue
137
138         source=None
139         for mc in c.childNodes:
140             if mc.name != mocle:
141                 continue
142             else :
143                 source=mc
144                 break
145         if source==None :
146             if debug : print "Pas de changement pour ", oper, " ", mocle, " source non trouvée"
147             continue
148         MonTexte=source.getText(jdc);
149         boolChange=1
150         inseremocle.insereMotCleDansFacteur(jdc,cible,MonTexte,plusieursFois)
151     if boolChange : jdc.reset(jdc.getSource())
152     removemocle.removeMotCle(jdc,oper,mocle)
153
154 #------------------------------------------------------
155 def copyMotClefInOperToFact(jdc,oper,mocle,factcible):
156 #------------------------------------------------------
157
158     if oper not in jdcSet : return
159     if debug : print "movemocleinoper pour " ,oper,mocle,factcible
160     boolChange=9
161     commands= jdc.root.childNodes[:]
162     commands.reverse()
163     for c in commands:
164         if c.name != oper : continue
165         cible=None
166         for mc in c.childNodes:
167             if mc.name != factcible :
168                 continue
169             else :
170                 cible=mc
171                 break
172         if cible==None :
173             if debug : print "Pas de changement pour ", oper, " ", factcible, " ", "cible non trouvée"
174             continue
175
176         source=None
177         for mc in c.childNodes:
178             if mc.name != mocle:
179                 continue
180             else :
181                 source=mc
182                 break
183         if source==None :
184             if debug : print "Pas de changement pour ", oper, " ", mocle, " source non trouvée"
185             continue
186         MonTexte=source.getText(jdc);
187         boolChange=1
188         inseremocle.insereMotCleDansFacteur(jdc,cible,MonTexte)
189     if boolChange : jdc.reset(jdc.getSource())
190
191 #----------------------------------------------------------------------
192 def moveMCFToCommand(jdc,command,factsource,commandcible,factcible):
193 #----------------------------------------------------------------------
194 # exemple CONTACT en 10
195 # CONTACT devient commande DEFI_CONTACT/ZONE
196 #
197     if command not in jdcSet : return
198     boolChange=0
199     commands= jdc.root.childNodes[:]
200     commands.reverse()
201     for c in commands:
202         if c.name != command  : continue
203         for mcF in c.childNodes:
204             if mcF.name != factsource : continue
205             l=mcF.getText(jdc)
206             texte=l.replace(factsource,factcible)
207             texte='xxxx='+commandcible+'('+texte+')\n'
208             jdc.splitLine(c.lineno,0)
209             jdc.addLine(texte,c.lineno)
210             logging.info("Deplacement de %s dans %s ligne %s",factsource,commandcible,c.lineno)
211             boolChange=1
212     if boolChange :
213         jdc.reset(jdc.getSource())
214         jdcSet.add(commandcible)
215
216 #-----------------------------------------------------
217 def fusionMotCleToFact(jdc,command,listeMc,factcible,defaut=0):
218 #-----------------------------------------------------
219     if command  not in jdcSet : return
220     boolChange=0
221     commands= jdc.root.childNodes[:]
222     commands.reverse()
223     for c in commands:
224         if c.name != command  : continue
225         list_val=[]
226         trouveUnMC=0
227         for mc in c.childNodes:
228             if mc.name not in listeMc : continue
229             val=mc.getText(jdc).split("=")[1].split(",")[0]
230             list_val.append(val)
231             trouveUnMC=1
232         if trouveUnMC :
233             TexteMC=factcible+"=("
234             for val in list_val : TexteMC=TexteMC+val+","
235             TexteMC=TexteMC[:-1]+"),"
236             inseremocle.insereMotCle(jdc,c,TexteMC)
237             jdc.reset(jdc.getSource())
238             boolChange=1
239     if boolChange :
240         jdc.reset(jdc.getSource())
241         for mc in listeMc :
242             removemocle.removeMotCle(jdc,command,mc)
243             jdc.reset(jdc.getSource())
244
245 #-----------------------------------------------------
246 def fusionMotCleInFact(jdc,command,fact,listeMc,new_name,defaut=0):
247 #-----------------------------------------------------
248     if command  not in jdcSet : return
249     boolChange=0
250     commands= jdc.root.childNodes[:]
251     commands.reverse()
252     for c in commands:
253         if c.name != command  : continue
254         list_val=[]
255         trouveUnMC=0
256         for mcF in c.childNodes:
257             if mcF.name != fact: continue
258             for ll in mcF.childNodes[:]:
259                 for mc in ll.childNodes:
260                     if mc.name not in listeMc : continue
261                     val=mc.getText(jdc).split("=")[1].split(",")[0]
262                     list_val.append(val)
263                     trouveUnMC=1
264                 if trouveUnMC :
265                     TexteMC=new_name+"=("+",".join(list_val)+"),"
266                     inseremocle.insereMotCleDansFacteur(jdc,mcF,TexteMC)
267                     jdc.reset(jdc.getSource())
268                     boolChange=1
269     if boolChange :
270         jdc.reset(jdc.getSource())
271         for mc in listeMc :
272             removemocle.removeMotCleInFact(jdc,command,fact,mc)
273             jdc.reset(jdc.getSource())
274
275 #-----------------------------------------------------
276 def fusionMCFToMCF(jdc,command,listeMcF,factcible,defaut=0):
277 #-----------------------------------------------------
278     if command  not in jdcSet : return
279     boolChange=0
280     commands= jdc.root.childNodes[:]
281     commands.reverse()
282     for c in commands:
283         if c.name != command  : continue
284         list_val=[]
285         trouveUnMC=0
286         TexteMC=factcible+'=('
287         esp1=' '*len(TexteMC)
288         pp=0
289         for mcF in c.childNodes:
290             if mcF.name not in listeMcF : continue
291             trouveUnMC=1
292             val=mcF.getText(jdc)
293             # esp=esp1+(inseremocle.chercheDebutFacteur(jdc,mcF)-len(mcF.name))*' '
294             esp=esp1+inseremocle.chercheAlignement(jdc,c)
295             # print len(esp)
296             for ll in mcF.childNodes[:]:
297                 # if(pp>0): TexteMC+=esp
298                 TexteMC+='_F('
299                 for mc in ll.childNodes:
300                     val=mc.getText(jdc)
301                     TexteMC+=val+'\n   '+esp
302                     # if('#' in val.split('\n')[-1]): TexteMC+='\n'+esp+'  '
303                 lastkey = ''.join(val.split('=')[-1].split(' '))
304                 if((len(lastkey.split('(')) - len(lastkey.split(')'))) >= 0):
305                     TexteMC += '),\n'+esp
306             # TexteMC+='),'
307         TexteMC+='),'
308         # print TexteMC
309         if(trouveUnMC):
310             inseremocle.insereMotCle(jdc,c,TexteMC)
311             jdc.reset(jdc.getSource())
312             boolChange=1
313     if boolChange :
314         jdc.reset(jdc.getSource())
315         for mcF in listeMcF :
316             removemocle.removeMotCle(jdc,command,mcF)
317             jdc.reset(jdc.getSource())
318
319
320
321 #--------------------------------------------------------------------
322 def eclaMotCleToFact(jdc,command,motcle,mot1,mot2,defaut=0):
323 #--------------------------------------------------------------------------
324 #  exemple STA10 pesanteur devient MCF avec eclatement des valeurs dans les MC
325 # On suppose que le MC est sur une seule ligne
326     if command  not in jdcSet : return
327     boolChange=0
328     for c in jdc.root.childNodes:
329         if c.name != command  : continue
330         trouveUnMC=0
331         for mc in c.childNodes:
332             if mc.name != motcle : continue
333             trouveUnMC=1
334             TexteMC=mc.getText(jdc)
335             indexLigneGlob=mc.lineno-1
336             MaLigneGlob=jdc.getLines()[indexLigneGlob]
337             Ligne=TexteMC.split('(')[1].split(')')[0].split(',')
338             motcle1=mot1+"="+Ligne[0]
339             motcle2=mot2+"=("+Ligne[1]+','+Ligne[2]+','+Ligne[3]+')'
340             texte=motcle+'=_F('+motcle1+','+motcle2+')'
341             num=lastParen(TexteMC)
342             Nouveau=MaLigneGlob.replace(TexteMC[0:num],texte)
343             jdc.getLines()[indexLigneGlob]=Nouveau
344             logging.info("Transformation de %s dans %s ligne %s",motcle,command,c.lineno)
345             boolChange=1
346     if boolChange : jdc.reset(jdc.getSource())