Salome HOME
modifs pour Telemac
[tools/eficas.git] / InterfaceQT4 / readercata.py
index faa27690b999415537c763f55d8a66b22f4e66b9..f788c8b6e386a316563dc371528dd55d23215aec 100644 (file)
 # -*- coding: utf-8 -*-
-#            CONFIGURATION MANAGEMENT OF EDF VERSION
-# ======================================================================
-# COPYRIGHT (C) 1991 - 2002  EDF R&D                  WWW.CODE-ASTER.ORG
-# THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
-# IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
-# THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
-# (AT YOUR OPTION) ANY LATER VERSION.
+# Copyright (C) 2007-2017   EDF R&D
 #
-# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
-# WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
-# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
-# GENERAL PUBLIC LICENSE FOR MORE DETAILS.
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License.
 #
-# YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
-# ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
-#    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
 #
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+#
+# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
-# ======================================================================
 """
     Ce module sert a lire un catalogue et a construire
     un objet CataItem pour Eficas.
     Il s'appuie sur la classe READERCATA
 """
 # Modules Python
+from __future__ import absolute_import
+from __future__ import print_function
+try :
+   from builtins import str
+   from builtins import object
+except : pass
+
 import time
 import os,sys,py_compile
 import traceback
-import cPickle
+import six.moves.cPickle
 import re
 import types
 
 # Modules Eficas
 from Noyau.N_CR import CR
 from Editeur.catadesc import CatalogDescription
-from Editeur.utils  import init_rep_cata_dev
 
 import analyse_catalogue
 import analyse_catalogue_initial
 import autre_analyse_cata
 import uiinfo
-from monChoixCata import MonChoixCata
+from InterfaceQT4.monChoixCata import MonChoixCata
+from Extensions.i18n import tr
+from Extensions.eficas_exception import EficasException
 
-from PyQt4 import *
-from PyQt4.QtGui import *
-from PyQt4.QtCore import *
+from PyQt5.QtWidgets import QMessageBox, QApplication, QDialog
 
+#-------------------------------
+class ReaderCataCommun(object):
+#-------------------------------
 
-class READERCATA:
-
-   def __init__(self,QWParent, appliEficas):
-      self.QWParent=QWParent
-      self.appliEficas=self.QWParent.appliEficas
-      self.VERSION_EFICAS=self.appliEficas.VERSION_EFICAS
-      self.code=self.QWParent.code
-      self.ssCode=self.appliEficas.ssCode
-      self.appliEficas.format_fichier='python'
-      if hasattr(self.appliEficas,'mode_nouv_commande'):
-        self.mode_nouv_commande=self.appliEficas.mode_nouv_commande
+   def askChoixCatalogue(self, cata_choice_list):
+   # ____________________________________________
+      """
+      Ouvre une fenetre de selection du catalogue dans le cas où plusieurs
+      ont ete definis dans Accas/editeur.ini
+      """
+      code = getattr(self.appliEficas.maConfiguration, "code", None)
+      if code != None :
+          title=tr("Choix d une version du code ")+str(code)
       else :
-         self.mode_nouv_commande='alpha'
-      self.version_code=self.QWParent.version_code
-      self.version_cata=None
-      self.fic_cata=None
-      self.OpenCata()
-      self.cataitem=None
+          title=tr("Choix d une version ")
 
