Salome HOME
premiere version
[tools/eficas.git] / generator / generator_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 generateur de fichier
22     au format ini pour EFICAS.
23
24
25 """
26 import traceback
27 import types,string
28 from Extensions.i18n import tr
29 from Extensions.eficas_exception import EficasException
30
31
32 from Noyau import N_CR
33 from Accas import MCSIMP,MCFACT,MCList
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' : IniGenerator,
45           }
46
47
48 class IniGenerator:
49    """
50        Ce generateur parcourt un objet de type MCFACT et produit
51        un fichier au format ini 
52        L'acquisition et le parcours sont réalisés par le méthode
53        generator.gener(objet_mcfact)
54        L'écriture du fichier au format ini par appel de la méthode
55        generator.writefile(nom_fichier)
56
57        Ses caractéristiques principales sont exposées dans des attributs 
58        de classe :
59          - extensions : qui donne une liste d'extensions de fichier préconisées
60
61    """
62    # Les extensions de fichier préconisées
63    extensions=('.ini','.conf')
64
65    def __init__(self,cr=None):
66       # Si l'objet compte-rendu n'est pas fourni, on utilise le compte-rendu standard
67       if cr :
68          self.cr=cr
69       else:
70          self.cr=N_CR.CR(debut='CR generateur format ini',
71                          fin='fin CR format ini')
72       # Le texte au format ini est stocké dans l'attribut text
73       self.text=''
74
75    def writefile(self,filename):
76       fp=open(filename,'w')
77       fp.write(self.text)
78       fp.close()
79
80    def gener(self,obj,config=None):
81       """
82          Tous les mots-clés simples du niveau haut sont mis dans la section DEFAUT
83          Tous les mots-clés facteurs sont convertis en sections
84          Un mot-clé facteur ne peut contenir que des mots-clés simples. Sinon => erreur
85       """
86       liste_mcfact=[]
87       sect_defaut=''
88       if isinstance(obj,MCList):
89         if len(obj.data) > 1:
90           raise EficasException(tr("Pas supporte"))
91         else:
92           obj=obj.data[0]
93
94       for mocle in obj.mc_liste:
95         if isinstance(mocle,MCList):
96           if len(mocle.data) > 1:
97             raise EficasException(tr("Pas supporte"))
98           else:
99             liste_mcfact.append(self.generMCFACT(mocle.data[0]))
100         elif isinstance(mocle,MCFACT):
101           liste_mcfact.append(self.generMCFACT(mocle))
102         elif isinstance(mocle,MCSIMP):
103           sect_defaut=sect_defaut+self.generMCSIMP(mocle)
104         else:
105           self.cr.fatal(tr("Entite inconnue ou interdite :%s",`mocle`))
106
107       self.text=''
108       if sect_defaut != '':
109          self.text="[DEFAULT]\n"+sect_defaut
110       self.text=self.text + string.join(liste_mcfact,'\n')
111       return self.text
112
113    def generMCFACT(self,obj):
114       """
115          Cette méthode convertit un mot-clé facteur ne contenant que des mots-clés
116          simples en une chaine de caractères
117       """
118       sect_text='[%s]\n' % obj.nom
119       for mocle in obj.mc_liste:
120          if isinstance(mocle,MCSIMP):
121             sect_text=sect_text+self.generMCSIMP(mocle)
122          else:
123             self.cr.fatal(tr("Entite inconnue ou interdite :%s. Elle est ignoree",`mocle`))
124       return sect_text
125
126    def generMCSIMP(self,obj):
127       """
128          Cette méthode convertit un mot-clé simple en une chaine de caractères
129          au format ini
130       """
131       s=''
132       if type(obj.valeur) == types.TupleType :
133          self.cr.fatal(tr("Les tuples ne sont pas supportes pour le format ini :%s ", obj.nom))
134          s="%s = %s\n" % (obj.nom,"ERREUR")
135       else :
136          try:
137             s="%s = %s\n" % (obj.nom,obj.valeur)
138          except Exception as e :
139             self.cr.fatal(tr("Type de valeur non supportee par le format ini :%(nom)s\n%(exception)s", \
140                                          {'nom': obj.nom, 'exception': str(e)}))
141       return s
142