Salome HOME
dernieres modifs
[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 import traceback
26 import types,string
27
28 from Noyau import N_CR
29 from Accas import MCSIMP,MCFACT,MCList
30 from Extensions.i18n import tr
31 from Extensions.eficas_exception import EficasException
32
33
34 def entryPoint():
35    """
36        Retourne les informations nécessaires pour le chargeur de plugins
37
38        Ces informations sont retournées dans un dictionnaire
39    """
40    return {
41         # Le nom du plugin
42           'name' : 'pyth',
43         # La factory pour créer une instance du plugin
44           'factory' : PythGenerator,
45           }
46
47
48 class PythGenerator:
49    """
50        Ce generateur parcourt un objet de type MCFACT et produit
51        un fichier au format pyth
52
53        L'acquisition et le parcours sont réalisés par la méthode
54        generator.gener(objet_mcfact)
55
56        L'écriture du fichier au format ini par appel de la méthode
57        generator.writefile(nom_fichier)
58
59        Ses caractéristiques principales sont exposées dans des attributs 
60        de classe :
61           - extensions : qui donne une liste d'extensions de fichier préconisées
62
63    """
64    # Les extensions de fichier préconisées
65    extensions=('.py','.comm')
66
67    def __init__(self,cr=None):
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 generateur format ini',
73                          fin='fin CR format ini')
74       # Le texte au format pyth est stocké dans l'attribut text
75       self.text=''
76
77    def writefile(self,filename):
78       fp=open(filename,'w')
79       fp.write(self.text)
80       fp.close()
81
82    def gener(self,obj,format='standard',config=None):
83       """
84          Tous les mots-clés simples du niveau haut sont transformés en variables 
85
86          Tous les mots-clés facteurs sont convertis en dictionnaires
87
88          Les mots-clés multiples ne sont pas traités
89       """
90       s=''
91       if isinstance(obj,MCList):
92         if len(obj.data) > 1:
93           raise EficasException(tr("Pas supporte"))
94         else:
95           obj=obj.data[0]
96
97       for mocle in obj.mc_liste:
98         if isinstance(mocle,MCList):
99           if len(mocle.data) > 1:
100             raise EficasException(tr("Pas supporte"))
101           else:
102             valeur=self.generMCFACT(mocle.data[0])
103             s=s+"%s = %s\n" % (mocle.nom,valeur)
104         elif isinstance(mocle,MCFACT):
105           valeur=self.generMCFACT(mocle)
106           s=s+"%s = %s\n" % (mocle.nom,valeur)
107         elif isinstance(v,MCSIMP):
108           valeur = self.generMCSIMP(mocle)
109           s=s+"%s = %s\n" % (mocle.nom,valeur)
110         else:
111           self.cr.fatal("Entite inconnue ou interdite : "+`mocle`)
112
113       self.text=s
114       return self.text
115
116    def generMCFACT(self,obj):
117       """
118          Cette méthode convertit un mot-clé facteur 
119          en une chaine de caractères représentative d'un dictionnaire
120       """
121       s = '{'
122       for mocle in obj.mc_liste:
123          if isinstance(mocle,MCSIMP):
124             valeur = self.generMCSIMP(mocle)
125             s=s+"'%s' : %s,\n" % (mocle.nom,valeur)
126          elif isinstance(mocle,MCFACT):
127             valeur=self.generMCFACT(mocle)
128             s=s+"'%s' : %s,\n" % (mocle.nom,valeur)
129          else:
130             self.cr.fatal(tr("Entite inconnue ou interdite : %s. Elle est ignoree", `mocle`))
131
132       s=s+'}'
133       return s
134
135    def generMCSIMP(self,obj):
136       """
137          Cette méthode convertit un mot-clé simple en une chaine de caractères
138          au format pyth
139       """
140       try:
141          s="%s" % obj.valeur
142       except Exception as e :
143          self.cr.fatal(tr("Type de valeur non supporte par le format pyth : n %(exception)s", \
144                            {'nom': obj.nom, 'exception': unicode(e)}))
145
146
147          s="ERREUR"
148       return s
149