-   def OpenCata(self):
-      """ 
-          Ouvre le catalogue standard du code courant, cad le catalogue présent
-          dans le répertoire Cata 
-      """
+      widgetChoix = MonChoixCata(self.appliEficas, [cata.user_name for cata in cata_choice_list], title)
+      ret=widgetChoix.exec_()
+
+
+      lab=str(self.VERSION_EFICAS)+" "
+      lab+=tr(" pour ")
+      lab+=str(self.code)
+      lab+=tr(" avec le catalogue ")
+      if ret == QDialog.Accepted:
+          cata = cata_choice_list[widgetChoix.CBChoixCata.currentIndex()]
+          self.fic_cata = cata.cata_file_path
+          self.versionCode = cata.identifier
+          self.appliEficas.format_fichier = cata.file_format
+          self.appliEficas.format_fichier_in = cata.file_format_in
+          lab+=self.versionCode
+          self.appliEficas.setWindowTitle(lab)
+          #qApp.mainWidget().setCaption(lab)
+          widgetChoix.close()
+      else:
+          widgetChoix.close()
+          raise EficasException()
+
+   def choisitCata(self):
+   # ____________________
 
       liste_cata_possibles=[]
       self.Commandes_Ordre_Catalogue=[]
 
       all_cata_list = []
-      for catalogue in self.appliEficas.CONFIGURATION.catalogues:
-          if isinstance(catalogue, CatalogDescription):
-              all_cata_list.append(catalogue)
-          elif isinstance(catalogue, types.TupleType):
-              all_cata_list.append(CatalogDescription.create_from_tuple(catalogue))
-          else:
-              print "Catalog description cannot be interpreted: ", catalogue
+      for catalogue in self.appliEficas.maConfiguration.catalogues:
+          if isinstance(catalogue, CatalogDescription): all_cata_list.append(catalogue)
+          elif isinstance(catalogue, tuple)           : all_cata_list.append(CatalogDescription.create_from_tuple(catalogue))
+          else: print(("Catalog description cannot be interpreted: ", catalogue))
 
       # This filter is only useful for codes that have subcodes (like MAP).
       # Otherwise, the "code" attribute of the catalog description can (should) be None.
-      if self.ssCode is None:
-          liste_cata_possibles = all_cata_list
+      if self.ssCode is None: liste_cata_possibles = all_cata_list
       else:
           for catalogue in all_cata_list:
-              #print catalogue.code
-              #print catalogue.file_format
-              if catalogue.code == self.code and catalogue.file_format == self.ssCode:
-                  liste_cata_possibles.append(catalogue)
-
-      #print "___________"
-      #print self.ssCode
-      #print self.code
-      if len(liste_cata_possibles)==0:          
-          QMessageBox.critical(self.QWParent, "Import du catalogue",
-                               u"Pas de catalogue défini pour le code %s" % self.code)
+              if catalogue.code == self.code and catalogue.file_format == self.ssCode: liste_cata_possibles.append(catalogue)
+
+      if len(liste_cata_possibles)==0:
+          QMessageBox.critical(self.QWParent, tr("Import du catalogue"),
+                               tr("Pas de catalogue defini pour le code ") + self.code)
           self.appliEficas.close()
-          if self.appliEficas.salome == 0 :
-             sys.exit(1)
+          if self.appliEficas.salome == 0 : sys.exit(1)
           return
 
 
-      if self.version_code is not None:
+      if self.versionCode is not None:
           # La version a ete fixee
           for cata in liste_cata_possibles:
-             if self.version_code == cata.identifier:
+             if self.versionCode == cata.identifier:
                 self.fic_cata = cata.cata_file_path
                 self.appliEficas.format_fichier = cata.file_format
                 self.appliEficas.format_fichier_in = cata.file_format_in
@@ -118,188 +127,243 @@ class READERCATA:
           cata_choice_list = []
           for cata in liste_cata_possibles:
               if cata.selectable:
-                  if cata.default:
-                      cata_choice_list.insert(0, cata)
-                  else :
-                      cata_choice_list.append(cata)
+                  if cata.default : cata_choice_list.insert(0, cata)
+                  else            : cata_choice_list.append(cata)
+
           if len(cata_choice_list) == 0:
-              QMessageBox.critical(self.QWParent, "Import du catalogue",
-                                   QString.fromUtf8("Aucun catalogue trouvé"))
+              QMessageBox.critical(self.QWParent, tr("Import du catalogue"),
+                                   tr("Aucun catalogue trouve"))
               self.appliEficas.close()
