From 786dbebc2b2765c6d42f90c923d8ec72e128857f Mon Sep 17 00:00:00 2001 From: Renaud Barate Date: Wed, 19 May 2010 16:23:02 +0000 Subject: [PATCH] Added generator and convert for file_from_template (file with holes) generation --- convert/CMakeLists.txt | 1 + convert/convert_file_from_template.py | 28 +++++++ generator/CMakeLists.txt | 1 + generator/generator_file_from_template.py | 94 +++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 convert/convert_file_from_template.py create mode 100644 generator/generator_file_from_template.py diff --git a/convert/CMakeLists.txt b/convert/CMakeLists.txt index c72a3c05..2ce0bfc5 100644 --- a/convert/CMakeLists.txt +++ b/convert/CMakeLists.txt @@ -20,6 +20,7 @@ # ====================================================================== install ( FILES __init__.py parseur_python.py convert_python.py + convert_file_from_template.py DESTINATION ${CMAKE_INSTALL_PREFIX}/convert ) diff --git a/convert/convert_file_from_template.py b/convert/convert_file_from_template.py new file mode 100644 index 00000000..39bd9374 --- /dev/null +++ b/convert/convert_file_from_template.py @@ -0,0 +1,28 @@ +# CONFIGURATION MANAGEMENT OF EDF VERSION +# ====================================================================== +# COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG +# THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY +# IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY +# THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR +# (AT YOUR OPTION) ANY LATER VERSION. +# +# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT +# WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF +# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU +# GENERAL PUBLIC LICENSE FOR MORE DETAILS. +# +# YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE +# ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER, +# 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE. +# +# +# ====================================================================== + +from convert_python import PythonParser + +def entryPoint(): + """ + Return a dictionary containing the description needed to load the plugin + """ + return {'name' : 'file_from_template', + 'factory' : PythonParser} diff --git a/generator/CMakeLists.txt b/generator/CMakeLists.txt index ee47eb38..7db19a87 100644 --- a/generator/CMakeLists.txt +++ b/generator/CMakeLists.txt @@ -20,6 +20,7 @@ # ====================================================================== install ( FILES __init__.py generator_python.py Formatage.py generator_aplat.py + generator_file_from_template.py DESTINATION ${CMAKE_INSTALL_PREFIX}/generator ) diff --git a/generator/generator_file_from_template.py b/generator/generator_file_from_template.py new file mode 100644 index 00000000..1fd91b4b --- /dev/null +++ b/generator/generator_file_from_template.py @@ -0,0 +1,94 @@ +# CONFIGURATION MANAGEMENT OF EDF VERSION +# ====================================================================== +# COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG +# THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY +# IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY +# THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR +# (AT YOUR OPTION) ANY LATER VERSION. +# +# THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT +# WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF +# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU +# GENERAL PUBLIC LICENSE FOR MORE DETAILS. +# +# YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE +# ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER, +# 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE. +# +# +# ====================================================================== + +import os + +from generator_python import PythonGenerator + + +def entryPoint(): + """ + Return a dictionary containing the description needed to load the plugin + """ + return {'name' : 'file_from_template', + 'factory' : FileFromTemplateGenerator} + + +class FileFromTemplateGenerator(PythonGenerator): + """ + This generator creates an output file from a template (file with holes) in + addition to Eficas .comm file. The parts to replace in the template must be + in the form %KEYWORD%, where KEYWORD may be either the name of the Eficas + element (short form, for instance MY_MCSIMP) or the "path" to the Eficas + element (long form, for instance MYPROC.MYBLOC.MY_MCSIMP). + + To use this generator, the configuration of the code must implement two + methods: get_extension() that must return the extension of the output file + and get_template_file() that must return the path of the template file. Be + sure also that your catalog is coherent with your template file. + """ + + def gener(self, obj, format = 'brut', config = None): + self.config = config + self.kw_dict = {} + self.text = PythonGenerator.gener(self, obj, format) + self.generate_output_from_template() + return self.text + + def generate_output_from_template(self) : + """ + Generate the output text from the template file and the keywords + """ + templateFileName = self.config.get_template_file() + if not os.path.isfile(templateFileName): + raise Exception("Template file %s does not exist." % + templateFileName) + f = file(templateFileName, "r") + template = f.read() + f.close() + self.output_text = self.replace_keywords(template) + + def generMCSIMP(self, obj) : + """ + Save object value in the keyword dict for further use, then generate + the text corresponding to the MCSIMP element. + """ + short_keyword = obj.nom.strip() + long_keyword = "" + for i in obj.get_genealogie()[:-1]: + long_keyword += i + "." + long_keyword += short_keyword + self.kw_dict[short_keyword] = obj.valeur + self.kw_dict[long_keyword] = obj.valeur + return PythonGenerator.generMCSIMP(self, obj) + + def replace_keywords(self, template_string): + result = template_string + for item in self.kw_dict.iteritems(): + replace_str = "%" + item[0] + "%" + result = result.replace(replace_str, str(item[1])) + return result + + def writeDefault(self, basefilename): + output_filename = os.path.splitext(basefilename)[0] + \ + self.config.get_extension() + f = open(output_filename, 'w') + f.write(self.output_text) + f.close() -- 2.39.2