Salome HOME
modif pour MT
[tools/eficas.git] / Editeur / analyse_catalogue.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 from __future__ import absolute_import
21 from __future__ import print_function
22 try : 
23    from builtins import str
24    from builtins import range
25    from builtins import object
26 except :
27    pass
28
29 import re,six.moves.cPickle,os
30
31 from Extensions.i18n import tr
32 from Noyau.N_CR import CR
33
34 from six.moves import range
35
36 #
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:  $"
39 #
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
44
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)
50
51 def chercheNom(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)
56         return a.group(1)
57
58 def chercheArgs(text):
59         text = text.strip()
60         longueur = len(text)
61         if text[0] != '(u':
62                 return 'erreur !'
63         else :
64                 nbpar = 1
65                 for i in range(1,longueur) :
66                         if text[i] =='(u':
67                                 nbpar = nbpar + 1
68                         elif text[i] == ')':
69                                 nbpar = nbpar - 1
70                         else :
71                                 continue
72                         if nbpar == 0:
73                                 break
74                 if nbpar != 0 :
75                         return tr('Erreur ! Erreur !')
76                 else :
77                         try :
78                                 return text[1:i],text[i+1:] # on enleve les premiere et derniere parentheses
79                         except :
80                                 return text[1:i],''
81
82 class ENTITE(object):
83         def chercheEnfants(self):
84                 try :
85                         self.text = self.text.strip()
86                         liste = re.split(u'=',self.text,1)
87                         if len(liste)>1 :
88                                 arg1=liste[0]
89                                 reste=liste[1]
90                                 reste = reste.strip()
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])
95                                 else :
96                                         self.text = reste
97                                 self.chercheEnfants()
98                         else :
99                                 # pas de = rencontre
100                                 return
101                 except Exception as e:
102                         self.cr.fatal(tr("Erreur rencontree dans rechercheEnfants : %s", e.__str()))
103                 
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)
111                 else :
112                         print (tr("Erreur dans la creation du mot-cle : %s", nom_mc) )
113
114         def construitListeDico(self):
115                 l=[]
116                 d={}
117                 if len(self.children)==0:
118                         self.ordre_mc = l
119                         self.entites = d
120                         return
121                 try :
122                         for child in self.children:
123                                 l.append(child.nom)
124                                 d[child.nom]=child
125                         self.ordre_mc = l
126                         self.entites = d
127                 except:
128                         print (("erreur : ", self.nom,  self.__class__))
129                 
130 class COMMANDE_CATA(ENTITE) :
131         def __init__(self,nom,args,parent):
132                 self.nom = nom
133                 self.args = args
134                 self.children = []
135                 self.text = args
136                 self.cr = CR()
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)
142
143         def affiche(self):
144                 texte_cmd = '\n'
145                 texte_cmd = texte_cmd + 'Commande :' + self.nom + '\n'
146                 for child in self.children :
147                         texte_cmd = texte_cmd + child.affiche(1)
148                 return texte_cmd
149
150 class SIMP_CATA(object) :
151         def __init__(self,nom,parent):
152                 self.nom = nom
153                 self.cr = CR()
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)
157
158         def affiche(self,ind):
159                 sep = ' '*5
160                 return sep*ind+self.nom+'\n'
161
162 class FACT_CATA(ENTITE) :
163         def __init__(self,nom,args,parent):
164                 self.nom=nom
165                 self.args=args
166                 self.children = []
167                 self.text=args
168                 self.cr = CR()
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)
174
175         def affiche(self,ind):
176                 sep = ' '*5
177                 text = ''
178                 text = text + sep*ind+self.nom+'\n'
179                 for child in self.children :
180                         text = text + child.affiche(ind+1)
181                 return text
182                 
183 class CATALOGUE_CATA(object):
184         def __init__(self,parent,fichier):
185                 self.parent = parent
186                 self.fichier=fichier
187                 self.cr = CR()
188                 self.cr.debut = "Debut compte-rendu catalogue %s" %self.fichier
189                 self.cr.fin = "Fin compte-rendu catalogue %s" %self.fichier
190                 self.ouvrirFichier()
191                 self.liste_commandes=[]
192                 self.liste_textes_commandes=[]
193
194         def ouvrirFichier(self):
195                 try :
196                         f=open(self.fichier,'r')
197                         self.texte_complet=f.read()
198                         f.close()
199                 except :
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)))
202
203         def constrListTxtCmd(self,text):
204                 text = elimineCommentaires(text)
205                 pattern = '\) *;'
206                 liste=re.split(pattern,text)
207                 for i in range(0,len(liste)-1):
208                         self.liste_textes_commandes.append(liste[i]+')')
209
210         def analyseCommandeOld(self,text):
211                 liste = re.split(u'OPER *\(u',text,1)
212                 if len(liste) < 2 :
213                         liste = re.split(u'PROC *\(u',text,1)
214                 if len(liste) < 2 :
215                         liste = re.split(u'MACRO *\(u',text,1)
216                 if len(liste) < 2 :
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))
220                         return
221                 debut = liste[0]
222                 fin = liste[1]
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)
231
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
236                 if len(liste) < 2 :
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}))
241                         return
242                 debut = liste[0]
243                 fin = liste[1]
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))
250                         print((tr(fin)))
251                 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
252                 self.liste_commandes.append(cmd)
253                 
254         def analyseTexte(self,texte):
255                 self.constrListTxtCmd(texte)
256                 try:
257                         self.parent.configure_barre(len(self.liste_textes_commandes))
258                 except:
259                         pass
260                 for texte_commande in self.liste_textes_commandes :
261                         try:
262                                 self.parent.update_barre()
263                         except:
264                                 pass
265                         self.analyseCommande(texte_commande)
266                 self.construitListeDico()
267
268         def ecritLcmd(self):
269                 f=open(u'U:\\EFICAS\\Accas\\cata.txt','w')
270                 for cmd in self.liste_commandes :
271                         f.write(cmd.affiche())
272                 f.close()
273
274         def construitListeDico(self):
275                 l=[]
276                 d={}
277                 for cmd in self.liste_commandes:
278                         l.append(cmd.nom)
279                         d[cmd.nom]=cmd
280                 self.ordre_mc = l
281                 self.entites = d
282
283         def report(self):
284                 """ retourne l'objet rapport du catalogue de commande """
285                 return self.cr
286
287 def analyseCatalogue(parent,nom_cata):
288         cata = CATALOGUE_CATA(parent,nom_cata)
289         cata.analyseTexte(cata.texte_complet)
290         return cata
291
292 def analyseCatalogueCommande(parent,nom_cata):
293         cata = CATALOGUE_CATA(parent,nom_cata)
294         cata.analyseCommande(cata.texte_complet)
295         cata.construitListeDico()
296         return cata
297
298
299 def makeCataPickle(fic_cata):
300         """
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
303         par Eficas
304         """
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)
310         f.close()
311         
312 if __name__ == "__main__" :
313         import profile
314         profile.run(u"analyseCatalogue(None,'U:\\EFICAS\\Cata\\cata_saturne.py')")
315
316
317
318
319
320
321
322
323
324
325
326                                 
327