Salome HOME
*** empty log message ***
[tools/eficas.git] / InterfaceQT4 / readercata.py
1 # -*- coding: utf-8 -*-
2 #            CONFIGURATION MANAGEMENT OF EDF VERSION
3 # ======================================================================
4 # COPYRIGHT (C) 1991 - 2002  EDF R&D                  WWW.CODE-ASTER.ORG
5 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
6 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
7 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
8 # (AT YOUR OPTION) ANY LATER VERSION.
9 #
10 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
11 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
12 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
13 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
14 #
15 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
16 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
17 #    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
18 #
19 #
20 # ======================================================================
21 """
22     Ce module sert a lire un catalogue et a construire
23     un objet CataItem pour Eficas.
24     Il s'appuie sur la classe READERCATA
25 """
26 # Modules Python
27 import time
28 import os,sys,py_compile
29 import traceback
30 import cPickle
31 import re
32
33 # Modules Eficas
34 from Noyau.N_CR import CR
35 from Editeur.utils  import init_rep_cata_dev
36
37 import analyse_catalogue
38 import analyse_catalogue_initial
39 import autre_analyse_cata
40 import uiinfo
41 from monChoixCata import MonChoixCata
42
43 from PyQt4 import *
44 from PyQt4.QtGui import *
45 from PyQt4.QtCore import *
46
47 VERSION_EFICAS="Eficas V1.16"
48
49 class READERCATA:
50
51    def __init__(self,QWParent, appliEficas):
52       self.QWParent=QWParent
53       self.appliEficas=self.QWParent.appliEficas
54       self.code=self.QWParent.code
55       self.appliEficas.format_fichier='python'
56       if hasattr(self.appliEficas,'mode_nouv_commande'):
57          self.mode_nouv_commande=self.appliEficas.mode_nouv_commande
58       else :
59          self.mode_nouv_commande='alpha'
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
66    def OpenCata(self):
67       """ 
68           Ouvre le catalogue standard du code courant, cad le catalogue présent
69           dans le répertoire Cata 
70       """
71
72       liste_cata_possibles=[]
73       for catalogue in self.appliEficas.CONFIGURATION.catalogues:
74           if catalogue[0] == self.code :
75              liste_cata_possibles.append(catalogue)
76
77       if len(liste_cata_possibles)==0:          
78           QMessageBox.critical( self.QWParent, "Import du catalogue","Pas de catalogue defini pour le code %s" % self.code)
79           self.appliEficas.close()
80           sys.exit(1)
81
82       if self.version_code is not None:
83           # La version a ete fixee
84           for cata in liste_cata_possibles:
85              if self.version_code == cata[1]:
86                 self.fic_cata = cata[2]
87                 self.appliEficas.format_fichier=cata[3]
88       elif len(liste_cata_possibles)==1:
89           self.fic_cata = liste_cata_possibles[0][2]
90           self.version_code = liste_cata_possibles[0][1]
91           self.appliEficas.format_fichier=liste_cata_possibles[0][3] 
92           lab=QString("Eficas V1.") 
93           lab+=QString(VERSION_EFICAS) 
94           lab+=QString(" pour ")
95           lab+=QString(self.code) 
96           lab+=QString(" avec le catalogue ")
97           lab+=self.version_code
98           try :
99           # souci pour les includes et sans Ihm
100               self.appliEficas.setWindowTitle(lab)
101           except :
102               pass
103       else:
104           # plusieurs catalogues sont disponibles : il faut demander a l'utilisateur
105           # lequel il veut utiliser ...
106           self.ask_choix_catalogue()
107
108       if self.fic_cata == None :
109           print "Pas de catalogue pour code %s, version %s" %(self.code,self.version_code)
110           sys.exit(0)
111
112       self.determineMater()
113
114
115       # détermination de fic_cata_c et fic_cata_p
116       self.fic_cata_c = self.fic_cata + 'c'
117       self.fic_cata_p = os.path.splitext(self.fic_cata)[0]+'_pickled.py'
118
119       # import du catalogue
120       self.cata = self.import_cata(self.fic_cata)
121       if not self.cata :          
122           QMessageBox.critical( self.QWParent, "Import du catalogue","Impossible d'importer le catalogue %s" %self.fic_cata)
123           self.appliEficas.close()
124           sys.exit(1)
125       #
126       # analyse du catalogue (ordre des mots-clés)
127       #
128       # Retrouve_Ordre_Cata_Standard fait une analyse textuelle du catalogue
129       # remplacé par Retrouve_Ordre_Cata_Standard_autre qui utilise une numerotation
130       # des mots clés a la création
131       self.Retrouve_Ordre_Cata_Standard_autre()
132       if self.mode_nouv_commande== "initial" :
133          self.Retrouve_Ordre_Cata_Standard()
134       else:
135          self.Commandes_Ordre_Catalogue=[]
136
137       #
138       # analyse des données liées a  l'IHM : UIinfo
139       #
140       uiinfo.traite_UIinfo(self.cata)
141
142       #
143       # traitement des clefs documentaires
144       #
145       self.traite_clefs_documentaires()
146       self.cata=(self.cata,)
147       titre=VERSION_EFICAS + " avec le catalogue " + os.path.basename(self.fic_cata)
148       if self.appliEficas.top:
149         self.appliEficas.setWindowTitle(titre)
150       self.appliEficas.titre=titre
151
152    def determineMater(self) :
153       # Determinination du repertoire materiau
154       v_codeSansPoint=self.version_code
155       v_codeSansPoint=re.sub("\.","",v_codeSansPoint)
156       chaine="rep_mat_"+v_codeSansPoint
157       if hasattr(self.appliEficas.CONFIGURATION,chaine):
158           a=getattr(self.appliEficas.CONFIGURATION,chaine)
159       else :
160           try :
161              a=self.appliEficas.CONFIGURATION.dRepMat[self.version_code]
162           except :
163              if self.code == "ASTER" :
164                 print "Probleme avec le repertoire materiau"
165              a='.'
166       self.appliEficas.CONFIGURATION.rep_mat=a
167
168    def import_cata(self,cata):
169       """ 
170           Réalise l'import du catalogue dont le chemin d'acces est donné par cata
171       """
172       nom_cata = os.path.splitext(os.path.basename(cata))[0]
173       rep_cata = os.path.dirname(cata)
174       sys.path[:0] = [rep_cata]
175       try :
176           o=__import__(nom_cata)
177           return o
178       except Exception,e:
179           traceback.print_exc()
180           return 0
181
182    def Retrouve_Ordre_Cata_Standard_autre(self):
183       """ 
184           Construit une structure de données dans le catalogue qui permet
185           a  EFICAS de retrouver l'ordre des mots-clés dans le texte du catalogue.
186           Pour chaque entité du catlogue on crée une liste de nom ordre_mc qui
187           contient le nom des mots clés dans le bon ordre
188       """ 
189       self.cata_ordonne_dico,self.appliEficas.liste_simp_reel=autre_analyse_cata.analyse_catalogue(self.cata)
190
191    def Retrouve_Ordre_Cata_Standard(self):
192       """ 
193           Retrouve l'ordre des mots-clés dans le catalogue, cad :
194           Attention s appuie sur les commentaires
195       """
196       nom_cata = os.path.splitext(os.path.basename(self.fic_cata))[0]
197       rep_cata = os.path.dirname(self.fic_cata)
198       self.Commandes_Ordre_Catalogue = analyse_catalogue_initial.analyse_catalogue(self.fic_cata)
199
200    def ask_choix_catalogue(self):
201       """
202       Ouvre une fenetre de sélection du catalogue dans le cas oa¹ plusieurs
203       ont été définis dans Accas/editeur.ini
204       """      
205       # construction du dictionnaire et de la liste des catalogues
206       self.dico_catalogues = {}
207       defaut = None
208       for catalogue in self.appliEficas.CONFIGURATION.catalogues:
209           if catalogue[0] == self.code :
210               self.dico_catalogues[catalogue[1]] = catalogue
211               if len(catalogue) == 5 :
212                   if catalogue[4]=='defaut' : defaut = catalogue[1]
213       liste_choix = self.dico_catalogues.keys()
214       liste_choix.sort()
215
216       lab=QString(VERSION_EFICAS)
217       lab+=QString(" pour ")
218       lab+=QString(self.code) 
219       lab+=QString(" avec le catalogue ")
220
221       # teste si plusieurs catalogues ou non
222       if len(liste_choix) == 0:          
223           QMessageBox.critical( self.QWParent, "", "Aucun catalogue déclaré pour %s" %self.code)
224           self.appliEficas.close()
225           sys.exit(1)
226           
227       # création d'une boite de dialogue modale
228       widgetChoix=MonChoixCata(liste_choix,self, self.appliEficas, "", True )
229       ret=widgetChoix.exec_()
230       
231       lab=QString(VERSION_EFICAS)
232       lab+=QString(" pour ")
233       lab+=QString(self.code) 
234       lab+=QString(" avec le catalogue ")
235       if ret == QDialog.Accepted:
236           self.version_cata=str(self.version_cata)
237           self.fic_cata = self.dico_catalogues[self.version_cata][2]
238           self.version_code = self.version_cata
239           self.appliEficas.format_fichier = self.dico_catalogues[self.version_cata][3]
240           lab+=self.version_cata
241           self.appliEficas.setWindowTitle(lab)
242           #qApp.mainWidget().setCaption(lab)
243       else :
244           sys.exit(0)
245
246
247
248    def traite_clefs_documentaires(self):
249       try:
250         self.fic_cata_clef=os.path.splitext(self.fic_cata_c)[0]+'_clefs_docu'
251         f=open(self.fic_cata_clef)
252       except:
253         #print "Pas de fichier associé contenant des clefs documentaires"
254         return
255
256       dict_clef_docu={}
257       for l in f.readlines():
258           clef=l.split(':')[0]
259           docu=l.split(':')[1]
260           docu=docu[0:-1]
261           dict_clef_docu[clef]=docu
262       for oper in self.cata.JdC.commandes:
263            if dict_clef_docu.has_key(oper.nom):
264               oper.docu=dict_clef_docu[oper.nom]