Salome HOME
chgt Copyrigth
[tools/eficas.git] / Editeur / analyse_catalogue.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021   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 object
25 except :
26    pass
27
28 import re,six.moves.cPickle,os
29
30 from Extensions.i18n import tr
31 from Noyau.N_CR import CR
32
33
34 #
35 __Id__="$Id: analyseCatalogue.py,v 1.9.8.1.2.1.2.6 2014-01-23 09:14:44 pnoyret Exp $"
36 __version__="$Name:  $"
37 #
38 l_noms_commandes = ['OPER','PROC','MACRO','FORM']
39 l_noms_composes=['FACT','BLOC','NUPL','FORM']
40 l_noms_simples=['SIMP',]
41 l_noms=l_noms_composes+l_noms_simples
42
43 def elimineCommentaires(text):
44         """ Elimine les lignes de commentaires dans text
45         Attention : supprime sauvagement tous les caracteres entre # et le retour chariot ..."""
46         comments = re.compile(r'#[^\n]*')
47         return comments.sub(u'',text)
48
49 def chercheNom(text):
50         Whitespace = r'[ \f\t]*'
51         Name = r'[a-zA-Z_]\w*'
52         myexpr = '(u'+Name+')'+Whitespace+'='+Whitespace+'$'
53         a=re.search(myexpr,text)
54         return a.group(1)
55
56 def chercheArgs(text):
57         text = text.strip()
58         longueur = len(text)
59         if text[0] != '(u':
60                 return 'erreur !'
61         else :
62                 nbpar = 1
63                 for i in range(1,longueur) :
64                         if text[i] =='(u':
65                                 nbpar = nbpar + 1
66                         elif text[i] == ')':
67                                 nbpar = nbpar - 1
68                         else :
69                                 continue
70                         if nbpar == 0:
71                                 break
72                 if nbpar != 0 :
73                         return tr('Erreur ! Erreur !')
74                 else :
75                         try :
76                                 return text[1:i],text[i+1:] # on enleve les premiere et derniere parentheses
77                         except :
78                                 return text[1:i],''
79
80 class ENTITE(object):
81         def chercheEnfants(self):
82                 try :
83                         self.text = self.text.strip()
84                         liste = re.split(u'=',self.text,1)
85                         if len(liste)>1 :
86                                 arg1=liste[0]
87                                 reste=liste[1]
88                                 reste = reste.strip()
89                                 if reste[0:4] in l_noms :
90                                         nom_mc = chercheNom(arg1+'=')
91                                         arg_mc, self.text = chercheArgs(reste[4:])
92                                         self.creeMc(nom_mc,arg_mc,reste[0:4])
93                                 else :
94                                         self.text = reste
95                                 self.chercheEnfants()
96                         else :
97                                 # pas de = rencontre
98                                 return
99                 except Exception as e:
100                         self.cr.fatal(tr("Erreur rencontree dans rechercheEnfants : %s", e.__str()))
101                 
102         def creeMc(self,nom_mc,arg_mc,test):
103                 if test in l_noms_composes :
104                         mc = FACT_CATA(nom_mc,arg_mc,self)
105                         self.children.append(mc)
106                 elif test in l_noms_simples :
107                         mc = SIMP_CATA(nom_mc,self)
108                         self.children.append(mc)
109                 else :
110                         print (tr("Erreur dans la creation du mot-cle : %s", nom_mc) )
111
112         def construitListeDico(self):
113                 l=[]
114                 d={}
115                 if len(self.children)==0:
116                         self.ordre_mc = l
117                         self.entites = d
118                         return
119                 try :
120                         for child in self.children:
121                                 l.append(child.nom)
122                                 d[child.nom]=child
123                         self.ordre_mc = l
124                         self.entites = d
125                 except:
126                         print (("erreur : ", self.nom,  self.__class__))
127                 
128 class COMMANDE_CATA(ENTITE) :
129         def __init__(self,nom,args,parent):
130                 self.nom = nom
131                 self.args = args
132                 self.children = []
133                 self.text = args
134                 self.cr = CR()
135                 self.cr.debut = "Debut commande %s" %self.nom
136                 self.cr.fin = "Fin commande %s" %self.nom
137                 self.chercheEnfants()
138                 self.construitListeDico()
139                 parent.cr.add(self.cr)
140
141         def affiche(self):
142                 texte_cmd = '\n'
143                 texte_cmd = texte_cmd + 'Commande :' + self.nom + '\n'
144                 for child in self.children :
145                         texte_cmd = texte_cmd + child.affiche(1)
146                 return texte_cmd
147
148 class SIMP_CATA(object) :
149         def __init__(self,nom,parent):
150                 self.nom = nom
151                 self.cr = CR()
152                 self.cr.debut = "Debut mot-cle simple %s" %self.nom
153                 self.cr.fin = "Fin mot-cle simple %s" %self.nom
154                 parent.cr.add(self.cr)
155
156         def affiche(self,ind):
157                 sep = ' '*5
158                 return sep*ind+self.nom+'\n'
159
160 class FACT_CATA(ENTITE) :
161         def __init__(self,nom,args,parent):
162                 self.nom=nom
163                 self.args=args
164                 self.children = []
165                 self.text=args
166                 self.cr = CR()
167                 self.cr.debut = "Debut mot-cle facteur ou bloc %s" %self.nom
168                 self.cr.fin = "Fin mot-cle facteur ou bloc %s" %self.nom
169                 self.chercheEnfants()
170                 self.construitListeDico()
171                 parent.cr.add(self.cr)
172
173         def affiche(self,ind):
174                 sep = ' '*5
175                 text = ''
176                 text = text + sep*ind+self.nom+'\n'
177                 for child in self.children :
178                         text = text + child.affiche(ind+1)
179                 return text
180                 
181 class CATALOGUE_CATA(object):
182         def __init__(self,parent,fichier):
183                 self.parent = parent
184                 self.fichier=fichier
185                 self.cr = CR()
186                 self.cr.debut = "Debut compte-rendu catalogue %s" %self.fichier
187                 self.cr.fin = "Fin compte-rendu catalogue %s" %self.fichier
188                 self.ouvrirFichier()
189                 self.liste_commandes=[]
190                 self.liste_textes_commandes=[]
191
192         def ouvrirFichier(self):
193                 try :
194                         f=open(self.fichier,'r')
195                         self.texte_complet=f.read()
196                         f.close()
197                 except :
198                         print((tr("Impossible d'ouvrir le fichier : %s ", str(self.fichier))))
199                         self.cr.fatal(tr("Impossible d'ouvrir le fichier : %s ", str(self.fichier)))
200
201         def constrListTxtCmd(self,text):
202                 text = elimineCommentaires(text)
203                 pattern = '\) *;'
204                 liste=re.split(pattern,text)
205                 for i in range(0,len(liste)-1):
206                         self.liste_textes_commandes.append(liste[i]+')')
207
208         def analyseCommandeOld(self,text):
209                 liste = re.split(u'OPER *\(u',text,1)
210                 if len(liste) < 2 :
211                         liste = re.split(u'PROC *\(u',text,1)
212                 if len(liste) < 2 :
213                         liste = re.split(u'MACRO *\(u',text,1)
214                 if len(liste) < 2 :
215                         print ((tr("le texte a analyser n'est pas celui d'une commande ou d'un operateur : "), text))
216                         self.cr.fatal(tr("le texte a analyser n'est pas celui d'une commande ou \
217                                          d'un operateur : %s", text))
218                         return
219                 debut = liste[0]
220                 fin = liste[1]
221                 nom_cmd = chercheNom(debut)
222                 if nom_cmd == 'erreur !':
223                         print((tr("Erreur dans la recherche  du nom de la commande : "), debut))
224                 args_cmd,toto = chercheArgs(u'(u'+fin)
225                 if args_cmd == 'erreur !':
226                         print((tr("Erreur dans la recherche des  args de la commande :") , debut))
227                 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
228                 self.liste_commandes.append(cmd)
229
230         def analyseCommande(self,text):
231                 for nom_cmd in l_noms_commandes:
232                         liste = re.split(nom_cmd+' *\(u',text,1)
233                         if len(liste) == 2 : break
234                 if len(liste) < 2 :
235                         print((tr("le texte a analyser n'est pas celui d'une commande connue : \
236                                         %(v_1)s %(v_2)s", {'v_1': str(l_noms_commandes), 'v_2': text})))
237                         self.cr.fatal(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                         return
240                 debut = liste[0]
241                 fin = liste[1]
242                 nom_cmd = chercheNom(debut)
243                 if nom_cmd == 'erreur !':
244                         print(( tr("Erreur dans la recherche du  nom de la commande : "), debut))
245                 args_cmd,toto = chercheArgs(u'(u'+fin)
246                 if args_cmd == 'erreur !':
247                         print(( tr("Erreur dans la recherche des args de la commande : "), debut))
248                         print((tr(fin)))
249                 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
250                 self.liste_commandes.append(cmd)
251                 
252         def analyseTexte(self,texte):
253                 self.constrListTxtCmd(texte)
254                 try:
255                         self.parent.configure_barre(len(self.liste_textes_commandes))
256                 except:
257                         pass
258                 for texte_commande in self.liste_textes_commandes :
259                         try:
260                                 self.parent.update_barre()
261                         except:
262                                 pass
263                         self.analyseCommande(texte_commande)
264                 self.construitListeDico()
265
266         def ecritLcmd(self):
267                 f=open(u'U:\\EFICAS\\Accas\\cata.txt','w')
268                 for cmd in self.liste_commandes :
269                         f.write(cmd.affiche())
270                 f.close()
271
272         def construitListeDico(self):
273                 l=[]
274                 d={}
275                 for cmd in self.liste_commandes:
276                         l.append(cmd.nom)
277                         d[cmd.nom]=cmd
278                 self.ordre_mc = l
279                 self.entites = d
280
281         def report(self):
282                 """ retourne l'objet rapport du catalogue de commande """
283                 return self.cr
284
285 def analyseCatalogue(parent,nom_cata):
286         cata = CATALOGUE_CATA(parent,nom_cata)
287         cata.analyseTexte(cata.texte_complet)
288         return cata
289
290 def analyseCatalogueCommande(parent,nom_cata):
291         cata = CATALOGUE_CATA(parent,nom_cata)
292         cata.analyseCommande(cata.texte_complet)
293         cata.construitListeDico()
294         return cata
295
296
297 def makeCataPickle(ficCata):
298         """
299         Lance l'analyse de l'ordre des mots-cles dans le catalogue dont le nom
300         est passe en argument et sauvegarde ces infos dans le fichier pickle relu
301         par Eficas
302         """
303         ficCata_p = os.path.splitext(ficCata)[0]+'_pickled.py'
304         cata_ordonne = analyseCatalogue(None,ficCata)
305         f = open(ficCata_p,'w+')
306         p = six.moves.cPickle.Pickler(f)
307         p.dump(cata_ordonne.entites)
308         f.close()
309         
310 if __name__ == "__main__" :
311         import profile
312         profile.run(u"analyseCatalogue(None,'U:\\EFICAS\\Cata\\cata_saturne.py')")
313
314
315
316
317
318
319
320
321
322
323
324                                 
325