Salome HOME
gestion des listes et label sur 2
[tools/eficas.git] / convert / convert_ini.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 from Extensions.i18n import tr
35 from Extensions.eficas_exception import EficasException
36
37
38 def entryPoint():
39    """
40        Retourne les informations nécessaires pour le chargeur de plugins
41        Ces informations sont retournées dans un dictionnaire
42    """
43    return {
44         # Le nom du plugin
45           'name' : 'ini',
46         # La factory pour créer une instance du plugin
47           'factory' : IniParser,
48           }
49
50
51 class IniParser(ConfigParser):
52    """
53        Ce convertisseur lit un fichier au format ini avec la 
54        methode readfile : convertisseur.readfile(nom_fichier)
55        et retourne le texte au format outformat avec la 
56        methode convertisseur.convert(outformat)
57
58        Ses caractéristiques principales sont exposées dans 2 attributs 
59        de classe :
60          - extensions : qui donne une liste d'extensions de fichier préconisées
61          - formats : qui donne une liste de formats de sortie supportés
62    """
63    # Les extensions de fichier préconisées
64    extensions=('.ini','.conf')
65    # Les formats de sortie supportés (eval ou exec)
66    formats=('eval','dict')
67
68    def __init__(self,cr=None):
69       ConfigParser.__init__(self)
70       # Si l'objet compte-rendu n'est pas fourni, on utilise le compte-rendu standard
71       if cr :
72          self.cr=cr
73       else:
74          self.cr=N_CR.CR(debut='CR convertisseur format ini',
75                          fin='fin CR format ini')
76
77    def readfile(self,filename):
78       try:
79          self.read(filename)
80       except Exception,e:
81          self.cr.fatal(tr("lecture du fichier impossible :")+str(e))
82
83    def convert(self,outformat,appli=None):
84       if outformat == 'eval':
85          return self.getdicttext()
86       elif outformat == 'dict':
87          return self.getdict()
88       else:
89         raise ExceptionException("Format de sortie : %s, non supporte", outformat)
90
91
92    def getdicttext(self):
93       s='{'
94       for section in self.sections():
95          s=s+ "'" + section + "' : {"
96          options=self.options(section)
97          for option in options:
98             value=self.get(section,option)
99             if value == '':value="None"
100             s=s+"'%s' : %s," % (option, value)
101          s=s+"}, "
102       s=s+"}"
103       return s
104
105    def getdict(self):
106       s={}
107       for section in self.sections():
108          s[section]=d={}
109          options=self.options(section)
110          for option in options:
111             value=self.get(section,option)
112             if value == '':
113                d[option]=None
114             else:
115                d[option]=eval(value)
116       return s
117