Salome HOME
CCAR: Modification principale : ajout de la possibilité d'afficher les noms de
[tools/eficas.git] / convert / convert_ini.py
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.
8 #
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.
13 #
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.
17 #
18 #
19 # ======================================================================
20 """
21     Ce module contient le plugin convertisseur de fichier
22     au format ini pour EFICAS.
23     Le convertisseur supporte le format de sortie eval
24
25     Le format eval est un texte Python qui peut etre 
26     evalué avec la commande eval de Python. Il doit donc 
27     etre une expression Python dont l'évaluation permet d'obtenir un objet
28
29 """
30 import traceback
31
32 from ConfigParser import ConfigParser
33 from Noyau import N_CR
34
35 def entryPoint():
36    """
37        Retourne les informations nécessaires pour le chargeur de plugins
38        Ces informations sont retournées dans un dictionnaire
39    """
40    return {
41         # Le nom du plugin
42           'name' : 'ini',
43         # La factory pour créer une instance du plugin
44           'factory' : IniParser,
45           }
46
47
48 class IniParser(ConfigParser):
49    """
50        Ce convertisseur lit un fichier au format ini avec la 
51        methode readfile : convertisseur.readfile(nom_fichier)
52        et retourne le texte au format outformat avec la 
53        methode convertisseur.convert(outformat)
54
55        Ses caractéristiques principales sont exposées dans 2 attributs 
56        de classe :
57
58        - extensions : qui donne une liste d'extensions de fichier préconisées
59
60        - formats : qui donne une liste de formats de sortie supportés
61    """
62    # Les extensions de fichier préconisées
63    extensions=('.ini','.conf')
64    # Les formats de sortie supportés (eval ou exec)
65    formats=('eval','dict')
66
67    def __init__(self,cr=None):
68       ConfigParser.__init__(self)
69       # Si l'objet compte-rendu n'est pas fourni, on utilise le compte-rendu standard
70       if cr :
71          self.cr=cr
72       else:
73          self.cr=N_CR.CR(debut='CR convertisseur format ini',
74                          fin='fin CR format ini')
75
76    def readfile(self,filename):
77       try:
78          self.read(filename)
79       except Exception,e:
80          self.cr.fatal(str(e))
81
82    def convert(self,outformat):
83       if outformat == 'eval':
84          return self.getdicttext()
85       elif outformat == 'dict':
86          return self.getdict()
87       else:
88          raise "Format de sortie : %s, non supporté"
89
90    def getdicttext(self):
91       s='{'
92       for section in self.sections():
93          s=s+ "'" + section + "' : {"
94          options=self.options(section)
95          for option in options:
96             value=self.get(section,option)
97             if value == '':value="None"
98             s=s+"'%s' : %s," % (option, value)
99          s=s+"}, "
100       s=s+"}"
101       return s
102
103    def getdict(self):
104       s={}
105       for section in self.sections():
106          s[section]=d={}
107          options=self.options(section)
108          for option in options:
109             value=self.get(section,option)
110             if value == '':
111                d[option]=None
112             else:
113                d[option]=eval(value)
114       return s
115