Salome HOME
Update version
[tools/eficas.git] / Traducteur / renamemocle.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 import logging
21 import sys
22 from Traducteur.parseur import FactNode
23 from Traducteur.load import jdcSet
24 from Traducteur import regles
25 from Traducteur.dictErreurs import ecritErreur
26 #debug=1
27 debug=0
28
29 #on n'a qu'un mocle par commande.
30 #en fin de traitement, on remet a jour l'arbre syntaxique (lineno,colno,etc.)
31
32 #--------------------------------------------------------------------------------
33 def renameMotCle(jdc,command,mocle,new_name, erreur=0,ensemble=regles.SansRegle):
34 #--------------------------------------------------------------------------------
35     if command not in jdcSet : return
36     boolChange=0
37     for c in jdc.root.childNodes:
38         if c.name != command:continue
39         for mc in c.childNodes:
40             if mc.name != mocle:continue
41             if ensemble.verif(c) == 0 : continue
42             boolChange=1
43             if debug: print ("Renommage de:",c.name,mc.name,mc.lineno,mc.colno)
44             if erreur :
45                 ecritErreur((command,mocle),c.lineno)
46             else :
47                 logging.info("Renommage de: %s  %s ligne %d en %s",c.name,mc.name,mc.lineno,new_name)
48             s=jdc.getLines()[mc.lineno-1]
49             jdc.getLines()[mc.lineno-1]=s[:mc.colno]+new_name+s[mc.colno+len(mocle):]
50             diff=len(new_name) - len(mocle)
51             decaleLignesdeNBlancs(jdc,mc.lineno,mc.endline-1,diff)
52
53     if boolChange : jdc.reset(jdc.getSource())
54
55 #------------------------------------------------------
56 def renameMotCleAvecErreur(jdc,command,mocle,new_name):
57 #------------------------------------------------------
58     if command not in jdcSet : return
59     renameMotCle(jdc,command,mocle,new_name,1,regles.SansRegle)
60
61 #--------------------------------------------------------------------------
62 def renameMotCleSiRegle(jdc,command,mocle,new_name,liste_regles, erreur=0):
63 #--------------------------------------------------------------------------
64     if command not in jdcSet : return
65     mesRegles=regles.ensembleRegles(liste_regles)
66     renameMotCle(jdc,command,mocle,new_name, erreur,mesRegles)
67
68 #-------------------------------------------
69 def renameOper(jdc,command,new_name):
70 #-------------------------------------------
71     if command not in jdcSet : return
72     jdcSet.add(new_name)
73     boolChange=0
74     for c in jdc.root.childNodes:
75         if c.name != command:continue
76         if debug: print ("Renommage de:",c.name,c.lineno,c.colno)
77         logging.info("Renommage de: %s ligne %d en %s",c.name,c.lineno,new_name)
78         boolChange=1
79         s=jdc.getLines()[c.lineno-1]
80         jdc.getLines()[c.lineno-1]=s[:c.colno]+new_name+s[c.colno+len(command):]
81         diff=len(new_name) - len(command)
82         decaleLignesdeNBlancs(jdc,c.lineno,c.endline,diff)
83     if boolChange : jdc.reset(jdc.getSource())
84
85 #----------------------------------------------------------
86 def decaleLignesdeNBlancs(jdc,premiere,derniere,nbBlanc):
87 #----------------------------------------------------------
88     ligne = premiere + 1
89     while ligne < derniere :
90         s=jdc.getLines()[ligne]
91         if nbBlanc > 0 :
92             jdc.getLines()[ligne] = nbBlanc*" " + s
93         else :
94             toutblancs=-1*nbBlanc*" "
95             if jdc.getLines()[ligne][0:-1*nbBlanc] == toutblancs:
96                 jdc.getLines()[ligne] = s[-1*nbBlanc:]
97         ligne=ligne+1
98
99 #---------------------------------------------------------------------------------------------
100 def renameMotCleInFact(jdc,command,fact,mocle,new_name, ensemble=regles.SansRegle, erreur=0):
101 #---------------------------------------------------------------------------------------------
102     if command not in jdcSet : return
103     boolChange=0
104     for c in jdc.root.childNodes:
105         if c.name != command:continue
106         for mc in c.childNodes:
107             if mc.name != fact:continue
108             l=mc.childNodes[:]
109             #on itere a l'envers
110             l.reverse()
111             for ll in l:
112                 for n in ll.childNodes:
113                     if n.name != mocle:continue
114                     if ensemble.verif(c) == 0 : continue
115                     s=jdc.getLines()[n.lineno-1]
116                     jdc.getLines()[n.lineno-1]=s[:n.colno]+new_name+s[n.colno+len(mocle):]
117                     boolChange=1
118                     if erreur :
119                         ecritErreur((command,fact,mocle),c.lineno)
120                     else :
121                         logging.info("Renommage de: %s, ligne %s, en %s",n.name,n.lineno,new_name)
122
123     if boolChange : jdc.reset(jdc.getSource())
124
125 #--------------------------------------------------------------------------
126 def renameMotCleInFactSiRegle(jdc,command,fact,mocle,new_name,liste_regles):
127 #--------------------------------------------------------------------------
128     if command not in jdcSet : return
129     mesRegles=regles.ensembleRegles(liste_regles)
130     renameMotCleInFact(jdc,command,fact,mocle,new_name,mesRegles)
131
132 def renameMotCleInFactCourantSiRegle(jdc,command,fact,mocle,new_name,liste_regles,erreur=0):
133 #--------------------------------------------------------------------------
134     if command not in jdcSet : return
135     ensemble=regles.ensembleRegles(liste_regles)
136     boolChange=0
137     for c in jdc.root.childNodes:
138         if c.name != command:continue
139         for mc in c.childNodes:
140             if mc.name != fact:continue
141             l=mc.childNodes[:]
142             #on itere a l'envers
143             l.reverse()
144             for ll in l:
145                 if ensemble.verif(ll) == 0 : continue
146                 for n in ll.childNodes:
147                     if n.name != mocle:continue
148                     s=jdc.getLines()[n.lineno-1]
149                     jdc.getLines()[n.lineno-1]=s[:n.colno]+new_name+s[n.colno+len(mocle):]
150                     boolChange=1
151                     if erreur :
152                         ecritErreur((command,fact,mocle),c.lineno)
153                     else :
154                         logging.info("Renommage de: %s, ligne %s, en %s",n.name,n.lineno,new_name)
155
156     if boolChange : jdc.reset(jdc.getSource())
157
158
159 #-----------------------------------------------------------------
160 def renameCommande(jdc,command,new_name,ensemble=regles.SansRegle):
161 #-----------------------------------------------------------------
162 # nom de la commande "ancien format" , nom de la commande " nouveau format "
163     if command not in jdcSet : return
164     jdcSet.add(new_name)
165     boolChange=0
166     if debug :
167         if ensemble != regles.SansRegle :
168             logging.info("traitement de %s renomme en %s sous conditions", command, new_name)
169         else  :
170             logging.info("traitement de %s renomme en %s ", command, new_name)
171     for c in jdc.root.childNodes:
172         if c.name != command:continue
173         if ensemble.verif(c) == 0 : continue
174         boolChange=1
175         if debug: print ("Renommage de:",c.name,new_name ,c.lineno,c.colno)
176         logging.info("Renommage de: %s ligne %d en %s",c.name,c.lineno,new_name)
177         s=jdc.getLines()[c.lineno-1]
178         jdc.getLines()[c.lineno-1]=s[:c.colno]+new_name+s[c.colno+len(command):]
179
180     if boolChange : jdc.reset(jdc.getSource())
181
182 #-----------------------------------------------------------
183 def renameCommandeSiRegle(jdc,command,new_name,liste_regles):
184 #-----------------------------------------------------------
185
186     if command not in jdcSet : return
187     mesRegles=regles.ensembleRegles(liste_regles)
188     renameCommande(jdc,command,new_name,mesRegles)