1 # CONFIGURATION MANAGEMENT OF EDF VERSION
2 # ======================================================================
3 # COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG
4 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
5 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
6 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
7 # (AT YOUR OPTION) ANY LATER VERSION.
9 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
10 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
11 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
12 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
14 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
15 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
16 # 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
19 # ======================================================================
23 from generator_python import PythonGenerator
28 Return a dictionary containing the description needed to load the plugin
30 return {'name' : 'file_from_template',
31 'factory' : FileFromTemplateGenerator}
34 class FileFromTemplateGenerator(PythonGenerator):
36 This generator creates an output file from a template (file with holes) in
37 addition to Eficas .comm file. The parts to replace in the template must be
38 in the form %KEYWORD%, where KEYWORD may be either the name of the Eficas
39 element (short form, for instance MY_MCSIMP) or the "path" to the Eficas
40 element (long form, for instance MYPROC.MYBLOC.MY_MCSIMP).
42 To use this generator, the configuration of the code must implement two
43 methods: get_extension() that must return the extension of the output file
44 and get_template_file() that must return the path of the template file. Be
45 sure also that your catalog is coherent with your template file.
48 def gener(self, obj, format = 'brut', config = None):
51 self.text = PythonGenerator.gener(self, obj, format)
52 self.generate_output_from_template()
55 def generate_output_from_template(self) :
57 Generate the output text from the template file and the keywords
59 templateFileName = self.config.get_template_file()
60 if not os.path.isfile(templateFileName):
61 raise Exception("Template file %s does not exist." %
63 f = file(templateFileName, "r")
66 self.output_text = self.replace_keywords(template)
68 def generMCSIMP(self, obj) :
70 Save object value in the keyword dict for further use, then generate
71 the text corresponding to the MCSIMP element.
73 short_keyword = obj.nom.strip()
75 for i in obj.get_genealogie()[:-1]:
76 long_keyword += i + "."
77 long_keyword += short_keyword
78 self.kw_dict[short_keyword] = obj.valeur
79 self.kw_dict[long_keyword] = obj.valeur
80 return PythonGenerator.generMCSIMP(self, obj)
82 def replace_keywords(self, template_string):
83 result = template_string
84 for item in self.kw_dict.iteritems():
85 replace_str = "%" + item[0] + "%"
86 result = result.replace(replace_str, str(item[1]))
89 def writeDefault(self, basefilename):
90 output_filename = os.path.splitext(basefilename)[0] + \
91 self.config.get_extension()
92 f = open(output_filename, 'w')
93 f.write(self.output_text)