Salome HOME
commentaire
[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 Traducteur.parseur import FactNode
22 from Traducteur.load import jdcSet 
23 from Traducteur.dictErreurs import EcritErreur
24 import string
25 from Traducteur 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,plusieursFois=True):
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     while ligneaCouper < facteur.endline + 1 :
89         trouve=0
90         trouveF=0
91         trouveP=0
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             if trouveF == 1 :
109                 indiceDeCoupe=indiceDeCoupe+indice
110     #            print "indice de Parcours" ,indiceParcours
111     #            print ancien[indiceParcours] 
112     #            print ancien[indiceParcours+1] 
113     #            print ancien[indiceParcours+2] 
114                 while  indiceParcours < longueur :
115                     if ancien[indiceParcours] == "(" :
116                         trouveP=1
117     #                    print "trouve"
118                         break
119                     if ancien[indiceParcours] != " " :
120                         trouveP=0
121     #                    print "mouv"
122                         break
123                     indiceParcours = indiceParcours+1
124             trouve = trouveP * trouveF
125             if trouve : break
126             ancien=ancien[indice+1:]
127         if trouve :
128             debut=indiceDeCoupe + 3
129             if(jdc.getLine(ligneaCouper)[debut:]!="\n"):
130                 jdc.splitLine(ligneaCouper,debut)
131             jdc.addLine(texte,ligneaCouper)
132             jdc.joinLineandNext(ligneaCouper)
133             logging.info("Insertion de %s ligne %d", texteinfo,ligneaCouper)
134
135             # Gestion du cas particulier du mot clef facteur vide
136             if facteur.childNodes == []:
137                 jdc.joinLineandNext(facteur.lineno)
138
139         ligneaCouper=ligneaCouper+1
140         ancien=jdc.getLine(ligneaCouper)
141         if not plusieursFois and trouve : break
142
143
144 #-----------------------------------
145 def chercheDebut1Mot(jdc,command):
146 #-----------------------------------
147 # Retourne le numero de colonne si le 1er mot clef est 
148 # sur la meme ligne que le mot clef facteur
149 # -1 sinon
150     assert (command.childNodes != [])
151     debut=-1
152     node1=command.childNodes[0]
153     if hasattr(node1,"lineno"):
154        if node1.lineno == command.lineno :
155           debut=node1.colno
156     else:
157        debut=chercheDebutFacteur(jdc,command) 
158     if debut == -1 and debug : print "attention!!! pb pour trouver le debut dans ", command
159     return debut
160
161 #-----------------------------------
162 def chercheDebutFacteur(jdc,facteur):
163 #-----------------------------------
164     debut=-1
165     ligne=jdc.getLines()[facteur.lineno]
166     debut=ligne.find("_F")
167     if debut >  -1 : debut=debut + 3
168     return debut
169     
170
171 #-----------------------------------
172 def chercheAlignement(jdc,command):
173 #-----------------------------------
174 # Retourne le nb de blanc
175 # pour aligner sur le 1er mot clef fils
176     assert (command.childNodes != []) 
177     node1=command.childNodes[0]
178     nbBlanc=node1.colno
179     return " "*nbBlanc
180
181 #---------------------------------------------------------------------------------------------------------
182 def chercheOperInsereFacteur(jdc,nomcommande,nouveau,ensemble=regles.SansRegle, estunFacteur=1, erreur=0):
183 #--------------------------------------------------------------------------------------------------------
184 # Cherche l oper
185 # cree le texte
186 # appelle insereMotCle pour ajouter le texte
187 #
188     boolChange=0
189     if estunFacteur : 
190       texte=nouveau+"=_F(),"
191     else :
192       texte=nouveau
193     if nomcommande  not in jdcSet : return
194     commands= jdc.root.childNodes[:]
195     commands.reverse()
196     for c in commands:
197         if c.name != nomcommande:continue
198         if ensemble.verif(c) == 0 : continue
199         if erreur : EcritErreur((nomcommande,nouveau),c.lineno)
200         boolChange=1
201         insereMotCle(jdc,c,texte)
202     if boolChange : jdc.reset(jdc.getSource())
203
204 #----------------------------------------------------------------------------------------
205 def chercheOperInsereFacteurSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
206 #----------------------------------------------------------------------------------------
207 # Cherche l oper
208 # cree le texte
209 # appelle insereMotCle pour ajouter le texte
210 #
211     if nomcommande  not in jdcSet : return
212     mesRegles=regles.ensembleRegles(liste_regles)
213     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
214
215 #----------------------------------------------------------------------------------------
216 def chercheOperInsereMotCleSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=0):
217 #----------------------------------------------------------------------------------------
218     if nomcommande  not in jdcSet : return
219     mesRegles=regles.ensembleRegles(liste_regles)
220     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
221
222     
223 #---------------------------------------------------------------------------------------------------------
224 def chercheOperInsereFacteurSiRegleAvecAvertissement(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
225 #---------------------------------------------------------------------------------------------------------
226     if nomcommande  not in jdcSet : return
227     mesRegles=regles.ensembleRegles(liste_regles)
228     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur,erreur=1)
229
230 #-------------------------------------------------------------------------------------------------
231 def AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,ensemble=regles.SansRegle, estunFacteur=0):
232 #-------------------------------------------------------------------------------------------------
233 # Cherche la commande
234 # Cherche le MCF
235 # cree le texte
236 # appelle insereMotCle pour ajouter le texte
237 #
238     if commande  not in jdcSet : return
239     if estunFacteur : 
240       texte=nouveau+"=_F(),"
241     else :
242       texte=nouveau
243     commands= jdc.root.childNodes[:]
244     commands.reverse()
245     boolChange=0
246     for c in commands:
247         if c.name != commande : continue
248         for mcF in c.childNodes:
249             if mcF.name != fact : continue
250             if ensemble.verif(c) == 0 : continue
251             boolChange=1
252             insereMotCleDansFacteur(jdc,mcF,texte)
253     if boolChange : jdc.reset(jdc.getSource())
254
255 #-------------------------------------------------------------------------------------------
256 def AjouteMotClefDansFacteurSiRegle(jdc,commande,fact,nouveau,liste_regles,estunFacteur=0):
257 #-------------------------------------------------------------------------------------------
258 #
259     if commande  not in jdcSet : return
260     mesRegles=regles.ensembleRegles(liste_regles)
261     AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,mesRegles,estunFacteur)
262
263 #-------------------------------------------------------------------------------------------
264 def AjouteMotClefDansFacteurCourantSiRegle(jdc,commande,fact,nouveau,liste_regles):
265 #-------------------------------------------------------------------------------------------
266 #
267     if commande  not in jdcSet : return
268     ensemble=regles.ensembleRegles(liste_regles)
269     commands= jdc.root.childNodes[:]
270     commands.reverse()
271     boolChange=0
272     for c in commands:
273         if c.name != commande : continue
274         for mcF in c.childNodes:
275           if mcF.name != fact : continue
276           l=mcF.childNodes[:]
277           l.reverse()
278           for ll in l:
279              if ensemble.verif(ll) == 0 : continue
280              boolChange=1
281              n=ll.childNodes[0]
282              ligneaCouper=n.lineno-1
283              numcol=n.colno
284              jdc.splitLine(ligneaCouper+1,numcol)
285              texte=nouveau+",\n"
286              jdc.addLine(texte,ligneaCouper+1)
287              logging.info("Insertion de %s dans %s : ligne %d", nouveau,c.name,ligneaCouper+1)
288              if numcol > 0 :    
289                  jdc.joinLineandNext(ligneaCouper+1)
290     if boolChange : jdc.reset(jdc.getSource())