1 # CONFIGURATION MANAGEMENT OF EDF VERSION
2 # ======================================================================
3 # COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG
4 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
5 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
6 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
7 # (AT YOUR OPTION) ANY LATER VERSION.
9 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
10 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
11 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
12 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
14 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
15 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
16 # 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
19 # ======================================================================
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 méthodes : readfile,convert.
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.
31 L'attribut de classe formats est une liste de formats de sortie
32 supportés par le convertisseur. Les formats possibles sont :
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é.
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)
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)
47 Ce convertisseur supporte le format de sortie dict
50 import sys,string,traceback
52 from Noyau import N_CR
56 Retourne les informations nécessaires pour le chargeur de plugins
57 Ces informations sont retournées dans un dictionnaire
62 # La factory pour créer une instance du plugin
63 'factory' : PythParser,
69 Ce convertisseur lit un fichier au format pyth avec la
70 methode readfile : convertisseur.readfile(nom_fichier)
71 et retourne le texte au format outformat avec la
72 methode convertisseur.convert(outformat)
74 Ses caractéristiques principales sont exposées dans 2 attributs
77 - extensions : qui donne une liste d'extensions de fichier préconisées
79 - formats : qui donne une liste de formats de sortie supportés
81 # Les extensions de fichier préconisées
83 # Les formats de sortie supportés (eval dict ou exec)
86 def __init__(self,cr=None):
87 # Si l'objet compte-rendu n'est pas fourni, on utilise le compte-rendu standard
91 self.cr=N_CR.CR(debut='CR convertisseur format pyth',
92 fin='fin CR format pyth')
95 def readfile(self,filename):
96 self.filename=filename
98 self.text=open(filename).read()
100 self.cr.fatal("Impossible ouvrir fichier %s",filename)
104 exec self.text in self.g
106 l=traceback.format_exception(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2])
107 s= string.join(l[2:])
108 s= string.replace(s,'"<string>"','"<%s>"'%self.filename)
109 self.cr.fatal("Erreur a l'evaluation :\n" + s)
111 def convert(self,outformat):
112 if outformat == 'dict':
113 return self.getdict()
115 raise "Format de sortie : %s, non supporté"
119 for k,v in self.g.items():
120 if k[0] != '_':d[k]=v