Salome HOME
CCAR: ajout du repertoire Doc qui contient un Makefile pour générer une doc
[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          - extensions : qui donne une liste d'extensions de fichier préconisées
58          - formats : qui donne une liste de formats de sortie supportés
59    """
60    # Les extensions de fichier préconisées
61    extensions=('.ini','.conf')
62    # Les formats de sortie supportés (eval ou exec)
63    formats=('eval','dict')
64
65    def __init__(self,cr=None):
66       ConfigParser.__init__(self)
67       # Si l'objet compte-rendu n'est pas fourni, on utilise le compte-rendu standard
68       if cr :
69          self.cr=cr
70       else:
71          self.cr=N_CR.CR(debut='CR convertisseur format ini',
72                          fin='fin CR format ini')
73
74    def readfile(self,filename):
75       try:
76          self.read(filename)
77       except Exception,e:
78          self.cr.fatal(str(e))
79
80    def convert(self,outformat):
81       if outformat == 'eval':
82          return self.getdicttext()
83       elif outformat == 'dict':
84          return self.getdict()
85       else:
86          raise "Format de sortie : %s, non supporté"
87
88    def getdicttext(self):
89       s='{'
90       for section in self.sections():
91          s=s+ "'" + section + "' : {"
92          options=self.options(section)
93          for option in options:
94             value=self.get(section,option)
95             if value == '':value="None"
96             s=s+"'%s' : %s," % (option, value)
97          s=s+"}, "
98       s=s+"}"
99       return s
100
101    def getdict(self):
102       s={}
103       for section in self.sections():
104          s[section]=d={}
105          options=self.options(section)
106          for option in options:
107             value=self.get(section,option)
108             if value == '':
109                d[option]=None
110             else:
111                d[option]=eval(value)
112       return s
113