1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013 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
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
26 from __future__ import absolute_import
27 from __future__ import print_function
29 from builtins import str
30 from builtins import object
34 import os,sys,py_compile
36 import six.moves.cPickle
41 from Noyau.N_CR import CR
42 from Editeur.catadesc import CatalogDescription
44 import analyse_catalogue
45 import analyse_catalogue_initial
46 import autre_analyse_cata
48 from .monChoixCata import MonChoixCata
49 from Extensions.i18n import tr
50 from Extensions.eficas_exception import EficasException
52 from PyQt5.QtWidgets import QMessageBox, QApplication, QDialog
56 class READERCATA(object):
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
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]
82 Ouvre le catalogue standard du code courant, cad le catalogue present
83 dans le repertoire Cata
86 liste_cata_possibles=[]
87 self.Commandes_Ordre_Catalogue=[]
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))
96 print(("Catalog description cannot be interpreted: ", catalogue))
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
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)
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 :
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
124 cata_choice_list = []
125 for cata in liste_cata_possibles:
128 cata_choice_list.insert(0, cata)
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 :
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
143 # plusieurs catalogues sont disponibles : il faut demander a l'utilisateur
144 # lequel il veut utiliser ...
145 self.ask_choix_catalogue(cata_choice_list)
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)))
152 self.appliEficas.close()
155 if self.code == "ASTER" : self.determineMater()
157 # import du catalogue
158 self.cata = self.import_cata(self.fic_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 :
165 # analyse du catalogue (ordre des mots-cles)
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
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
181 # analyse des donnees liees l'IHM : UIinfo
183 uiinfo.traite_UIinfo(self.cata)
186 # traitement des clefs documentaires
188 if self.code == "ASTER" : self.traite_clefs_documentaires()
189 self.cata=(self.cata,)
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
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)
207 a=self.appliEficas.CONFIGURATION.dRepMat[self.version_code]
209 if self.code == "ASTER" :
210 print ("Probleme avec le repertoire materiau")
212 self.appliEficas.CONFIGURATION.rep_mat=a
214 def import_cata(self,cata):
216 Realise l'import du catalogue dont le chemin d'acces est donne par cata
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)
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+'.':
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]
235 self.appliEficas.mesScripts[self.code]=__import__(mesScriptsNomFichier)
238 sys.path=sys.path[1:]
241 self.appliEficas.mesScripts[self.code]=__import__(mesScriptsNomFichier)
246 o=__import__(nom_cata)
248 except Exception as e:
249 traceback.print_exc()
254 def Retrouve_Ordre_Cata_Standard_autre(self):
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
261 self.cata_ordonne_dico,self.appliEficas.liste_simp_reel=autre_analyse_cata.analyse_catalogue(self.cata)
263 def Retrouve_Ordre_Cata_Standard(self):
265 Retrouve l'ordre des mots-cles dans le catalogue, cad :
266 Attention s appuie sur les commentaires
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
273 def ask_choix_catalogue(self, cata_choice_list):
275 Ouvre une fenetre de selection du catalogue dans le cas où plusieurs
276 ont ete definis dans Accas/editeur.ini
278 code = getattr(self.appliEficas.CONFIGURATION, "code", None)
280 title=tr("Choix d une version du code ")+str(code)
282 title=tr("Choix d une version ")
284 widgetChoix = MonChoixCata(self.appliEficas, [cata.user_name for cata in cata_choice_list], title)
285 ret=widgetChoix.exec_()
287 lab=str(self.VERSION_EFICAS)+" "
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)
302 raise EficasException()
305 def traite_clefs_documentaires(self):
307 fic_doc='rep_doc_'+str(self.version_code)
308 self.fic_doc=getattr(self.appliEficas.CONFIGURATION,fic_doc )
311 print ("Pas de fichier associe contenant des clefs documentaires")
315 for l in f.readlines():
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]
324 def cree_dico_inverse(self):
327 listeEtapes=self.cata[0].JdC.commandes
328 for e in self.cata[0].JdC.commandes:
329 self.traite_entite(e)
331 #self.dicoFrancaisAnglais={}
332 #self.dicoAnglaisFrancais={}
333 #for k in self.dicoInverse:
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
343 def cree_dico_CasToCata(self):
344 if self.appliEficas.langue=="ang" :
345 from dicoCasEnToCata import dicoCasEnToCata as dicoCasToCata
347 from dicoCasFrToCata import dicoCasFrToCata as dicoCasToCata
348 self.dicoCasToCata=dicoCasToCata
353 def traite_entite(self,e):
355 for (nomFils, fils) in list(e.entites.items()) :
356 self.dicoMC[nomFils]=fils
357 self.traite_entite(fils)
362 while hasattr(moi,'pere') :
363 liste.append((moi.nom,moi))
365 liste.append((moi.nom,moi))
366 self.dicoInverse[e.nom]=liste
367 self.dicoInverse[tr(e.nom)]=liste
369 def cree_rubrique(self,e,dico, niveau):
370 from Accas import A_BLOC
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