Salome HOME
*** empty log message ***
[tools/eficas.git] / convert / convert_ini.py
1 # -*- coding: utf-8 -*-
2 #            CONFIGURATION MANAGEMENT OF EDF VERSION
3 # ======================================================================
4 # COPYRIGHT (C) 1991 - 2002  EDF R&D                  WWW.CODE-ASTER.ORG
5 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
6 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
7 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
8 # (AT YOUR OPTION) ANY LATER VERSION.
9 #
10 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
11 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
12 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
13 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
14 #
15 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
16 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
17 #    1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
18 #
19 #
20 # ======================================================================
21 """
22     Ce module contient le plugin convertisseur de fichier
23     au format ini pour EFICAS.
24     Le convertisseur supporte le format de sortie eval
25
26     Le format eval est un texte Python qui peut etre 
27     evalué avec la commande eval de Python. Il doit donc 
28     etre une expression Python dont l'évaluation permet d'obtenir un objet
29
30 """
31 import traceback
32
33 from ConfigParser import ConfigParser
34 from Noyau import N_CR
35
36 def entryPoint():
37    """
38        Retourne les informations nécessaires pour le chargeur de plugins
39        Ces informations sont retournées dans un dictionnaire
40    """
41    return {
42         # Le nom du plugin
43           'name' : 'ini',
44         # La factory pour créer une instance du plugin
45           'factory' : IniParser,
46           }
47
48
49 class IniParser(ConfigParser):
50    """
51        Ce convertisseur lit un fichier au format ini avec la 
52        methode readfile : convertisseur.readfile(nom_fichier)
53        et retourne le texte au format outformat avec la 
54        methode convertisseur.convert(outformat)
55
56        Ses caractéristiques principales sont exposées dans 2 attributs 
57        de classe :
58          - extensions : qui donne une liste d'extensions de fichier préconisées
59          - formats : qui donne une liste de formats de sortie supportés
60    """
61    # Les extensions de fichier préconisées
62    extensions=('.ini','.conf')
63    # Les formats de sortie supportés (eval ou exec)
64    formats=('eval','dict')
65
66    def __init__(self,cr=None):
67       ConfigParser.__init__(self)
68       # Si l'objet compte-rendu n'est pas fourni, on utilise le compte-rendu standard
69       if cr :
70          self.cr=cr
71       else:
72          self.cr=N_CR.CR(debut='CR convertisseur format ini',
73                          fin='fin CR format ini')
74
75    def readfile(self,filename):
76       try:
77          self.read(filename)
78       except Exception,e:
79          self.cr.fatal(str(e))
80
81    def convert(self,outformat,appli=None):
82       if outformat == 'eval':
83          return self.getdicttext()
84       elif outformat == 'dict':
85          return self.getdict()
86       else:
87          raise "Format de sortie : %s, non supporté"
88
89    def getdicttext(self):
90       s='{'
91       for section in self.sections():
92          s=s+ "'" + section + "' : {"
93          options=self.options(section)
94          for option in options:
95             value=self.get(section,option)
96             if value == '':value="None"
97             s=s+"'%s' : %s," % (option, value)
98          s=s+"}, "
99       s=s+"}"
100       return s
101
102    def getdict(self):
103       s={}
104       for section in self.sections():
105          s[section]=d={}
106          options=self.options(section)
107          for option in options:
108             value=self.get(section,option)
109             if value == '':
110                d[option]=None
111             else:
112                d[option]=eval(value)
113       return s
114