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