-              if self.appliEficas.salome == 0 :
-                 sys.exit(1)
+              if self.appliEficas.salome == 0 : sys.exit(1)
+
           elif len(cata_choice_list) == 1:
               self.fic_cata = cata_choice_list[0].cata_file_path
-              self.version_code = cata_choice_list[0].identifier
+              self.versionCode = cata_choice_list[0].identifier
               self.appliEficas.format_fichier = cata_choice_list[0].file_format
               self.appliEficas.format_fichier_in = cata_choice_list[0].file_format_in
-              lab=QString("Eficas ") 
-              lab+=QString(self.VERSION_EFICAS) 
-              lab+=QString(" pour ")
-              lab+=QString(self.code) 
-              lab+=QString(" avec le catalogue ")
-              lab+=self.version_code
-              try :
-              # souci pour les includes et sans Ihm
-                  self.appliEficas.setWindowTitle(lab)
-              except :
-                  pass
+
           else:
               # plusieurs catalogues sont disponibles : il faut demander a l'utilisateur
               # lequel il veut utiliser ...
-              self.ask_choix_catalogue(cata_choice_list)
-              # On est dans Salome et il faut sortir proprement
+              self.askChoixCatalogue(cata_choice_list)
+              self.demandeCatalogue=True
 
       if self.fic_cata == None :
           if self.appliEficas.salome == 0 :
-             print "Pas de catalogue pour code %s, version %s" %(self.code,self.version_code)
+             print(("Pas de catalogue pour code %s, version %s" %(self.code,self.versionCode)))
              sys.exit(1)
           else :
              self.appliEficas.close()
              return
 
-      self.determineMater()
-      # détermination de fic_cata_c et fic_cata_p
-      self.fic_cata_c = self.fic_cata + 'c'
-      self.fic_cata_p = os.path.splitext(self.fic_cata)[0]+'_pickled.py'
 
+#------------------------------------
+class ReaderCata (ReaderCataCommun):
+#------------------------------------
+
+   def __init__(self,QWParent, appliEficas):
+   # ______________________________________
+
+      self.QWParent=QWParent
+      self.appliEficas=self.QWParent.appliEficas
+      self.VERSION_EFICAS=self.appliEficas.VERSION_EFICAS
+      self.demandeCatalogue=False
+      self.code=self.appliEficas.code
+      self.ssCode=self.appliEficas.ssCode
+      self.appliEficas.format_fichier='python'
+      self.versionCode=self.appliEficas.versionCode
+      self.fic_cata=None
+      self.openCata()
+      self.traiteIcones()
+      self.cataitem=None
+      self.creeDicoInverse()
+      if self.code=="TELEMAC": self.creeDicoCasToCata()
+
+
+
+   def openCata(self):
+      """
+          Ouvre le catalogue standard du code courant, cad le catalogue present
+          dans le repertoire Cata
+      """
       # import du catalogue
-      self.cata = self.import_cata(self.fic_cata)
-      if not self.cata :          
-          QMessageBox.critical( self.QWParent, "Import du catalogue","Impossible d'importer le catalogue %s" %self.fic_cata)
-         self.appliEficas.close()
+      self.choisitCata()
+
+      if self.appliEficas.maConfiguration.withXSD :
+         try :
+           #import raw.Telemac2d as modeleMetier
+           #import raw.cata_genere_fact as modeleMetier
+           import raw.cata_map_genere as modeleMetier
+           #import raw.cata_bloc as modeleMetier
+           print ('import Test ad modeleMetier')
+         except :
+           modeleMetier = None
+      else :
+           modeleMetier = None
+
+      self.cata = self.importCata(self.fic_cata)
+      self.cata.modeleMetier = modeleMetier
+      if not self.cata :
+          QMessageBox.critical( self.QWParent, tr("Import du catalogue"),tr("Impossible d'importer le catalogue ")+ self.fic_cata)
+          self.appliEficas.close()
           if self.appliEficas.salome == 0 :
              sys.exit(1)
       #
