Salome HOME
1fd91b4bffd229511469e7b35413e86277db9376
[tools/eficas.git] / generator / generator_file_from_template.py
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.
8 #
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.
13 #
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.
17 #
18 #
19 # ======================================================================
20
21 import os
22
23 from generator_python import PythonGenerator
24
25
26 def entryPoint():
27     """
28     Return a dictionary containing the description needed to load the plugin
29     """
30     return {'name' : 'file_from_template',
31             'factory' : FileFromTemplateGenerator}
32
33
34 class FileFromTemplateGenerator(PythonGenerator):
35     """
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).
41     
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.
46     """
47     
48     def gener(self, obj, format = 'brut', config = None):
49         self.config = config
50         self.kw_dict = {}
51         self.text = PythonGenerator.gener(self, obj, format)
52         self.generate_output_from_template()
53         return self.text
54     
55     def generate_output_from_template(self) :
56         """
57         Generate the output text from the template file and the keywords
58         """
59         templateFileName = self.config.get_template_file()
60         if not os.path.isfile(templateFileName):
61             raise Exception("Template file %s does not exist." %
62                             templateFileName)
63         f = file(templateFileName, "r")
64         template = f.read()  
65         f.close()
66         self.output_text = self.replace_keywords(template)
67
68     def generMCSIMP(self, obj) :
69         """
70         Save object value in the keyword dict for further use, then generate
71         the text corresponding to the MCSIMP element.
72         """
73         short_keyword = obj.nom.strip()
74         long_keyword = ""
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)
81
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]))
87         return result
88     
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)
94         f.close()