Salome HOME
chgt Copyrigth
[tools/eficas.git] / convert / convert_dico.py
1 # Copyright (C) 2007-2021   EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19 from __future__ import absolute_import
20
21
22 #from Extensions.i18n import tr
23 #from Extensions import localisation
24
25
26 from .convert_python import Pythonparser
27 from Noyau import N_CR
28
29 try:
30   basestring
31 except NameError:
32   basestring = str
33
34
35
36 def entryPoint():
37    """
38    Return a dictionary containing the description needed to load the plugin
39    """
40    return {
41           'name' : 'dico',
42           'factory' : Dicoparser
43           }
44
45 class Dicoparser(Pythonparser):
46   """
47    This converter initializes model variable from a python dictionnary
48   """
49
50   def __init__(self,cr=None):
51       # Si l'objet compte-rendu n'est pas fourni, on utilise le
52       # compte-rendu standard
53       self.text=''
54       self.textePy=''
55       if cr :
56          self.cr=cr
57       else:
58          self.cr=N_CR.CR(debut='CR convertisseur format dico',
59                          fin='fin CR format dico')
60
61   def readfile(self,filename):
62      self.filename=filename
63      try:
64         self.text=open(filename).read()
65      except:
66         self.cr.exception(tr("Impossible d'ouvrir le fichier %s" ,str(filename)))
67         self.cr.fatal(tr("Impossible d'ouvrir le fichier %s" ,str(filename)))
68         return
69
70   def convert(self,outformat,appli=None):
71      monTexteDico={}
72      exec (self.text,globals(),monTexteDico)
73      if len(monTexteDico.keys()) != 1 : 
74         self.cr.exception(tr("Impossible de traiter le fichier %s" ,str(filename)))
75         self.cr.fatal(tr("Impossible de traiter le fichier %s" ,str(filename)))
76         return
77      self.textePy=""
78      monDico=monTexteDico[monTexteDico.keys()[0]]
79      for commande in monDico :
80          valeurs=monDico[commande]
81          if valeurs.has_key('NomDeLaSdCommande') :
82             # cas d un oper 
83               self.textePy+=valeurs['NomDeLaSdCommande']+' = '+commande+'('
84               del valeurs['NomDeLaSdCommande']
85          else :
86               self.textePy+=commande+'('
87          for mot in valeurs :
88              if isinstance(valeurs[mot],dict) : self.traiteMCFact(mot,valeurs[mot])
89              else : self.textePy += mot+' = ' +str(valeurs[mot])+','
90          self.textePy+=');\n' # fin de la commande
91      #print (self.textePy)
92      return self.textePy
93
94   def traiteMCFact(self,mot,valeurs):
95       self.textePy += mot + '=_F('
96       for mot in valeurs :
97           if isinstance(valeurs[mot],dict) : self.traiteMCFact(mot,valeurs[mot])
98           else : self.textePy +=mot+' = ' +str(valeurs[mot])+','
99       self.textePy +='),'
100