1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 from __future__ import absolute_import
22 from builtins import str
26 from Extensions.i18n import tr
27 from Extensions.eficas_exception import EficasException
28 from .generator_python import PythonGenerator
33 Return a dictionary containing the description needed to load the plugin
35 return {'name' : 'file_from_template',
36 'factory' : FileFromTemplateGenerator}
39 class FileFromTemplateGenerator(PythonGenerator):
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).
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.
53 def gener(self, obj, format = 'brut', config = None):
56 self.text = PythonGenerator.gener(self, obj, format)
59 def generate_output_from_template(self) :
61 Generate the output text from the template file and the keywords
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")
70 self.output_text = self.replace_keywords(template)
72 def generMCSIMP(self, obj) :
74 Save object value in the keyword dict for further use, then generate
75 the text corresponding to the MCSIMP element.
77 short_keyword = obj.nom.strip()
79 for i in obj.get_genealogie()[:-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)
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]))
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)