Salome HOME
pb d accent. on les enleve en français
[tools/eficas.git] / InterfaceQT4 / readercata.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 """
21     Ce module sert a lire un catalogue et a construire
22     un objet CataItem pour Eficas.
23     Il s'appuie sur la classe READERCATA
24 """
25 # Modules Python
26 import time
27 import os,sys,py_compile
28 import traceback
29 import cPickle
30 import re
31 import types
32
33 # Modules Eficas
34 from Noyau.N_CR import CR
35 from Editeur.catadesc import CatalogDescription
36
37 import analyse_catalogue
38 import analyse_catalogue_initial
39 import autre_analyse_cata
40 import uiinfo
41 from monChoixCata import MonChoixCata
42 from Extensions.i18n import tr
43 from Extensions.eficas_exception import EficasException
44
45 from PyQt4 import *
46 from PyQt4.QtGui import *
47 from PyQt4.QtCore import *
48
49
50 class READERCATA:
51
52    def __init__(self,QWParent, appliEficas):
53       self.QWParent=QWParent
54       self.appliEficas=self.QWParent.appliEficas
55       self.VERSION_EFICAS=self.appliEficas.VERSION_EFICAS
56       self.code=self.QWParent.code
57       self.ssCode=self.appliEficas.ssCode
58       self.appliEficas.format_fichier='python'
59       self.mode_nouv_commande=self.appliEficas.CONFIGURATION.mode_nouv_commande
60       self.version_code=self.QWParent.version_code
61       self.version_cata=None
62       self.fic_cata=None
63       self.OpenCata()
64       self.cataitem=None
65       if self.code=="TELEMAC": self.cree_dico_inverse()
66
67    def OpenCata(self):
68       """ 
69           Ouvre le catalogue standard du code courant, cad le catalogue present
70           dans le repertoire Cata 
71       """
72
73       liste_cata_possibles=[]
74       self.Commandes_Ordre_Catalogue=[]
75
76       all_cata_list = []
77       for catalogue in self.appliEficas.CONFIGURATION.catalogues:
78           if isinstance(catalogue, CatalogDescription):
79               all_cata_list.append(catalogue)
80           elif isinstance(catalogue, types.TupleType):
81               all_cata_list.append(CatalogDescription.create_from_tuple(catalogue))
82           else:
83               print "Catalog description cannot be interpreted: ", catalogue
84
85       # This filter is only useful for codes that have subcodes (like MAP).
86       # Otherwise, the "code" attribute of the catalog description can (should) be None.
87       if self.ssCode is None:
88           liste_cata_possibles = all_cata_list
89       else:
90           for catalogue in all_cata_list:
91               if catalogue.code == self.code and catalogue.file_format == self.ssCode:
92                   liste_cata_possibles.append(catalogue)
93
94       if len(liste_cata_possibles)==0:          
95           QMessageBox.critical(self.QWParent, tr("Import du catalogue"),
96                                tr("Pas de catalogue defini pour le code ") + self.code)
97           self.appliEficas.close()
98           if self.appliEficas.salome == 0 :
99              sys.exit(1)
100           return
101
102
103       if self.version_code is not None:
104           # La version a ete fixee
105           for cata in liste_cata_possibles:
106              if self.version_code == cata.identifier:
107                 self.fic_cata = cata.cata_file_path
108                 self.appliEficas.format_fichier = cata.file_format
109                 self.appliEficas.format_fichier_in = cata.file_format_in
110       else:
111           cata_choice_list = []
112           for cata in liste_cata_possibles:
113               if cata.selectable:
114                   if cata.default:
115                       cata_choice_list.insert(0, cata)
116                   else :
117                       cata_choice_list.append(cata)
118           if len(cata_choice_list) == 0:
119               QMessageBox.critical(self.QWParent, tr("Import du catalogue"),
120                                    tr("Aucun catalogue trouve"))
121               self.appliEficas.close()
122               if self.appliEficas.salome == 0 :
123                  sys.exit(1)
124           elif len(cata_choice_list) == 1:
125               self.fic_cata = cata_choice_list[0].cata_file_path
126               self.version_code = cata_choice_list[0].identifier
127               self.appliEficas.format_fichier = cata_choice_list[0].file_format
128               self.appliEficas.format_fichier_in = cata_choice_list[0].file_format_in
129           else:
130               # plusieurs catalogues sont disponibles : il faut demander a l'utilisateur
131               # lequel il veut utiliser ...
132               self.ask_choix_catalogue(cata_choice_list)
133
134       if self.fic_cata == None :
135           if self.appliEficas.salome == 0 :
136              print "Pas de catalogue pour code %s, version %s" %(self.code,self.version_code)
137              sys.exit(1)
138           else :
139              self.appliEficas.close()
140              return
141
142       if self.code == "ASTER" : self.determineMater()
143
144       # import du catalogue
145       self.cata = self.import_cata(self.fic_cata)
146       if not self.cata :          
147           QMessageBox.critical( self.QWParent, tr("Import du catalogue"),tr("Impossible d'importer le catalogue ")+ self.fic_cata)
148           self.appliEficas.close()
149           if self.appliEficas.salome == 0 :
150              sys.exit(1)
151       #
152       # analyse du catalogue (ordre des mots-cles)
153       #
154       # Retrouve_Ordre_Cata_Standard fait une analyse textuelle du catalogue
155       # remplace par Retrouve_Ordre_Cata_Standard_autre qui utilise une numerotation
156       # des mots cles a la creation
157       self.Retrouve_Ordre_Cata_Standard_autre()
158       if self.mode_nouv_commande== "initial" : self.Retrouve_Ordre_Cata_Standard()
159       if hasattr(self.cata, 'Ordre_Des_Commandes') : self.Ordre_Des_Commandes=self.cata.Ordre_Des_Commandes
160       else : self.Ordre_Des_Commandes=None
161       #print self.cata.Ordre_Des_Commandes
162
163       #
164       # analyse des donnees liees l'IHM : UIinfo
165       #
166       uiinfo.traite_UIinfo(self.cata)
167
168       #
169       # traitement des clefs documentaires
170       #
171       if self.code == "ASTER" : self.traite_clefs_documentaires()
172       self.cata=(self.cata,)
173
174       self.titre=self.VERSION_EFICAS+" "+tr( " avec le catalogue ") + os.path.basename(self.fic_cata)
175       if self.appliEficas.top:
176         self.appliEficas.setWindowTitle(self.titre)
177       self.appliEficas.titre=self.titre
178       self.QWParent.titre=self.titre
179
180    def determineMater(self) :
181       # Determinination du repertoire materiau
182       v_codeSansPoint=self.version_code
183       if v_codeSansPoint == None : return 
184       v_codeSansPoint=re.sub("\.","",v_codeSansPoint)
185       chaine="rep_mat_"+v_codeSansPoint
186       if hasattr(self.appliEficas.CONFIGURATION,chaine):
187           a=getattr(self.appliEficas.CONFIGURATION,chaine)
188       else :
189           try :
190              a=self.appliEficas.CONFIGURATION.dRepMat[self.version_code]
191           except :
192              if self.code == "ASTER" :
193                 print "Probleme avec le repertoire materiau"
194              a='.'
195       self.appliEficas.CONFIGURATION.rep_mat=a
196
197    def import_cata(self,cata):
198       """ 
199           Realise l'import du catalogue dont le chemin d'acces est donne par cata
200       """
201       nom_cata = os.path.splitext(os.path.basename(cata))[0]
202       rep_cata = os.path.dirname(cata)
203       sys.path[:0] = [rep_cata]
204       self.appliEficas.listeAEnlever.append(rep_cata)
205
206       
207       if sys.modules.has_key(nom_cata):
208         del sys.modules[nom_cata]
209       for k in sys.modules.keys():
210         if k[0:len(nom_cata)+1] == nom_cata+'.':
211           del sys.modules[k]
212
213       mesScriptsNomFichier='mesScripts_'+self.code.upper()
214       if self.code == "ASTER" :
215          self.appliEficas.rep_scripts=os.path.join(rep_cata,nom_cata)
216          sys.path[:0] = [self.appliEficas.rep_scripts]
217          try :
218              self.appliEficas.mesScripts=__import__(mesScriptsNomFichier)
219          except:
220              pass
221          sys.path=sys.path[1:]
222       else :
223          try :
224             self.appliEficas.mesScripts=__import__(mesScriptsNomFichier)
225          except:
226             pass
227
228       try :
229           o=__import__(nom_cata)
230           return o
231       except Exception,e:
232           traceback.print_exc()
233           return 0
234
235
236
237    def Retrouve_Ordre_Cata_Standard_autre(self):
238       """ 
239           Construit une structure de donnees dans le catalogue qui permet
240           a EFICAS de retrouver l'ordre des mots-cles dans le texte du catalogue.
241           Pour chaque entite du catlogue on cree une liste de nom ordre_mc qui
242           contient le nom des mots cles dans le bon ordre
243       """ 
244       self.cata_ordonne_dico,self.appliEficas.liste_simp_reel=autre_analyse_cata.analyse_catalogue(self.cata)
245       #print self.cata_ordonne_dico,self.appliEficas.liste_simp_reel
246
247    def Retrouve_Ordre_Cata_Standard(self):
248       """ 
249           Retrouve l'ordre des mots-cles dans le catalogue, cad :
250           Attention s appuie sur les commentaires
251       """
252       nom_cata = os.path.splitext(os.path.basename(self.fic_cata))[0]
253       rep_cata = os.path.dirname(self.fic_cata)
254       self.Commandes_Ordre_Catalogue = analyse_catalogue_initial.analyse_catalogue(self.fic_cata)
255       #print self.Commandes_Ordre_Catalogue
256
257    def ask_choix_catalogue(self, cata_choice_list):
258       """
259       Ouvre une fenetre de selection du catalogue dans le cas où plusieurs
260       ont ete definis dans Accas/editeur.ini
261       """      
262       code = getattr(self.appliEficas.CONFIGURATION, "code", None)
263       if code != None : 
264           title=tr("Choix d une version du code ")+str(code)
265       else :
266           title=tr("Choix d une version ")
267     
268       widgetChoix = MonChoixCata(self.appliEficas, [cata.user_name for cata in cata_choice_list], title)
269       ret=widgetChoix.exec_()
270       
271       lab=QString(self.VERSION_EFICAS)+" "
272       lab+=tr(" pour ")
273       lab+=QString(self.code) 
274       lab+=tr(" avec le catalogue ")
275       if ret == QDialog.Accepted:
276           cata = cata_choice_list[widgetChoix.CBChoixCata.currentIndex()]
277           self.version_cata = cata.identifier
278           self.fic_cata = cata.cata_file_path
279           self.version_code = self.version_cata
280           self.appliEficas.format_fichier = cata.file_format
281           self.appliEficas.format_fichier_in = cata.file_format_in
282           lab+=self.version_cata
283           self.appliEficas.setWindowTitle(lab)
284           #qApp.mainWidget().setCaption(lab)
285       else:
286           raise EficasException()
287
288    def traite_clefs_documentaires(self):
289       try:
290         fic_doc='rep_doc_'+str(self.version_code)
291         self.fic_doc=getattr(self.appliEficas.CONFIGURATION,fic_doc )
292         f=open(self.fic_doc)
293       except:
294         print "Pas de fichier associe contenant des clefs documentaires"
295         return
296
297       dict_clef_docu={}
298       for l in f.readlines():
299           clef=l.split(':')[0]
300           deb=l.find(':')+1
301           docu=l[deb:-1]
302           dict_clef_docu[clef]=docu
303       for oper in self.cata.JdC.commandes:
304            if dict_clef_docu.has_key(oper.nom):
305               oper.docu=dict_clef_docu[oper.nom]
306
307
308    def cree_dico_inverse(self):
309         self.dicoInverse={}
310         self.dico={}
311         listeEtapes=self.cata[0].JdC.commandes
312         for e in self.cata[0].JdC.commandes:
313             self.traite_entite(e)
314         #print self.dicoInverse.keys()
315         #for e in self.cata[0].JdC.commandes:
316         #    print "___________", e. nom , '__________________'
317         #    self.cree_rubrique(e,self.dico,0)
318
319         
320    def traite_entite(self,e):
321        boolIn=0
322        for (nomFils, fils) in e.entites.items() :
323           self.traite_entite(fils)
324           boolIn=1
325        if boolIn==0 :
326           liste=[]
327           moi=e
328           while hasattr(moi,'pere') :
329                 liste.append((moi.nom,moi))
330                 moi=moi.pere
331           liste.append((moi.nom,moi))
332           self.dicoInverse[e.nom]=liste
333
334    def cree_rubrique(self,e,dico, niveau):
335        from Accas import A_BLOC
336        decale=niveau*"   "
337        if niveau != 0 :
338            if isinstance(e,A_BLOC.BLOC): print decale, e.condition 
339            else :                           print decale, e. nom  
340        for (nom, fils) in e.entites.items() :
341            if  fils.entites.items() != [] : self.cree_rubrique(fils,dico,niveau+1)
342            else : print (niveau+1)*"   ", nom
343
344         
345           
346               
347