]> SALOME platform Git repositories - tools/eficas.git/blob - generator/oldCodes/generator_file_from_template.py
Salome HOME
chgt Copyrigth
[tools/eficas.git] / generator / oldCodes / generator_file_from_template.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021   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 from __future__ import absolute_import
21 try :
22    from builtins import str
23 except : pass
24 import os
25
26 from Extensions.i18n import tr
27 from Extensions.eficas_exception import EficasException
28 from .generator_python import PythonGenerator
29 import six
30
31 def entryPoint():
32     """
33     Return a dictionary containing the description needed to load the plugin
34     """
35     return {'name' : 'file_from_template',
36             'factory' : FileFromTemplateGenerator}
37
38
39 class FileFromTemplateGenerator(PythonGenerator):
40     """
41     This generator creates an output file from a template (file with holes) in
42     addition to Eficas .comm file. The parts to replace in the template must be
43     in the form %KEYWORD%, where KEYWORD may be either the name of the Eficas
44     element (short form, for instance MY_MCSIMP) or the "path" to the Eficas
45     element (long form, for instance MYPROC.MYBLOC.MY_MCSIMP).
46     
47     To use this generator, the configuration of the code must implement two
48     methods: get_extension() that must return the extension of the output file
49     and get_template_file() that must return the path of the template file. Be
50     sure also that your catalog is coherent with your template file.
51     """
52     
53     def gener(self, obj, format = 'brut', config = None):
54         self.config = config
55         self.kw_dict = {}
56         self.text = PythonGenerator.gener(self, obj, format)
57         return self.text
58     
59     def generate_output_from_template(self) :
60         """
61         Generate the output text from the template file and the keywords
62         """
63         templateFileName = self.config.get_template_file()
64         if not os.path.isfile(templateFileName):
65             raise EficasException(tr("Fichier patron %s n'existe pas.",
66                                     str( templateFileName)))
67         f = open(templateFileName, "r")
68         template = f.read()  
69         f.close()
70         self.output_text = self.replace_keywords(template)
71
72     def generMCSIMP(self, obj) :
73         """
74         Save object value in the keyword dict for further use, then generate
75         the text corresponding to the MCSIMP element.
76         """
77         short_keyword = obj.nom.strip()
78         long_keyword = ""
79         for i in obj.getGenealogie()[:-1]:
80             long_keyword += i + "."
81         long_keyword += short_keyword
82         self.kw_dict[short_keyword] = obj.valeur
83         self.kw_dict[long_keyword] = obj.valeur
84         return PythonGenerator.generMCSIMP(self, obj)
85
86     def replace_keywords(self, template_string):
87         result = template_string
88         for item in six.iteritems(self.kw_dict):
89             replace_str = "%" + item[0] + "%"
90             result = result.replace(replace_str, str(item[1]))
91         return result
92     
93     def writeDefault(self, basefilename):
94         self.generate_output_from_template()
95         output_filename = os.path.splitext(basefilename)[0] + \
96                           self.config.get_extension()
97         f = open(output_filename, 'w')
98         f.write(self.output_text)
99         f.close()