Salome HOME
Update version
[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             with open(filename) as fd :
65                 self.text=fd.read()
66         except:
67             self.cr.exception(tr("Impossible d'ouvrir le fichier %s" ,str(filename)))
68             self.cr.fatal(tr("Impossible d'ouvrir le fichier %s" ,str(filename)))
69             return
70
71     def convert(self,outformat,appli=None):
72         monTexteDico={}
73         exec (self.text,globals(),monTexteDico)
74         if len(monTexteDico.keys()) != 1 :
75             self.cr.exception(tr("Impossible de traiter le fichier %s" ,str(filename)))
76             self.cr.fatal(tr("Impossible de traiter le fichier %s" ,str(filename)))
77             return
78         self.textePy=""
79         monDico=monTexteDico[monTexteDico.keys()[0]]
80         for commande in monDico :
81             valeurs=monDico[commande]
82             if valeurs.has_key('NomDeLaSdCommande') :
83                # cas d un oper
84                 self.textePy+=valeurs['NomDeLaSdCommande']+' = '+commande+'('
85                 del valeurs['NomDeLaSdCommande']
86             else :
87                 self.textePy+=commande+'('
88             for mot in valeurs :
89                 if isinstance(valeurs[mot],dict) : self.traiteMCFact(mot,valeurs[mot])
90                 else : self.textePy += mot+' = ' +str(valeurs[mot])+','
91             self.textePy+=');\n' # fin de la commande
92         #print (self.textePy)
93         return self.textePy
94
95     def traiteMCFact(self,mot,valeurs):
96         self.textePy += mot + '=_F('
97         for mot in valeurs :
98             if isinstance(valeurs[mot],dict) : self.traiteMCFact(mot,valeurs[mot])
99             else : self.textePy +=mot+' = ' +str(valeurs[mot])+','
100         self.textePy +='),'