-      # analyse du catalogue (ordre des mots-clés)
+      # analyse du catalogue (ordre des mots-cles)
       #
-      # Retrouve_Ordre_Cata_Standard fait une analyse textuelle du catalogue
-      # remplacé par Retrouve_Ordre_Cata_Standard_autre qui utilise une numerotation
-      # des mots clés a la création
-      self.Retrouve_Ordre_Cata_Standard_autre()
-      if self.mode_nouv_commande== "initial" :
-         self.Retrouve_Ordre_Cata_Standard()
+      # retrouveOrdreCataStandard fait une analyse textuelle du catalogue
+      # remplace par retrouveOrdreCataStandardAutre qui utilise une numerotation
+      # des mots cles a la creation
+      #print (self.cata)
+      #print (dir(self.cata))
+      self.retrouveOrdreCataStandardAutre()
+      if self.appliEficas.maConfiguration.modeNouvCommande == "initial" : self.retrouveOrdreCataStandard()
+      if hasattr(self.cata, 'Ordre_Des_Commandes') : self.Ordre_Des_Commandes=self.cata.Ordre_Des_Commandes
+      else : self.Ordre_Des_Commandes=None
+
+      if hasattr(self.cata, 'Classement_Commandes_Ds_Arbre') :
+             self.Classement_Commandes_Ds_Arbre=self.cata.Classement_Commandes_Ds_Arbre
+      else : self.Classement_Commandes_Ds_Arbre=()
+      if hasattr(self.cata,'enum'):
+         try :
+           _temp= __import__(self.cata.enum,globals(), locals(), ['DicoEnumCasFrToEnumCasEn', 'TelemacdicoEn'], 0)
+           self.DicoEnumCasFrToEnumCasEn = _temp.DicoEnumCasFrToEnumCasEn
+           self.TelemacdicoEn = _temp.TelemacdicoEn
+         except : pass
+
+      #print self.cata.Ordre_Des_Commandes
 
       #
-      # analyse des données liées a  l'IHM : UIinfo
+      # analyse des donnees liees l'IHM : UIinfo
       #
       uiinfo.traite_UIinfo(self.cata)
 
       #
       # traitement des clefs documentaires
       #
