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