Salome HOME
fin portage python 3
[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 from __future__ import absolute_import
25 try :
26    from builtins import str
27    from builtins import object
28 except : pass
29
30 import traceback
31 import types
32 from Extensions.i18n import tr
33 from Extensions.eficas_exception import EficasException
34
35
36 from Noyau import N_CR
37 from Accas import MCSIMP,MCFACT,MCList
38
39 def entryPoint():
40    """
41        Retourne les informations necessaires pour le chargeur de plugins
42        Ces informations sont retournees dans un dictionnaire
43    """
44    return {
45         # Le nom du plugin
46           'name' : 'ini',
47         # La factory pour creer une instance du plugin
48           'factory' : IniGenerator,
49           }
50
51
52 class IniGenerator(object):
53    """
54        Ce generateur parcourt un objet de type MCFACT et produit
55        un fichier au format ini 
56        L'acquisition et le parcours sont realises par le methode
57        generator.gener(objet_mcfact)
58        L'ecriture du fichier au format ini par appel de la methode
59        generator.writefile(nom_fichier)
60
61        Ses caracteristiques principales sont exposees dans des attributs 
62        de classe :
63          - extensions : qui donne une liste d'extensions de fichier preconisees
64
65    """
66    # Les extensions de fichier preconisees
67    extensions=('.ini','.conf')
68
69    def __init__(self,cr=None):
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 generateur format ini',
75                          fin='fin CR format ini')
76       # Le texte au format ini est stocke dans l'attribut text
77       self.text=''
78
79    def writefile(self,filename):
80       fp=open(filename,'w')
81       fp.write(self.text)
82       fp.close()
83
84    def gener(self,obj,config=None):
85       """
86          Tous les mots-cles simples du niveau haut sont mis dans la section DEFAUT
87          Tous les mots-cles facteurs sont convertis en sections
88          Un mot-cle facteur ne peut contenir que des mots-cles simples. Sinon => erreur
89       """
90       liste_mcfact=[]
91       sect_defaut=''
92       if isinstance(obj,MCList):
93         if len(obj.data) > 1:
94           raise EficasException(tr("Pas supporte"))
95         else:
96           obj=obj.data[0]
97
98       for mocle in obj.mc_liste:
99         if isinstance(mocle,MCList):
100           if len(mocle.data) > 1:
101             raise EficasException(tr("Pas supporte"))
102           else:
103             liste_mcfact.append(self.generMCFACT(mocle.data[0]))
104         elif isinstance(mocle,MCFACT):
105           liste_mcfact.append(self.generMCFACT(mocle))
106         elif isinstance(mocle,MCSIMP):
107           sect_defaut=sect_defaut+self.generMCSIMP(mocle)
108         else:
109           self.cr.fatal(tr("Entite inconnue ou interdite :%s",repr(mocle)))
110
111       self.text=''
112       if sect_defaut != '':
113          self.text="[DEFAULT]\n"+sect_defaut
114       self.text=self.text + ''.join(liste_mcfact,'\n')
115       return self.text
116
117    def generMCFACT(self,obj):
118       """
119          Cette methode convertit un mot-cle facteur ne contenant que des mots-cles
120          simples en une chaine de caracteres
121       """
122       sect_text='[%s]\n' % obj.nom
123       for mocle in obj.mc_liste:
124          if isinstance(mocle,MCSIMP):
125             sect_text=sect_text+self.generMCSIMP(mocle)
126          else:
127             self.cr.fatal(tr("Entite inconnue ou interdite :%s. Elle est ignoree",repr(mocle)))
128       return sect_text
129
130    def generMCSIMP(self,obj):
131       """
132          Cette methode convertit un mot-cle simple en une chaine de caracteres
133          au format ini
134       """
135       s=''
136       if type(obj.valeur) == tuple :
137          self.cr.fatal(tr("Les tuples ne sont pas supportes pour le format ini :%s ", obj.nom))
138          s="%s = %s\n" % (obj.nom,"ERREUR")
139       else :
140          try:
141             s="%s = %s\n" % (obj.nom,obj.valeur)
142          except Exception as e :
143             self.cr.fatal(tr("Type de valeur non supportee par le format ini :%(nom)s\n%(exception)s", \
144                                          {'nom': obj.nom, 'exception': str(e)}))
145       return s
146