From 360b38b56a7d7b687fb4ed4cf980146871d44422 Mon Sep 17 00:00:00 2001 From: Renaud Barate Date: Thu, 10 Nov 2011 15:54:41 +0000 Subject: [PATCH] Fix opening of OT study with inconsistent or inexistent definition of variables (fix bug METIERS #40) --- convert/convert_openturns_study.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/convert/convert_openturns_study.py b/convert/convert_openturns_study.py index ba62b9f9..d1727ad3 100644 --- a/convert/convert_openturns_study.py +++ b/convert/convert_openturns_study.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # CONFIGURATION MANAGEMENT OF EDF VERSION # ====================================================================== # COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG @@ -18,22 +17,33 @@ # # # ====================================================================== -""" -""" -import parseur_python -from convert_python import * +import re +from convert_python import PythonParser def entryPoint(): """ - Retourne les informations nécessaires pour le chargeur de plugins - Ces informations sont retournées dans un dictionnaire + Return a dictionary containing the description needed to load the plugin """ return { - # Le nom du plugin 'name' : 'openturns_study', - # La factory pour créer une instance du plugin - 'factory' : PythonParser, + 'factory' : OTStudyParser } +class OTStudyParser(PythonParser): + """ + This converter works like PythonParser, except that it also initializes all + model variables to None in order to avoid Python syntax errors when loading + a file with a different or inexistent definition of variables. + """ + # We look for pattern "ModelVariable=NOMVAR," + pattern_model_variable = re.compile(r'ModelVariable\s*=\s*(\w+)\s*,') + def convert(self, outformat, appli=None): + text = PythonParser.convert(self, outformat, appli) + varnames = self.pattern_model_variable.findall(text) + newtext = "" + for var in varnames: + newtext += "%s = None\n" % var + newtext += text + return newtext -- 2.39.2