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