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 contient le plugin convertisseur de fichier
22 au format python pour EFICAS.
24 Un plugin convertisseur doit fournir deux attributs de classe :
25 extensions et formats et deux methodes : readfile,convert.
27 L'attribut de classe extensions est une liste d'extensions
28 de fichiers preconisees pour ce type de format. Cette information
29 est seulement indicative.
31 L'attribut de classe formats est une liste de formats de sortie
32 supportes par le convertisseur. Les formats possibles sont :
34 Le format eval est un texte source Python qui peut etre evalue. Le
35 resultat de l'evaluation 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 execute.
39 La methode readfile a pour fonction de lire un fichier dont le
40 nom est passe en argument de la fonction.
41 - convertisseur.readfile(nom_fichier)
43 La methode convert a pour fonction de convertir le fichier
44 prealablement lu dans un objet du format passe en argument.
45 - objet=convertisseur.convert(outformat)
47 Ce convertisseur supporte le format de sortie exec
50 import sys,string,traceback
53 from Noyau import N_CR
54 from Extensions.i18n import tr
55 from Extensions.eficas_exception import EficasException
59 Retourne les informations necessaires pour le chargeur de plugins
60 Ces informations sont retournees dans un dictionnaire
65 # La factory pour creer une instance du plugin
66 'factory' : PythonParser,
72 Ce convertisseur lit un fichier au format python avec la
73 methode readfile : convertisseur.readfile(nom_fichier)
74 et retourne le texte au format outformat avec la
75 methode convertisseur.convert(outformat)
77 Ses caracteristiques principales sont exposees dans 2 attributs
79 - extensions : qui donne une liste d'extensions de fichier preconisees
80 - formats : qui donne une liste de formats de sortie supportes
82 # Les extensions de fichier preconisees
84 # Les formats de sortie supportes (eval dict ou exec)
85 # Le format exec est du python executable (commande exec) converti avec PARSEUR_PYTHON
86 # Le format execnoparseur est du python executable (commande exec) non converti
87 formats=('exec','execnoparseur')
89 def __init__(self,cr=None):
90 # Si l'objet compte-rendu n'est pas fourni, on utilise le
91 # compte-rendu standard
96 self.cr=N_CR.CR(debut='CR convertisseur format python',
97 fin='fin CR format python')
99 def readfile(self,filename):
100 self.filename=filename
102 self.text=open(filename).read()
104 self.cr.exception(tr("Impossible d'ouvrir le fichier %s" ,str(filename)))
105 self.cr.fatal(tr("Impossible d'ouvrir le fichier %s" ,str(filename)))
108 def convert(self,outformat,appli=None):
109 if outformat == 'exec':
111 return parseur_python.PARSEUR_PYTHON(self.text).get_texte(appli)
112 except EficasException:
113 # Erreur lors de la conversion
114 l=traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],
116 self.cr.exception(tr("Impossible de convertir le fichier Python qui doit contenir des erreurs.\n\
117 On retourne le fichier non converti. Prevenir la maintenance.\n\n %s", string.join(l)))
118 # On retourne neanmoins le source initial non converti (au cas ou)
120 elif outformat == 'execnoparseur':
123 raise EficasException(tr("Format de sortie : %s, non supporte", outformat))