1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021 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
20 from __future__ import absolute_import
21 from __future__ import print_function
23 from builtins import str
24 from builtins import range
25 from builtins import object
29 import re,six.moves.cPickle,os
31 from Extensions.i18n import tr
32 from Noyau.N_CR import CR
34 from six.moves import range
37 __Id__="$Id: analyseCatalogue.py,v 1.9.8.1.2.1.2.6 2014-01-23 09:14:44 pnoyret Exp $"
38 __version__="$Name: $"
40 l_noms_commandes = ['OPER','PROC','MACRO','FORM']
41 l_noms_composes=['FACT','BLOC','NUPL','FORM']
42 l_noms_simples=['SIMP',]
43 l_noms=l_noms_composes+l_noms_simples
45 def elimineCommentaires(text):
46 """ Elimine les lignes de commentaires dans text
47 Attention : supprime sauvagement tous les caracteres entre # et le retour chariot ..."""
48 comments = re.compile(r'#[^\n]*')
49 return comments.sub(u'',text)
52 Whitespace = r'[ \f\t]*'
53 Name = r'[a-zA-Z_]\w*'
54 myexpr = '(u'+Name+')'+Whitespace+'='+Whitespace+'$'
55 a=re.search(myexpr,text)
58 def chercheArgs(text):
65 for i in range(1,longueur) :
75 return tr('Erreur ! Erreur !')
78 return text[1:i],text[i+1:] # on enleve les premiere et derniere parentheses
83 def chercheEnfants(self):
85 self.text = self.text.strip()
86 liste = re.split(u'=',self.text,1)
91 if reste[0:4] in l_noms :
92 nom_mc = chercheNom(arg1+'=')
93 arg_mc, self.text = chercheArgs(reste[4:])
94 self.creeMc(nom_mc,arg_mc,reste[0:4])
101 except Exception as e:
102 self.cr.fatal(tr("Erreur rencontree dans rechercheEnfants : %s", e.__str()))
104 def creeMc(self,nom_mc,arg_mc,test):
105 if test in l_noms_composes :
106 mc = FACT_CATA(nom_mc,arg_mc,self)
107 self.children.append(mc)
108 elif test in l_noms_simples :
109 mc = SIMP_CATA(nom_mc,self)
110 self.children.append(mc)
112 print (tr("Erreur dans la creation du mot-cle : %s", nom_mc) )
114 def construitListeDico(self):
117 if len(self.children)==0:
122 for child in self.children:
128 print (("erreur : ", self.nom, self.__class__))
130 class COMMANDE_CATA(ENTITE) :
131 def __init__(self,nom,args,parent):
137 self.cr.debut = "Debut commande %s" %self.nom
138 self.cr.fin = "Fin commande %s" %self.nom
139 self.chercheEnfants()
140 self.construitListeDico()
141 parent.cr.add(self.cr)
145 texte_cmd = texte_cmd + 'Commande :' + self.nom + '\n'
146 for child in self.children :
147 texte_cmd = texte_cmd + child.affiche(1)
150 class SIMP_CATA(object) :
151 def __init__(self,nom,parent):
154 self.cr.debut = "Debut mot-cle simple %s" %self.nom
155 self.cr.fin = "Fin mot-cle simple %s" %self.nom
156 parent.cr.add(self.cr)
158 def affiche(self,ind):
160 return sep*ind+self.nom+'\n'
162 class FACT_CATA(ENTITE) :
163 def __init__(self,nom,args,parent):
169 self.cr.debut = "Debut mot-cle facteur ou bloc %s" %self.nom
170 self.cr.fin = "Fin mot-cle facteur ou bloc %s" %self.nom
171 self.chercheEnfants()
172 self.construitListeDico()
173 parent.cr.add(self.cr)
175 def affiche(self,ind):
178 text = text + sep*ind+self.nom+'\n'
179 for child in self.children :
180 text = text + child.affiche(ind+1)
183 class CATALOGUE_CATA(object):
184 def __init__(self,parent,fichier):
188 self.cr.debut = "Debut compte-rendu catalogue %s" %self.fichier
189 self.cr.fin = "Fin compte-rendu catalogue %s" %self.fichier
191 self.liste_commandes=[]
192 self.liste_textes_commandes=[]
194 def ouvrirFichier(self):
196 f=open(self.fichier,'r')
197 self.texte_complet=f.read()
200 print((tr("Impossible d'ouvrir le fichier : %s ", str(self.fichier))))
201 self.cr.fatal(tr("Impossible d'ouvrir le fichier : %s ", str(self.fichier)))
203 def constrListTxtCmd(self,text):
204 text = elimineCommentaires(text)
206 liste=re.split(pattern,text)
207 for i in range(0,len(liste)-1):
208 self.liste_textes_commandes.append(liste[i]+')')
210 def analyseCommandeOld(self,text):
211 liste = re.split(u'OPER *\(u',text,1)
213 liste = re.split(u'PROC *\(u',text,1)
215 liste = re.split(u'MACRO *\(u',text,1)
217 print ((tr("le texte a analyser n'est pas celui d'une commande ou d'un operateur : "), text))
218 self.cr.fatal(tr("le texte a analyser n'est pas celui d'une commande ou \
219 d'un operateur : %s", text))
223 nom_cmd = chercheNom(debut)
224 if nom_cmd == 'erreur !':
225 print((tr("Erreur dans la recherche du nom de la commande : "), debut))
226 args_cmd,toto = chercheArgs(u'(u'+fin)
227 if args_cmd == 'erreur !':
228 print((tr("Erreur dans la recherche des args de la commande :") , debut))
229 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
230 self.liste_commandes.append(cmd)
232 def analyseCommande(self,text):
233 for nom_cmd in l_noms_commandes:
234 liste = re.split(nom_cmd+' *\(u',text,1)
235 if len(liste) == 2 : break
237 print((tr("le texte a analyser n'est pas celui d'une commande connue : \
238 %(v_1)s %(v_2)s", {'v_1': str(l_noms_commandes), 'v_2': text})))
239 self.cr.fatal(tr("le texte a analyser n'est pas celui d'une commande connue : \
240 %(v_1)s %(v_2)s", {'v_1': str(l_noms_commandes), 'v_2': text}))
244 nom_cmd = chercheNom(debut)
245 if nom_cmd == 'erreur !':
246 print(( tr("Erreur dans la recherche du nom de la commande : "), debut))
247 args_cmd,toto = chercheArgs(u'(u'+fin)
248 if args_cmd == 'erreur !':
249 print(( tr("Erreur dans la recherche des args de la commande : "), debut))
251 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
252 self.liste_commandes.append(cmd)
254 def analyseTexte(self,texte):
255 self.constrListTxtCmd(texte)
257 self.parent.configure_barre(len(self.liste_textes_commandes))
260 for texte_commande in self.liste_textes_commandes :
262 self.parent.update_barre()
265 self.analyseCommande(texte_commande)
266 self.construitListeDico()
269 f=open(u'U:\\EFICAS\\Accas\\cata.txt','w')
270 for cmd in self.liste_commandes :
271 f.write(cmd.affiche())
274 def construitListeDico(self):
277 for cmd in self.liste_commandes:
284 """ retourne l'objet rapport du catalogue de commande """
287 def analyseCatalogue(parent,nom_cata):
288 cata = CATALOGUE_CATA(parent,nom_cata)
289 cata.analyseTexte(cata.texte_complet)
292 def analyseCatalogueCommande(parent,nom_cata):
293 cata = CATALOGUE_CATA(parent,nom_cata)
294 cata.analyseCommande(cata.texte_complet)
295 cata.construitListeDico()
299 def makeCataPickle(fic_cata):
301 Lance l'analyse de l'ordre des mots-cles dans le catalogue dont le nom
302 est passe en argument et sauvegarde ces infos dans le fichier pickle relu
305 fic_cata_p = os.path.splitext(fic_cata)[0]+'_pickled.py'
306 cata_ordonne = analyseCatalogue(None,fic_cata)
307 f = open(fic_cata_p,'w+')
308 p = six.moves.cPickle.Pickler(f)
309 p.dump(cata_ordonne.entites)
312 if __name__ == "__main__" :
314 profile.run(u"analyseCatalogue(None,'U:\\EFICAS\\Cata\\cata_saturne.py')")