-      self.traite_clefs_documentaires()
-      self.cata=(self.cata,)
-      titre=self.VERSION_EFICAS + " avec le catalogue " + os.path.basename(self.fic_cata)
-      if self.appliEficas.top:
-        self.appliEficas.setWindowTitle(titre)
-      self.appliEficas.titre=titre
-      self.QWParent.titre=titre
-
-   def determineMater(self) :
-      # Determinination du repertoire materiau
-      v_codeSansPoint=self.version_code
-      if v_codeSansPoint == None : return 
-      v_codeSansPoint=re.sub("\.","",v_codeSansPoint)
-      chaine="rep_mat_"+v_codeSansPoint
-      if hasattr(self.appliEficas.CONFIGURATION,chaine):
-          a=getattr(self.appliEficas.CONFIGURATION,chaine)
-      else :
-          try :
-             a=self.appliEficas.CONFIGURATION.dRepMat[self.version_code]
-          except :
-             if self.code == "ASTER" :
-                print "Probleme avec le repertoire materiau"
-             a='.'
-      self.appliEficas.CONFIGURATION.rep_mat=a
-
-   def import_cata(self,cata):
-      """ 
-          Réalise l'import du catalogue dont le chemin d'acces est donné par cata
+
+      self.titre=self.VERSION_EFICAS+" "+tr( " avec le catalogue ") + os.path.basename(self.fic_cata)
+      if self.appliEficas.ssIhm == False : self.appliEficas.setWindowTitle(self.titre)
+      self.appliEficas.titre=self.titre
+      self.QWParent.titre=self.titre
+
+
+   def importCata(self,cata):
+      """
+          Realise l'import du catalogue dont le chemin d'acces est donne par cata
       """
       nom_cata = os.path.splitext(os.path.basename(cata))[0]
       rep_cata = os.path.dirname(cata)
       sys.path[:0] = [rep_cata]
-      
-      if sys.modules.has_key(nom_cata):
+      self.appliEficas.listeAEnlever.append(rep_cata)
+
+
+      if nom_cata in list(sys.modules.keys()) :
         del sys.modules[nom_cata]
-      for k in sys.modules.keys():
+      for k in sys.modules:
         if k[0:len(nom_cata)+1] == nom_cata+'.':
           del sys.modules[k]
 
+      mesScriptsNomFichier='mesScripts_'+self.code.upper()
+      try :
+          self.appliEficas.mesScripts[self.code]=__import__(mesScriptsNomFichier)
+      except:
+          pass
+
+      #if 1 :
       try :
           o=__import__(nom_cata)
           return o
-      except Exception,e:
+      except Exception as e:
           traceback.print_exc()
           return 0
 
-   def Retrouve_Ordre_Cata_Standard_autre(self):
-      """ 
-          Construit une structure de données dans le catalogue qui permet
-          a  EFICAS de retrouver l'ordre des mots-clés dans le texte du catalogue.
-          Pour chaque entité du catlogue on crée une liste de nom ordre_mc qui
-          contient le nom des mots clés dans le bon ordre
-      """ 
-      self.cata_ordonne_dico,self.appliEficas.liste_simp_reel=autre_analyse_cata.analyse_catalogue(self.cata)
-
-   def Retrouve_Ordre_Cata_Standard(self):
-      """ 
-          Retrouve l'ordre des mots-clés dans le catalogue, cad :
+
+
+   def retrouveOrdreCataStandardAutre(self):
+      """
+          Construit une structure de donnees dans le catalogue qui permet
+          a EFICAS de retrouver l'ordre des mots-cles dans le texte du catalogue.
+          Pour chaque entite du catlogue on cree une liste de nom ordre_mc qui
+          contient le nom des mots cles dans le bon ordre
+      """
+      self.cata_ordonne_dico, self.appliEficas.liste_simp_reel=autre_analyse_cata.analyseCatalogue(self.cata)
+      #self.appliEficas.liste_simp_reel = ()
+      #self.cata_ordonne_dico = {}
+
+   def retrouveOrdreCataStandard(self):
+      """
+          Retrouve l'ordre des mots-cles dans le catalogue, cad :
           Attention s appuie sur les commentaires
       """
       nom_cata = os.path.splitext(os.path.basename(self.fic_cata))[0]
       rep_cata = os.path.dirname(self.fic_cata)
-      self.Commandes_Ordre_Catalogue = analyse_catalogue_initial.analyse_catalogue(self.fic_cata)
+      self.Commandes_Ordre_Catalogue = analyse_catalogue_initial.analyseCatalogue(self.fic_cata)
+      #print self.Commandes_Ordre_Catalogue
 
-   def ask_choix_catalogue(self, cata_choice_list):
-      """
-      Ouvre une fenetre de sélection du catalogue dans le cas où plusieurs
-      ont été définis dans Accas/editeur.ini
-      """      
-      code = getattr(self.appliEficas.CONFIGURATION, "code", None)
-      if code != None : 
-          title="Choix d une version du code "+str(code)
-      else :
-          title="Choix d une version "
-    
-      widgetChoix = MonChoixCata(self.appliEficas, [cata.user_name for cata in cata_choice_list], title)
-      ret=widgetChoix.exec_()
-      
-      lab=QString(self.VERSION_EFICAS)
-      lab+=QString(" pour ")
-      lab+=QString(self.code) 
-      lab+=QString(" avec le catalogue ")
-      if ret == QDialog.Accepted:
-          cata = cata_choice_list[widgetChoix.CBChoixCata.currentIndex()]
-          self.version_cata = cata.identifier
-          self.fic_cata = cata.cata_file_path
-          self.version_code = self.version_cata
-          self.appliEficas.format_fichier = cata.file_format
-          self.appliEficas.format_fichier_in = cata.file_format_in
-          lab+=self.version_cata
-          self.appliEficas.setWindowTitle(lab)
-          #qApp.mainWidget().setCaption(lab)
-      else:
-          self.appliEficas.close()
-          if self.appliEficas.salome == 0 :
-             sys.exit(0)
-
-   def traite_clefs_documentaires(self):
+   def traiteIcones(self):
+      if self.appliEficas.maConfiguration.ficIcones==None : return
       try:
