Salome HOME
gestion des listes et label sur 2
[tools/eficas.git] / Editeur / analyse_catalogue.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 from string import split,strip,lowercase,uppercase
21 import re,string,cPickle,os
22
23 from Extensions.i18n import tr
24 from Noyau.N_CR import CR
25
26 #
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:  $"
29 #
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
34
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)
40
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)
46         return a.group(1)
47
48 def cherche_args(text):
49         text = strip(text)
50         longueur = len(text)
51         if text[0] != '(u':
52                 return 'erreur !'
53         else :
54                 nbpar = 1
55                 for i in range(1,longueur) :
56                         if text[i] =='(u':
57                                 nbpar = nbpar + 1
58                         elif text[i] == ')':
59                                 nbpar = nbpar - 1
60                         else :
61                                 continue
62                         if nbpar == 0:
63                                 break
64                 if nbpar != 0 :
65                         return tr('Erreur ! Erreur !')
66                 else :
67                         try :
68                                 return text[1:i],text[i+1:] # on enleve les premiere et derniere parentheses
69                         except :
70                                 return text[1:i],''
71
72 class ENTITE:
73         def cherche_enfants(self):
74                 try :
75                         self.text = strip(self.text)
76                         liste = re.split(u'=',self.text,1)
77                         if len(liste)>1 :
78                                 arg1=liste[0]
79                                 reste=liste[1]
80                                 reste = strip(reste)
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])
85                                 else :
86                                         self.text = reste
87                                 self.cherche_enfants()
88                         else :
89                                 # pas de = rencontre
90                                 return
91                 except Exception as e:
92                         self.cr.fatal(tr("Erreur rencontree dans recherche_enfants : %s", e.__str()))
93                 
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)
101                 else :
102                         print tr("Erreur dans la creation du mot-cle : %s", nom_mc)
103
104         def construit_liste_dico(self):
105                 l=[]
106                 d={}
107                 if len(self.children)==0:
108                         self.ordre_mc = l
109                         self.entites = d
110                         return
111                 try :
112                         for child in self.children:
113                                 l.append(child.nom)
114                                 d[child.nom]=child
115                         self.ordre_mc = l
116                         self.entites = d
117                 except:
118                         print "erreur : ", self.nom,  self.__class__
119                 
120 class COMMANDE_CATA(ENTITE) :
121         def __init__(self,nom,args,parent):
122                 self.nom = nom
123                 self.args = args
124                 self.children = []
125                 self.text = args
126                 self.cr = CR()
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)
132
133         def affiche(self):
134                 texte_cmd = '\n'
135                 texte_cmd = texte_cmd + 'Commande :' + self.nom + '\n'
136                 for child in self.children :
137                         texte_cmd = texte_cmd + child.affiche(1)
138                 return texte_cmd
139
140 class SIMP_CATA :
141         def __init__(self,nom,parent):
142                 self.nom = nom
143                 self.cr = CR()
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)
147
148         def affiche(self,ind):
149                 sep = ' '*5
150                 return sep*ind+self.nom+'\n'
151
152 class FACT_CATA(ENTITE) :
153         def __init__(self,nom,args,parent):
154                 self.nom=nom
155                 self.args=args
156                 self.children = []
157                 self.text=args
158                 self.cr = CR()
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)
164
165         def affiche(self,ind):
166                 sep = ' '*5
167                 text = ''
168                 text = text + sep*ind+self.nom+'\n'
169                 for child in self.children :
170                         text = text + child.affiche(ind+1)
171                 return text
172                 
173 class CATALOGUE_CATA:
174         def __init__(self,parent,fichier):
175                 self.parent = parent
176                 self.fichier=fichier
177                 self.cr = CR()
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=[]
183
184         def ouvrir_fichier(self):
185                 try :
186                         f=open(self.fichier,'r')
187                         self.texte_complet=f.read()
188                         f.close()
189                 except :
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)))
192
193         def constr_list_txt_cmd(self,text):
194                 text = elimine_commentaires(text)
195                 pattern = '\) *;'
196                 liste=re.split(pattern,text)
197                 for i in range(0,len(liste)-1):
198                         self.liste_textes_commandes.append(liste[i]+')')
199
200         def analyse_commande_old(self,text):
201                 #if strip(text) == '' or strip(text) ==')': return
202                 liste = re.split(u'OPER *\(u',text,1)
203                 if len(liste) < 2 :
204                         liste = re.split(u'PROC *\(u',text,1)
205                 if len(liste) < 2 :
206                         liste = re.split(u'MACRO *\(u',text,1)
207                 if len(liste) < 2 :
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))
211                         return
212                 debut = liste[0]
213                 fin = liste[1]
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)
222
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
228                 if len(liste) < 2 :
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}))
233                         return
234                 debut = liste[0]
235                 fin = liste[1]
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
242                         print tr(fin)
243                 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
244                 self.liste_commandes.append(cmd)
245                 
246         def analyse_texte(self,texte):
247                 self.constr_list_txt_cmd(texte)
248                 try:
249                         self.parent.configure_barre(len(self.liste_textes_commandes))
250                 except:
251                         pass
252                 for texte_commande in self.liste_textes_commandes :
253                         try:
254                                 self.parent.update_barre()
255                         except:
256                                 pass
257                         self.analyse_commande(texte_commande)
258                 self.construit_liste_dico()
259
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())
264                 f.close()
265
266         def construit_liste_dico(self):
267                 l=[]
268                 d={}
269                 for cmd in self.liste_commandes:
270                         l.append(cmd.nom)
271                         d[cmd.nom]=cmd
272                 self.ordre_mc = l
273                 self.entites = d
274
275         def report(self):
276                 """ retourne l'objet rapport du catalogue de commande """
277                 return self.cr
278
279 def analyse_catalogue(parent,nom_cata):
280         cata = CATALOGUE_CATA(parent,nom_cata)
281         cata.analyse_texte(cata.texte_complet)
282         return cata
283
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()
288         return cata
289
290
291 def make_cata_pickle(fic_cata):
292         """
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
295         par Eficas
296         """
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)
302         f.close()
303         
304 if __name__ == "__main__" :
305         import profile
306         profile.run(u"analyse_catalogue(None,'U:\\EFICAS\\Cata\\cata_saturne.py')")
307
308
309
310
311
312
313
314
315
316
317
318                                 
319