1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2010-2011 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
20 # Author: André Ribes, andre.ribes@edf.fr, EDF R&D
22 from generator.generator_python import PythonGenerator
28 Retourne les informations necessaires pour le chargeur de plugins
30 Ces informations sont retournees dans un dictionnaire
35 # La factory pour creer une instance du plugin
36 'factory' : AdaoGenerator,
39 class AdaoGenerator(PythonGenerator):
41 def __init__(self,cr=None):
42 PythonGenerator.__init__(self, cr)
46 self.text_da_status = False
47 self.logger = logging.getLogger('ADAO EFICAS GENERATOR')
48 self.logger.setLevel(logging.INFO)
49 ch = logging.StreamHandler()
50 ch.setLevel(logging.DEBUG)
51 formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
52 ch.setFormatter(formatter)
53 self.logger.addHandler(ch)
55 def gener(self,obj,format='brut',config=None):
56 self.logger.debug("method gener called")
57 self.text_comm = PythonGenerator.gener(self, obj, format, config)
58 for key, value in self.dictMCVal.iteritems():
59 self.logger.debug("dictMCVAl %s %s" % (key,value))
62 self.text_da_status = False
64 self.text_da_status = True
66 self.logger.info("Case is not correct, python command file for YACS schema generation cannot be created")
67 self.logger.debug(self.text_da)
72 def writeDefault(self, fn):
73 if self.text_da_status:
74 print "write adao python command file"
75 filename = fn[:fn.rfind(".")] + '.py'
76 f = open( str(filename), 'wb')
77 f.write( self.text_da )
80 def generMCSIMP(self,obj) :
82 Convertit un objet MCSIMP en texte python
85 for i in obj.get_genealogie() :
87 self.dictMCVal[clef]=obj.valeur
89 s=PythonGenerator.generMCSIMP(self,obj)
92 def generate_da(self):
94 self.text_da += "#-*-coding:iso-8859-1-*- \n"
95 self.text_da += "study_config = {} \n"
97 # Extraction de Study_name
98 self.text_da += "study_config['Name'] = '" + self.dictMCVal["__ASSIMILATION_STUDY__Study_name"] + "'\n"
100 self.text_da += "study_config['Debug'] = '" + str(self.dictMCVal["__ASSIMILATION_STUDY__Debug"]) + "'\n"
101 # Extraction de Algorithm
102 self.text_da += "study_config['Algorithm'] = '" + self.dictMCVal["__ASSIMILATION_STUDY__Algorithm"] + "'\n"
104 self.add_data("Background")
105 self.add_data("BackgroundError")
106 self.add_data("Observation")
107 self.add_data("ObservationError")
108 self.add_data("ObservationOperator")
111 # Parametres optionnels
113 # Extraction du Study_repertory
114 if "__ASSIMILATION_STUDY__Study_repertory" in self.dictMCVal.keys():
115 self.text_da += "study_config['Repertory'] = '" + self.dictMCVal["__ASSIMILATION_STUDY__Study_repertory"] + "'\n"
116 # Extraction de AlgorithmParameters
117 if "__ASSIMILATION_STUDY__AlgorithmParameters__INPUT_TYPE" in self.dictMCVal.keys():
118 self.add_algorithm_parameters()
119 # Extraction de UserPostAnalysis
120 if "__ASSIMILATION_STUDY__UserPostAnalysis__FROM" in self.dictMCVal.keys():
121 self.add_UserPostAnalysis()
122 if "__ASSIMILATION_STUDY__UserDataInit__INIT_FILE" in self.dictMCVal.keys():
125 def add_data(self, data_name):
127 # Extraction des données
128 search_text = "__ASSIMILATION_STUDY__" + data_name + "__"
129 data_type = self.dictMCVal[search_text + "INPUT_TYPE"]
130 search_type = search_text + data_type + "__data__"
131 from_type = self.dictMCVal[search_type + "FROM"]
133 if from_type == "String":
134 data = self.dictMCVal[search_type + "STRING_DATA__STRING"]
135 elif from_type == "Script":
136 data = self.dictMCVal[search_type + "SCRIPT_DATA__SCRIPT_FILE"]
137 elif from_type == "FunctionDict":
138 data = self.dictMCVal[search_type + "FUNCTIONDICT_DATA__FUNCTIONDICT_FILE"]
140 raise Exception('From Type unknown', from_type)
142 if from_type == "String" or from_type == "Script":
143 self.text_da += data_name + "_config = {} \n"
144 self.text_da += data_name + "_config['Type'] = '" + data_type + "' \n"
145 self.text_da += data_name + "_config['From'] = '" + from_type + "' \n"
146 self.text_da += data_name + "_config['Data'] = '" + data + "' \n"
147 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config \n"
149 if from_type == "FunctionDict":
150 self.text_da += data_name + "_FunctionDict = {} \n"
151 self.text_da += data_name + "_FunctionDict['Function'] = ['Direct', 'Tangent', 'Adjoint'] \n"
152 self.text_da += data_name + "_FunctionDict['Script'] = {} \n"
153 self.text_da += data_name + "_FunctionDict['Script']['Direct'] = '" + data + "' \n"
154 self.text_da += data_name + "_FunctionDict['Script']['Tangent'] = '" + data + "' \n"
155 self.text_da += data_name + "_FunctionDict['Script']['Adjoint'] = '" + data + "' \n"
156 self.text_da += data_name + "_config = {} \n"
157 self.text_da += data_name + "_config['Type'] = 'Function' \n"
158 self.text_da += data_name + "_config['From'] = 'FunctionDict' \n"
159 self.text_da += data_name + "_config['Data'] = " + data_name + "_FunctionDict \n"
160 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config \n"
162 def add_algorithm_parameters(self):
164 data_name = "AlgorithmParameters"
167 data = self.dictMCVal["__ASSIMILATION_STUDY__AlgorithmParameters__Dict__data__SCRIPT_DATA__SCRIPT_FILE"]
169 self.text_da += data_name + "_config = {} \n"
170 self.text_da += data_name + "_config['Type'] = '" + data_type + "' \n"
171 self.text_da += data_name + "_config['From'] = '" + from_type + "' \n"
172 self.text_da += data_name + "_config['Data'] = '" + data + "' \n"
173 self.text_da += "study_config['" + data_name + "'] = " + data_name + "_config \n"
177 init_file_data = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__INIT_FILE"]
178 init_target_list = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__TARGET_LIST"]
180 self.text_da += "Init_config = {} \n"
181 self.text_da += "Init_config['Type'] = 'Dict' \n"
182 self.text_da += "Init_config['From'] = 'Script' \n"
183 self.text_da += "Init_config['Data'] = '" + init_file_data + "'\n"
184 self.text_da += "Init_config['Target'] = ["
185 for target in init_target_list:
186 self.text_da += "'" + target + "',"
187 self.text_da += "] \n"
188 self.text_da += "study_config['UserDataInit'] = Init_config \n"
190 def add_UserPostAnalysis(self):
192 from_type = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__FROM"]
194 if from_type == "String":
195 data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__STRING_DATA__STRING"]
196 self.text_da += "Analysis_config = {} \n"
197 self.text_da += "Analysis_config['From'] = 'String' \n"
198 self.text_da += "Analysis_config['Data'] = \"\"\"" + data + "\"\"\" \n"
199 self.text_da += "study_config['UserPostAnalysis'] = Analysis_config \n"
200 elif from_type == "Script":
201 data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__SCRIPT_DATA__SCRIPT_FILE"]
202 self.text_da += "Analysis_config = {} \n"
203 self.text_da += "Analysis_config['From'] = 'Script' \n"
204 self.text_da += "Analysis_config['Data'] = '" + data + "' \n"
205 self.text_da += "study_config['UserPostAnalysis'] = Analysis_config \n"
207 raise Exception('From Type unknown', from_type)
209 def add_variables(self):
212 if "__ASSIMILATION_STUDY__InputVariables__NAMES" in self.dictMCVal.keys():
215 if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"], type("")):
216 names.append(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"])
218 names = self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__NAMES"]
219 if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"], type(1)):
220 sizes.append(self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"])
222 sizes = self.dictMCVal["__ASSIMILATION_STUDY__InputVariables__SIZES"]
224 self.text_da += "inputvariables_config = {} \n"
225 self.text_da += "inputvariables_config['Order'] = %s \n" % list(names)
226 for name, size in zip(names, sizes):
227 self.text_da += "inputvariables_config['%s'] = %s \n" % (name,size)
228 self.text_da += "study_config['InputVariables'] = inputvariables_config \n"
230 self.text_da += "inputvariables_config = {} \n"
231 self.text_da += "inputvariables_config['Order'] =['adao_default'] \n"
232 self.text_da += "inputvariables_config['adao_default'] = -1 \n"
233 self.text_da += "study_config['InputVariables'] = inputvariables_config \n"
236 if "__ASSIMILATION_STUDY__OutputVariables__NAMES" in self.dictMCVal.keys():
239 if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"], type("")):
240 names.append(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"])
242 names = self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__NAMES"]
243 if isinstance(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"], type(1)):
244 sizes.append(self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"])
246 sizes = self.dictMCVal["__ASSIMILATION_STUDY__OutputVariables__SIZES"]
248 self.text_da += "outputvariables_config = {} \n"
249 self.text_da += "outputvariables_config['Order'] = %s \n" % list(names)
250 for name, size in zip(names, sizes):
251 self.text_da += "outputvariables_config['%s'] = %s \n" % (name,size)
252 self.text_da += "study_config['OutputVariables'] = outputvariables_config \n"
254 self.text_da += "outputvariables_config = {} \n"
255 self.text_da += "outputvariables_config['Order'] = ['adao_default'] \n"
256 self.text_da += "outputvariables_config['adao_default'] = -1 \n"
257 self.text_da += "study_config['OutputVariables'] = outputvariables_config \n"