Salome HOME
pb de check box
[tools/eficas.git] / convert / convert_asterv5.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 contient le plugin convertisseur de fichier
22     au format asterv5 pour EFICAS.
23
24     Un plugin convertisseur doit fournir deux attributs de classe :
25     extensions et formats et deux méthodes : readfile,convert.
26
27     L'attribut de classe extensions est une liste d'extensions
28     de fichiers préconisées pour ce type de format. Cette information
29     est seulement indicative.
30
31     L'attribut de classe formats est une liste de formats de sortie
32     supportés par le convertisseur. Les formats possibles sont :
33     eval, dict ou exec.
34     Le format eval est un texte source Python qui peut etre evalué. Le
35     résultat de l'évaluation est un objet Python quelconque.
36     Le format dict est un dictionnaire Python.
37     Le format exec est un texte source Python qui peut etre executé. 
38
39     La méthode readfile a pour fonction de lire un fichier dont le
40     nom est passé en argument de la fonction.
41        - convertisseur.readfile(nom_fichier)
42
43     La méthode convert a pour fonction de convertir le fichier
44     préalablement lu dans un objet du format passé en argument.
45        - objet=convertisseur.convert(outformat)
46
47     Ce convertisseur supporte uniquement le format de sortie exec
48
49 """
50 import sys,string,traceback
51
52 from Noyau import N_CR
53
54 def entryPoint():
55    """
56        Retourne les informations nécessaires pour le chargeur de plugins
57        Ces informations sont retournées dans un dictionnaire
58    """
59    return {
60         # Le nom du plugin
61           'name' : 'asterv5',
62         # La factory pour créer une instance du plugin
63           'factory' : AsterParser,
64           }
65
66 import Parserv5.conv
67 import parseur_python
68
69 class AsterParser:
70    """
71    """
72    # Les extensions de fichier préconisées
73    extensions=('.comm',)
74    # Les formats de sortie supportés (eval dict ou exec)
75    formats=('exec','execnoparseur')
76
77    def __init__(self,cr=None):
78       # Si l'objet compte-rendu n'est pas fourni, on utilise le compte-rendu standard
79       if cr :
80          self.cr=cr
81       else:
82          self.cr=N_CR.CR(debut='CR convertisseur format asterv5',
83                          fin='fin CR format asterv5')
84       self.oldtext=''
85       self.out=self.err=self.warn=''
86
87    def readfile(self,filename):
88       self.filename=filename
89       try:
90          self.text=open(filename).read()
91       except:
92          self.cr.fatal("Impossible ouvrir fichier %s" % filename)
93          return
94
95    def convert(self,outformat,appli=None):
96       if outformat == 'exec':
97          return self.getexec()
98       elif outformat == 'execnoparseur':
99          return self.getexecnoparseur()
100       else:
101          raise "Format de sortie : %s, non supporté"
102
103    def getexec(self):
104       if self.text != self.oldtext:
105          self.out, self.err, self.warn= Parserv5.conv.conver(self.text)
106          if self.err:
107             self.cr.fatal("Erreur a l'interpretation de %s" % self.filename)
108             self.cr.fatal(str(self.err))
109             return self.out
110          # On transforme les commentaires et les parametres en objets Python
111          # avec un deuxième parseur
112          try:
113             self.out = parseur_python.PARSEUR_PYTHON(self.out).get_texte()
114          except:
115             self.cr.fatal("Erreur dans la deuxième phase d interpretation de %s" % self.filename)
116             traceback.print_exc()
117             return ""
118          self.oldtext=self.text
119       return self.out
120
121    def getexecnoparseur(self):
122       if self.text != self.oldtext:
123          self.out, self.err, self.warn= Parserv5.conv.conver(self.text)
124          if self.err:
125             self.cr.fatal("Erreur a l'interpretation de %s" % self.filename)
126             self.cr.fatal(str(self.err))
127             return self.out
128          self.oldtext=self.text
129       return self.out
130