-        self.fic_cata_clef=os.path.splitext(self.fic_cata_c)[0]+'_clefs_docu'
-        #print self.fic_cata_clef
-        f=open(self.fic_cata_clef)
+        ficIcones=self.appliEficas.maConfiguration.ficIcones
+        fichierIcones = __import__(ficIcones, globals(), locals(), [], -1)
+        self.appliEficas.maConfiguration.dicoIcones=fichierIcones.dicoDesIcones.dicoIcones
+        self.appliEficas.maConfiguration.dicoImages=fichierIcones.dicoDesIcones.dicoImages
       except:
-        #print "Pas de fichier associe contenant des clefs documentaires"
-        return
-
-      dict_clef_docu={}
-      for l in f.readlines():
-          clef=l.split(':')[0]
-          docu=l.split(':')[1]
-          docu=docu[0:-1]
-          dict_clef_docu[clef]=docu
-      for oper in self.cata.JdC.commandes:
-           if dict_clef_docu.has_key(oper.nom):
-              oper.docu=dict_clef_docu[oper.nom]
+        print ("Pas de fichier associe contenant des liens sur les icones ")
+        self.appliEficas.maConfiguration.dicoIcones={}
+
+
+
+   def creeDicoInverse(self):
+        self.dicoInverse={}
+        self.dicoMC={}
+        listeEtapes=self.cata.JdC.commandes
+        for e in self.cata.JdC.commandes:
+            self.traiteEntite(e)
+
+
+   def creeDicoCasToCata(self):
+      if hasattr(self.cata,'dicoCasEn'):
+        _temp= __import__(self.cata.dicoCasEn,globals(), locals(), ['DicoCasEnToCata'], 0)
+        if self.appliEficas.langue=="ang" :
+           self.dicoCasToCata=_temp.dicoCasEnToCata
+        else :
+           self.dicoCasToCata=_temp.dicoCasFrToCata
+
+
+
+   def traiteEntite(self,e):
+       boolIn=0
+       for (nomFils, fils) in list(e.entites.items()) :
+          self.dicoMC[nomFils]=fils
+          self.traiteEntite(fils)
+          boolIn=1
+       if boolIn==0 :
+          liste=[]
+          moi=e
+          while hasattr(moi,'pere') :
+                liste.append((moi.nom,moi))
+                moi=moi.pere
+          liste.append((moi.nom,moi))
+          self.dicoInverse[e.nom]=liste
+          self.dicoInverse[tr(e.nom)]=liste
+
+   def creeRubrique(self,e,dico, niveau):
+       from Accas import A_BLOC
+       decale=niveau*"   "
+       #if niveau != 0 :
+       #    if isinstance(e,A_BLOC.BLOC): print decale, e.condition
+       #    else :                           print decale, e. nom
+       for (nom, fils) in list(e.entites.items()) :
+           if  list(fils.entites.items()) != [] : self.creeRubrique(fils,dico,niveau+1)
+           #else : print (niveau+1)*"   ", nom
+
+
+   def dumpToXsdEficas(self):
+       # Pas sur qu on ait jamais besoin de cela
+       pass
+       #from Efi2Xsd import readerEfficas
+       #newSchema=   xml = open('Cata_MED_FAM.xml').read()
+       #SchemaMed = efficas.CreateFromDocument(xml)
+       #SchemaMed.alimenteCata(self.cata)
+