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
20 from string import split,strip,lowercase,uppercase
21 import re,string,cPickle,os
23 from Extensions.i18n import tr
24 from Noyau.N_CR import CR
27 __Id__="$Id: analyse_catalogue.py,v 1.9.8.1.2.1.2.6 2014-01-23 09:14:44 pnoyret Exp $"
28 __version__="$Name: $"
30 l_noms_commandes = ['OPER','PROC','MACRO','FORM']
31 l_noms_composes=['FACT','BLOC','NUPL','FORM']
32 l_noms_simples=['SIMP',]
33 l_noms=l_noms_composes+l_noms_simples
35 def elimine_commentaires(text):
36 """ Elimine les lignes de commentaires dans text
37 Attention : supprime sauvagement tous les caracteres entre # et le retour chariot ..."""
38 comments = re.compile(r'#[^\n]*')
39 return comments.sub(u'',text)
41 def cherche_nom(text):
42 Whitespace = r'[ \f\t]*'
43 Name = r'[a-zA-Z_]\w*'
44 myexpr = '(u'+Name+')'+Whitespace+'='+Whitespace+'$'
45 a=re.search(myexpr,text)
48 def cherche_args(text):
55 for i in range(1,longueur) :
65 return tr('Erreur ! Erreur !')
68 return text[1:i],text[i+1:] # on enleve les premiere et derniere parentheses
73 def cherche_enfants(self):
75 self.text = strip(self.text)
76 liste = re.split(u'=',self.text,1)
81 if reste[0:4] in l_noms :
82 nom_mc = cherche_nom(arg1+'=')
83 arg_mc, self.text = cherche_args(reste[4:])
84 self.cree_mc(nom_mc,arg_mc,reste[0:4])
87 self.cherche_enfants()
91 except Exception as e:
92 self.cr.fatal(tr("Erreur rencontree dans recherche_enfants : %s", e.__str()))
94 def cree_mc(self,nom_mc,arg_mc,test):
95 if test in l_noms_composes :
96 mc = FACT_CATA(nom_mc,arg_mc,self)
97 self.children.append(mc)
98 elif test in l_noms_simples :
99 mc = SIMP_CATA(nom_mc,self)
100 self.children.append(mc)
102 print tr("Erreur dans la creation du mot-cle : %s", nom_mc)
104 def construit_liste_dico(self):
107 if len(self.children)==0:
112 for child in self.children:
118 print "erreur : ", self.nom, self.__class__
120 class COMMANDE_CATA(ENTITE) :
121 def __init__(self,nom,args,parent):
127 self.cr.debut = "Debut commande %s" %self.nom
128 self.cr.fin = "Fin commande %s" %self.nom
129 self.cherche_enfants()
130 self.construit_liste_dico()
131 parent.cr.add(self.cr)
135 texte_cmd = texte_cmd + 'Commande :' + self.nom + '\n'
136 for child in self.children :
137 texte_cmd = texte_cmd + child.affiche(1)
141 def __init__(self,nom,parent):
144 self.cr.debut = "Debut mot-cle simple %s" %self.nom
145 self.cr.fin = "Fin mot-cle simple %s" %self.nom
146 parent.cr.add(self.cr)
148 def affiche(self,ind):
150 return sep*ind+self.nom+'\n'
152 class FACT_CATA(ENTITE) :
153 def __init__(self,nom,args,parent):
159 self.cr.debut = "Debut mot-cle facteur ou bloc %s" %self.nom
160 self.cr.fin = "Fin mot-cle facteur ou bloc %s" %self.nom
161 self.cherche_enfants()
162 self.construit_liste_dico()
163 parent.cr.add(self.cr)
165 def affiche(self,ind):
168 text = text + sep*ind+self.nom+'\n'
169 for child in self.children :
170 text = text + child.affiche(ind+1)
173 class CATALOGUE_CATA:
174 def __init__(self,parent,fichier):
178 self.cr.debut = "Debut compte-rendu catalogue %s" %self.fichier
179 self.cr.fin = "Fin compte-rendu catalogue %s" %self.fichier
180 self.ouvrir_fichier()
181 self.liste_commandes=[]
182 self.liste_textes_commandes=[]
184 def ouvrir_fichier(self):
186 f=open(self.fichier,'r')
187 self.texte_complet=f.read()
190 print tr("Impossible d'ouvrir le fichier : %s ", str(self.fichier))
191 self.cr.fatal(tr("Impossible d'ouvrir le fichier : %s ", str(self.fichier)))
193 def constr_list_txt_cmd(self,text):
194 text = elimine_commentaires(text)
196 liste=re.split(pattern,text)
197 for i in range(0,len(liste)-1):
198 self.liste_textes_commandes.append(liste[i]+')')
200 def analyse_commande_old(self,text):
201 #if strip(text) == '' or strip(text) ==')': return
202 liste = re.split(u'OPER *\(u',text,1)
204 liste = re.split(u'PROC *\(u',text,1)
206 liste = re.split(u'MACRO *\(u',text,1)
208 print tr("le texte a analyser n'est pas celui d'une commande ou d'un operateur : "), text
209 self.cr.fatal(tr("le texte a analyser n'est pas celui d'une commande ou \
210 d'un operateur : %s", text))
214 nom_cmd = cherche_nom(debut)
215 if nom_cmd == 'erreur !':
216 print tr("Erreur dans la recherche du nom de la commande : "), debut
217 args_cmd,toto = cherche_args(u'(u'+fin)
218 if args_cmd == 'erreur !':
219 print tr("Erreur dans la recherche des args de la commande :") , debut
220 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
221 self.liste_commandes.append(cmd)
223 def analyse_commande(self,text):
224 #if strip(text) == '' or strip(text) ==')': return
225 for nom_cmd in l_noms_commandes:
226 liste = re.split(nom_cmd+' *\(u',text,1)
227 if len(liste) == 2 : break
229 print tr("le texte a analyser n'est pas celui d'une commande connue : \
230 %(v_1)s %(v_2)s", {'v_1': str(l_noms_commandes), 'v_2': text})
231 self.cr.fatal(tr("le texte a analyser n'est pas celui d'une commande connue : \
232 %(v_1)s %(v_2)s", {'v_1': str(l_noms_commandes), 'v_2': text}))
236 nom_cmd = cherche_nom(debut)
237 if nom_cmd == 'erreur !':
238 print tr("Erreur dans la recherche du nom de la commande : "), debut
239 args_cmd,toto = cherche_args(u'(u'+fin)
240 if args_cmd == 'erreur !':
241 print tr("Erreur dans la recherche des args de la commande : "), debut
243 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
244 self.liste_commandes.append(cmd)
246 def analyse_texte(self,texte):
247 self.constr_list_txt_cmd(texte)
249 self.parent.configure_barre(len(self.liste_textes_commandes))
252 for texte_commande in self.liste_textes_commandes :
254 self.parent.update_barre()
257 self.analyse_commande(texte_commande)
258 self.construit_liste_dico()
260 def ecrit_lcmd(self):
261 f=open(u'U:\\EFICAS\\Accas\\cata.txt','w')
262 for cmd in self.liste_commandes :
263 f.write(cmd.affiche())
266 def construit_liste_dico(self):
269 for cmd in self.liste_commandes:
276 """ retourne l'objet rapport du catalogue de commande """
279 def analyse_catalogue(parent,nom_cata):
280 cata = CATALOGUE_CATA(parent,nom_cata)
281 cata.analyse_texte(cata.texte_complet)
284 def analyse_catalogue_commande(parent,nom_cata):
285 cata = CATALOGUE_CATA(parent,nom_cata)
286 cata.analyse_commande(cata.texte_complet)
287 cata.construit_liste_dico()
291 def make_cata_pickle(fic_cata):
293 Lance l'analyse de l'ordre des mots-cles dans le catalogue dont le nom
294 est passe en argument et sauvegarde ces infos dans le fichier pickle relu
297 fic_cata_p = os.path.splitext(fic_cata)[0]+'_pickled.py'
298 cata_ordonne = analyse_catalogue(None,fic_cata)
299 f = open(fic_cata_p,'w+')
300 p = cPickle.Pickler(f)
301 p.dump(cata_ordonne.entites)
304 if __name__ == "__main__" :
306 profile.run(u"analyse_catalogue(None,'U:\\EFICAS\\Cata\\cata_saturne.py')")