Salome HOME
Version cmt-01
[tools/eficas.git] / Traducteur / inseremocle.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   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 parseur import FactNode
22 from dictErreurs import jdcSet
23 from dictErreurs import EcritErreur
24 import string
25 import regles
26 debug=0
27
28
29 #-----------------------------------
30 def insereMotCle(jdc,recepteur,texte):
31 #-----------------------------------
32 # appelle la methode selon la classe 
33 # du recepteur
34
35     if recepteur.name  not in jdcSet : return
36     if recepteur.__class__.__name__ == "Command" :
37        if debug : print " Ajout de ", texte, "dans la commande : " ,recepteur.name 
38        insereMotCleDansCommande(jdc,recepteur,texte)
39        return
40
41
42 #--------------------------------------------
43 def insereMotCleDansCommande(jdc,command,texte):
44 #---------------------------------------------
45 # insere le texte comme 1er mot cle
46 # de la commande
47     if command.name  not in jdcSet : return
48     if debug : print "insereMotCle ", texte , " dans ", command.name
49     numcol=chercheDebut1Mot(jdc,command)
50     if numcol > 0 :
51        jdc.splitLine(command.lineno,numcol)
52     indice = -1
53     while texte[indice] == " " or texte[indice] == "\n": 
54        indice = indice -1
55     if texte[indice] != "," : texte=texte+","
56     texteinfo=texte
57     texte=texte+'\n'
58     jdc.addLine(texte,command.lineno) 
59     logging.info("Insertion de : %s ligne %d", texteinfo,command.lineno)
60     if numcol > 0 :             # Les mots clefs etaient sur la même ligne
61         jdc.joinLineandNext(command.lineno)
62
63 #---------------------------------------------
64 def insereMotCleDansFacteur(jdc,facteur,texte):
65 #-------------------------------------------------
66     if debug : print "insereMotCle ", texte , " dans ", facteur.name
67
68     if texte[-1] == "\n" : texte=texte[0:-1] 
69     ancien=jdc.getLine(facteur.lineno)
70
71     # On va chercher la derniere ) pour ajouter avant
72     # on va verifier s il il y a un , avant
73     # si le texte ne finit pas par une ","
74     # on en met une
75
76     indice = -1
77     while texte[indice] == " " : 
78        indice = indice -1
79     if texte[indice] != "," : 
80        texte=texte+","
81     if (texte.find("#") > -1) and (texte.find("#") < texte.find(",")) :
82           texte=texte+"\n,"
83           
84     texteinfo=texte
85     texte=texte+"\n"
86   
87     ligneaCouper=facteur.lineno
88     trouve=0
89     trouveF=0
90     trouveP=0
91     while ligneaCouper < facteur.endline + 1 :
92        indiceDeCoupe=0
93        while  ancien.find("_F") > 0 :
94           longueur=len(ancien)
95           indice=ancien.find("_F")
96           indiceParcours=0
97           # pour ne pas tenir compte des autres noms 
98           # Attention si 2 MCF sur la meme ligne (la 1ere)
99           if trouveF == 0 :
100             if ((ligneaCouper!=facteur.lineno) or ((ancien.find(facteur.name) < indice ) or (ancien.find(facteur.name) < 0))) :
101                trouveF=1
102                indiceParcours=indice + 2
103           # attention pour regler DEFI_FONCTION .. 
104             else :
105                indiceDeCoupe=indiceDeCoupe+indice+2
106                ancien=ancien[indice +2:]
107                continue
108
109           if trouveF == 1 :
110              indiceDeCoupe=indiceDeCoupe+indice
111     #         print "indice de Parcours" ,indiceParcours
112     #         print ancien[indiceParcours] 
113     #         print ancien[indiceParcours+1] 
114     #         print ancien[indiceParcours+2] 
115              while  indiceParcours < longueur :
116                if ancien[indiceParcours] == "(" :
117                 trouveP=1
118     #            print "trouve"
119                 break
120                if ancien[indiceParcours] != " " :
121                 trouveP=0
122     #            print "mouv"
123                 break
124                indiceParcours = indiceParcours+1
125           trouve = trouveP * trouveF
126           if trouve : break
127           ancien=ancien[indice+1:]
128           
129        trouve = trouveP * trouveF
130        if trouve : break
131        ligneaCouper=ligneaCouper+1
132        ancien=jdc.getLine(ligneaCouper)
133          
134     if trouve :
135        debut=indiceDeCoupe + 3
136        jdc.splitLine(ligneaCouper,debut)
137     else :
138        print "Le traducteur ne sait pas faire"
139        assert 0
140
141     # enleve les blancs en debut de texte
142     i = 0
143     while i < len(texte) :
144       if texte[i] != " " : break
145       i = i +1
146
147     jdc.addLine(texte,ligneaCouper)
148     jdc.joinLineandNext(ligneaCouper)
149     logging.info("Insertion de %s ligne %d", texteinfo,ligneaCouper)
150     # Gestion du cas particulier du mot clef facteur vide
151     if facteur.childNodes == []:
152        jdc.joinLineandNext(facteur.lineno)
153
154
155 #-----------------------------------
156 def chercheDebut1Mot(jdc,command):
157 #-----------------------------------
158 # Retourne le numero de colonne si le 1er mot clef est 
159 # sur la meme ligne que le mot clef facteur
160 # -1 sinon
161     assert (command.childNodes != [])
162     debut=-1
163     node1=command.childNodes[0]
164     if hasattr(node1,"lineno"):
165        if node1.lineno == command.lineno :
166           debut=node1.colno
167     else:
168        debut=chercheDebutFacteur(jdc,command) 
169     if debut == -1 and debug : print "attention!!! pb pour trouver le debut dans ", command
170     return debut
171
172 #-----------------------------------
173 def chercheDebutFacteur(jdc,facteur):
174 #-----------------------------------
175     debut=-1
176     ligne=jdc.getLines()[facteur.lineno]
177     debut=ligne.find("_F")
178     if debut >  -1 : debut=debut + 3
179     return debut
180     
181
182 #-----------------------------------
183 def chercheAlignement(jdc,command):
184 #-----------------------------------
185 # Retourne le nb de blanc
186 # pour aligner sur le 1er mot clef fils
187     assert (command.childNodes != []) 
188     node1=command.childNodes[0]
189     nbBlanc=node1.colno
190     return " "*nbBlanc
191
192 #---------------------------------------------------------------------------------------------------------
193 def chercheOperInsereFacteur(jdc,nomcommande,nouveau,ensemble=regles.SansRegle, estunFacteur=1, erreur=0):
194 #--------------------------------------------------------------------------------------------------------
195 # Cherche l oper
196 # cree le texte
197 # appelle insereMotCle pour ajouter le texte
198 #
199     boolChange=0
200     if estunFacteur : 
201       texte=nouveau+"=_F(),"
202     else :
203       texte=nouveau
204     if nomcommande  not in jdcSet : return
205     commands= jdc.root.childNodes[:]
206     commands.reverse()
207     for c in commands:
208         if c.name != nomcommande:continue
209         if ensemble.verif(c) == 0 : continue
210         if erreur : EcritErreur((nomcommande,nouveau),c.lineno)
211         boolChange=1
212         insereMotCle(jdc,c,texte)
213     if boolChange : jdc.reset(jdc.getSource())
214
215 #----------------------------------------------------------------------------------------
216 def chercheOperInsereFacteurSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
217 #----------------------------------------------------------------------------------------
218 # Cherche l oper
219 # cree le texte
220 # appelle insereMotCle pour ajouter le texte
221 #
222     if nomcommande  not in jdcSet : return
223     mesRegles=regles.ensembleRegles(liste_regles)
224     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
225
226 #----------------------------------------------------------------------------------------
227 def chercheOperInsereMotCleSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=0):
228 #----------------------------------------------------------------------------------------
229     if nomcommande  not in jdcSet : return
230     mesRegles=regles.ensembleRegles(liste_regles)
231     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
232
233     
234 #---------------------------------------------------------------------------------------------------------
235 def chercheOperInsereFacteurSiRegleAvecAvertissement(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
236 #---------------------------------------------------------------------------------------------------------
237     if nomcommande  not in jdcSet : return
238     mesRegles=regles.ensembleRegles(liste_regles)
239     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur,erreur=1)
240
241 #-------------------------------------------------------------------------------------------------
242 def AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,ensemble=regles.SansRegle, estunFacteur=0):
243 #-------------------------------------------------------------------------------------------------
244 # Cherche la commande
245 # Cherche le MCF
246 # cree le texte
247 # appelle insereMotCle pour ajouter le texte
248 #
249     if commande  not in jdcSet : return
250     if estunFacteur : 
251       texte=nouveau+"=_F(),"
252     else :
253       texte=nouveau
254     commands= jdc.root.childNodes[:]
255     commands.reverse()
256     boolChange=0
257     for c in commands:
258         if c.name != commande : continue
259         for mcF in c.childNodes:
260           if mcF.name != fact : continue
261           if ensemble.verif(c) == 0 : continue
262           l=mcF.childNodes[:]
263           l.reverse()
264           boolChange=1
265           insereMotCleDansFacteur(jdc,mcF,texte)
266     if boolChange : jdc.reset(jdc.getSource())
267
268 #-------------------------------------------------------------------------------------------
269 def AjouteMotClefDansFacteurSiRegle(jdc,commande,fact,nouveau,liste_regles,estunFacteur=0):
270 #-------------------------------------------------------------------------------------------
271 #
272     if commande  not in jdcSet : return
273     mesRegles=regles.ensembleRegles(liste_regles)
274     AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,mesRegles,estunFacteur)
275
276 #-------------------------------------------------------------------------------------------
277 def AjouteMotClefDansFacteurCourantSiRegle(jdc,commande,fact,nouveau,liste_regles):
278 #-------------------------------------------------------------------------------------------
279 #
280     if commande  not in jdcSet : return
281     ensemble=regles.ensembleRegles(liste_regles)
282     commands= jdc.root.childNodes[:]
283     commands.reverse()
284     boolChange=0
285     for c in commands:
286         if c.name != commande : continue
287         for mcF in c.childNodes:
288           if mcF.name != fact : continue
289           l=mcF.childNodes[:]
290           l.reverse()
291           for ll in l:
292              if ensemble.verif(ll) == 0 : continue
293              boolChange=1
294              n=ll.childNodes[0]
295              ligneaCouper=n.lineno-1
296              numcol=n.colno
297              jdc.splitLine(ligneaCouper+1,numcol)
298              texte=nouveau+",\n"
299              jdc.addLine(texte,ligneaCouper+1)
300              logging.info("Insertion de %s dans %s : ligne %d", nouveau,c.name,ligneaCouper+1)
301              if numcol > 0 :    
302                  jdc.joinLineandNext(ligneaCouper+1)
303     if boolChange : jdc.reset(jdc.getSource())