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
21 from Extensions.i18n import tr
22 from Extensions.eficas_exception import EficasException
23 from generator_python import PythonGenerator
27 Return a dictionary containing the description needed to load the plugin
29 return {'name' : 'file_from_template',
30 'factory' : FileFromTemplateGenerator}
33 class FileFromTemplateGenerator(PythonGenerator):
35 This generator creates an output file from a template (file with holes) in
36 addition to Eficas .comm file. The parts to replace in the template must be
37 in the form %KEYWORD%, where KEYWORD may be either the name of the Eficas
38 element (short form, for instance MY_MCSIMP) or the "path" to the Eficas
39 element (long form, for instance MYPROC.MYBLOC.MY_MCSIMP).
41 To use this generator, the configuration of the code must implement two
42 methods: get_extension() that must return the extension of the output file
43 and get_template_file() that must return the path of the template file. Be
44 sure also that your catalog is coherent with your template file.
47 def gener(self, obj, format = 'brut', config = None):
50 self.text = PythonGenerator.gener(self, obj, format)
53 def generate_output_from_template(self) :
55 Generate the output text from the template file and the keywords
57 templateFileName = self.config.get_template_file()
58 if not os.path.isfile(templateFileName):
59 raise EficasException(tr("Fichier patron %s n'existe pas.",
60 str( templateFileName)))
61 f = file(templateFileName, "r")
64 self.output_text = self.replace_keywords(template)
66 def generMCSIMP(self, obj) :
68 Save object value in the keyword dict for further use, then generate
69 the text corresponding to the MCSIMP element.
71 short_keyword = obj.nom.strip()
73 for i in obj.get_genealogie()[:-1]:
74 long_keyword += i + "."
75 long_keyword += short_keyword
76 self.kw_dict[short_keyword] = obj.valeur
77 self.kw_dict[long_keyword] = obj.valeur
78 return PythonGenerator.generMCSIMP(self, obj)
80 def replace_keywords(self, template_string):
81 result = template_string
82 for item in self.kw_dict.iteritems():
83 replace_str = "%" + item[0] + "%"
84 result = result.replace(replace_str, str(item[1]))
87 def writeDefault(self, basefilename):
88 self.generate_output_from_template()
89 output_filename = os.path.splitext(basefilename)[0] + \
90 self.config.get_extension()
91 f = open(output_filename, 'w')
92 f.write(self.output_text)