1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 from Traducteur.parseur import FactNode
23 from Traducteur.load import jdcSet
24 from Traducteur import regles
25 from Traducteur.dictErreurs import EcritErreur
29 #on n'a qu'un mocle par commande.
30 #en fin de traitement, on remet à jour l'arbre syntaxique (lineno,colno,etc.)
32 #--------------------------------------------------------------------------------
33 def renameMotCle(jdc,command,mocle,new_name, erreur=0,ensemble=regles.SansRegle):
34 #--------------------------------------------------------------------------------
35 if command not in jdcSet : return
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
43 if debug:print "Renommage de:",c.name,mc.name,mc.lineno,mc.colno
45 EcritErreur((command,mocle),c.lineno)
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)
53 if boolChange : jdc.reset(jdc.getSource())
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)
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)
68 #-------------------------------------------
69 def renameOper(jdc,command,new_name):
70 #-------------------------------------------
71 if command not in jdcSet : return
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)
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())
85 #----------------------------------------------------------
86 def decaleLignesdeNBlancs(jdc,premiere,derniere,nbBlanc):
87 #----------------------------------------------------------
89 while ligne < derniere :
90 s=jdc.getLines()[ligne]
92 jdc.getLines()[ligne] = nbBlanc*" " + s
94 toutblancs=-1*nbBlanc*" "
95 if jdc.getLines()[ligne][0:-1*nbBlanc] == toutblancs:
96 jdc.getLines()[ligne] = s[-1*nbBlanc:]
99 #---------------------------------------------------------------------------------------------
100 def renameMotCleInFact(jdc,command,fact,mocle,new_name, ensemble=regles.SansRegle, erreur=0):
101 #---------------------------------------------------------------------------------------------
102 if command not in jdcSet : return
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
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):]
119 EcritErreur((command,fact,mocle),c.lineno)
121 logging.info("Renommage de: %s, ligne %s, en %s",n.name,n.lineno,new_name)
123 if boolChange : jdc.reset(jdc.getSource())
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)
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)
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
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):]
152 EcritErreur((command,fact,mocle),c.lineno)
154 logging.info("Renommage de: %s, ligne %s, en %s",n.name,n.lineno,new_name)
156 if boolChange : jdc.reset(jdc.getSource())
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
167 if ensemble != regles.SansRegle :
168 logging.info("Traitement de %s renomme en %s sous conditions", command, new_name)
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
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):]
180 if boolChange : jdc.reset(jdc.getSource())
182 #-----------------------------------------------------------
183 def renameCommandeSiRegle(jdc,command,new_name,liste_regles):
184 #-----------------------------------------------------------
186 if command not in jdcSet : return
187 mesRegles=regles.ensembleRegles(liste_regles)
188 renameCommande(jdc,command,new_name,mesRegles)