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 object
28 import re,six.moves.cPickle,os
30 from Extensions.i18n import tr
31 from Noyau.N_CR import CR
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: $"
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
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)
50 Whitespace = r'[ \f\t]*'
51 Name = r'[a-zA-Z_]\w*'
52 myexpr = '(u'+Name+')'+Whitespace+'='+Whitespace+'$'
53 a=re.search(myexpr,text)
56 def chercheArgs(text):
63 for i in range(1,longueur) :
73 return tr('Erreur ! Erreur !')
76 return text[1:i],text[i+1:] # on enleve les premiere et derniere parentheses
81 def chercheEnfants(self):
83 self.text = self.text.strip()
84 liste = re.split(u'=',self.text,1)
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])
99 except Exception as e:
100 self.cr.fatal(tr("Erreur rencontree dans rechercheEnfants : %s", e.__str()))
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)
110 print (tr("Erreur dans la creation du mot-cle : %s", nom_mc) )
112 def construitListeDico(self):
115 if len(self.children)==0:
120 for child in self.children:
126 print (("erreur : ", self.nom, self.__class__))
128 class COMMANDE_CATA(ENTITE) :
129 def __init__(self,nom,args,parent):
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)
143 texte_cmd = texte_cmd + 'Commande :' + self.nom + '\n'
144 for child in self.children :
145 texte_cmd = texte_cmd + child.affiche(1)
148 class SIMP_CATA(object) :
149 def __init__(self,nom,parent):
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)
156 def affiche(self,ind):
158 return sep*ind+self.nom+'\n'
160 class FACT_CATA(ENTITE) :
161 def __init__(self,nom,args,parent):
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)
173 def affiche(self,ind):
176 text = text + sep*ind+self.nom+'\n'
177 for child in self.children :
178 text = text + child.affiche(ind+1)
181 class CATALOGUE_CATA(object):
182 def __init__(self,parent,fichier):
186 self.cr.debut = "Debut compte-rendu catalogue %s" %self.fichier
187 self.cr.fin = "Fin compte-rendu catalogue %s" %self.fichier
189 self.liste_commandes=[]
190 self.liste_textes_commandes=[]
192 def ouvrirFichier(self):
194 with open(self.fichier) as fd:
195 self.texte_complet=fd.read()
197 print((tr("Impossible d'ouvrir le fichier : %s ", str(self.fichier))))
198 self.cr.fatal(tr("Impossible d'ouvrir le fichier : %s ", str(self.fichier)))
200 def constrListTxtCmd(self,text):
201 text = elimineCommentaires(text)
203 liste=re.split(pattern,text)
204 for i in range(0,len(liste)-1):
205 self.liste_textes_commandes.append(liste[i]+')')
207 def analyseCommandeOld(self,text):
208 liste = re.split(u'OPER *\(u',text,1)
210 liste = re.split(u'PROC *\(u',text,1)
212 liste = re.split(u'MACRO *\(u',text,1)
214 print ((tr("le texte a analyser n'est pas celui d'une commande ou d'un operateur : "), text))
215 self.cr.fatal(tr("le texte a analyser n'est pas celui d'une commande ou \
216 d'un operateur : %s", text))
220 nom_cmd = chercheNom(debut)
221 if nom_cmd == 'erreur !':
222 print((tr("Erreur dans la recherche du nom de la commande : "), debut))
223 args_cmd,toto = chercheArgs(u'(u'+fin)
224 if args_cmd == 'erreur !':
225 print((tr("Erreur dans la recherche des args de la commande :") , debut))
226 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
227 self.liste_commandes.append(cmd)
229 def analyseCommande(self,text):
230 for nom_cmd in l_noms_commandes:
231 liste = re.split(nom_cmd+' *\(u',text,1)
232 if len(liste) == 2 : break
234 print((tr("le texte a analyser n'est pas celui d'une commande connue : \
235 %(v_1)s %(v_2)s", {'v_1': str(l_noms_commandes), 'v_2': text})))
236 self.cr.fatal(tr("le texte a analyser n'est pas celui d'une commande connue : \
237 %(v_1)s %(v_2)s", {'v_1': str(l_noms_commandes), 'v_2': text}))
241 nom_cmd = chercheNom(debut)
242 if nom_cmd == 'erreur !':
243 print(( tr("Erreur dans la recherche du nom de la commande : "), debut))
244 args_cmd,toto = chercheArgs(u'(u'+fin)
245 if args_cmd == 'erreur !':
246 print(( tr("Erreur dans la recherche des args de la commande : "), debut))
248 cmd=COMMANDE_CATA(nom_cmd,args_cmd,self)
249 self.liste_commandes.append(cmd)
251 def analyseTexte(self,texte):
252 self.constrListTxtCmd(texte)
254 self.parent.configure_barre(len(self.liste_textes_commandes))
257 for texte_commande in self.liste_textes_commandes :
259 self.parent.update_barre()
262 self.analyseCommande(texte_commande)
263 self.construitListeDico()
266 f=open(u'U:\\EFICAS\\Accas\\cata.txt','w')
267 for cmd in self.liste_commandes :
268 f.write(cmd.affiche())
271 def construitListeDico(self):
274 for cmd in self.liste_commandes:
281 """ retourne l'objet rapport du catalogue de commande """
284 def analyseCatalogue(parent,nom_cata):
285 cata = CATALOGUE_CATA(parent,nom_cata)
286 cata.analyseTexte(cata.texte_complet)
289 def analyseCatalogueCommande(parent,nom_cata):
290 cata = CATALOGUE_CATA(parent,nom_cata)
291 cata.analyseCommande(cata.texte_complet)
292 cata.construitListeDico()
296 def makeCataPickle(ficCata):
298 Lance l'analyse de l'ordre des mots-cles dans le catalogue dont le nom
299 est passe en argument et sauvegarde ces infos dans le fichier pickle relu
302 ficCata_p = os.path.splitext(ficCata)[0]+'_pickled.py'
303 cata_ordonne = analyseCatalogue(None,ficCata)
304 f = open(ficCata_p,'w+')
305 p = six.moves.cPickle.Pickler(f)
306 p.dump(cata_ordonne.entites)
309 if __name__ == "__main__" :
311 profile.run(u"analyseCatalogue(None,'U:\\EFICAS\\Cata\\cata_saturne.py')")