]> SALOME platform Git repositories - modules/adao.git/blob - src/daEficas/generator_adao.py
Salome HOME
Debut ajout definition variables
[modules/adao.git] / src / daEficas / generator_adao.py
1 # -*- coding: utf-8 -*-
2 print "import generator_adao"
3
4 from generator.generator_python import PythonGenerator
5 import traceback
6 import logging
7
8 def entryPoint():
9    """
10       Retourne les informations necessaires pour le chargeur de plugins
11
12       Ces informations sont retournees dans un dictionnaire
13    """
14    return {
15         # Le nom du plugin
16         'name' : 'adao',
17         # La factory pour creer une instance du plugin
18           'factory' : AdaoGenerator,
19           }
20
21 class AdaoGenerator(PythonGenerator):
22
23   def __init__(self,cr=None):
24     PythonGenerator.__init__(self, cr)
25     self.dictMCVal={}
26     self.text_comm = ""
27     self.text_da = ""
28     self.text_da_status = False
29     self.logger = logging.getLogger('ADAO EFICAS GENERATOR')
30     self.logger.setLevel(logging.DEBUG)
31     ch = logging.StreamHandler()
32     ch.setLevel(logging.DEBUG)
33     formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
34     ch.setFormatter(formatter)
35     self.logger.addHandler(ch)
36
37
38   def gener(self,obj,format='brut',config=None):
39     self.logger.debug("method gener called")
40     self.text_comm = PythonGenerator.gener(self, obj, format, config)
41     for key, value in self.dictMCVal.iteritems():
42       self.logger.debug("dictMCVAl %s %s" % (key,value))
43
44     try :
45       self.text_da_status = False
46       self.generate_da()
47       self.text_da_status = True
48     except:
49       self.logger.info("Case is not correct, python command file for YACS schema generation cannot be created")
50       self.logger.debug(self.text_da)
51       self.dictMCVal = {}
52       traceback.print_exc()
53     return self.text_comm
54
55   def writeDefault(self, fn):
56     if self.text_da_status:
57       print "write adao python command file"
58       filename = fn[:fn.rfind(".")] + '.py'
59       f = open( str(filename), 'wb')
60       f.write( self.text_da )
61       f.close()
62
63   def generMCSIMP(self,obj) :
64     """
65     Convertit un objet MCSIMP en texte python
66     """
67     clef=""
68     for i in obj.get_genealogie() :
69       print obj
70       print obj.get_genealogie()
71       clef=clef+"__"+i
72     self.dictMCVal[clef]=obj.valeur
73
74     s=PythonGenerator.generMCSIMP(self,obj)
75     return s
76
77   def generate_da(self):
78
79     self.text_da += "#-*-coding:iso-8859-1-*- \n"
80     self.text_da += "study_config = {} \n"
81
82     # Extraction de Study_name
83     self.text_da += "study_config[\"Name\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Study_name"] + "\"\n"
84     # Extraction de Debug
85     self.text_da += "study_config[\"Debug\"] = \"" + str(self.dictMCVal["__ASSIMILATION_STUDY__Debug"]) + "\"\n"
86     # Extraction de Algorithm
87     self.text_da += "study_config[\"Algorithm\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Algorithm"] + "\"\n"
88
89     self.add_data("Background")
90     self.add_data("BackgroundError")
91     self.add_data("Observation")
92     self.add_data("ObservationError")
93     self.add_data("ObservationOperator")
94
95     # Parametres optionnels
96
97     # Extraction du Study_repertory
98     if "__ASSIMILATION_STUDY__Study_repertory" in self.dictMCVal.keys():
99       self.text_da += "study_config[\"Repertory\"] = \"" + self.dictMCVal["__ASSIMILATION_STUDY__Study_repertory"] + "\"\n"
100     # Extraction de AlgorithmParameters
101     if "__ASSIMILATION_STUDY__AlgorithmParameters__INPUT_TYPE" in self.dictMCVal.keys():
102       self.add_algorithm_parameters()
103     # Extraction de UserPostAnalysis
104     if "__ASSIMILATION_STUDY__UserPostAnalysis__FROM" in self.dictMCVal.keys():
105       self.add_UserPostAnalysis()
106     if "__ASSIMILATION_STUDY__UserDataInit__INIT_FILE" in self.dictMCVal.keys():
107       self.add_init()
108
109   def add_data(self, data_name):
110
111     # Extraction des données
112     search_text = "__ASSIMILATION_STUDY__" + data_name + "__"
113     data_type = self.dictMCVal[search_text + "INPUT_TYPE"]
114     search_type = search_text + data_type + "__data__"
115     from_type = self.dictMCVal[search_type + "FROM"]
116     data = ""
117     if from_type == "String":
118       data = self.dictMCVal[search_type + "STRING_DATA__STRING"]
119     elif from_type == "Script":
120       data = self.dictMCVal[search_type + "SCRIPT_DATA__SCRIPT_FILE"]
121     elif from_type == "FunctionDict":
122       data = self.dictMCVal[search_type + "FUNCTIONDICT_DATA__FUNCTIONDICT_FILE"]
123     else:
124       raise Exception('From Type unknown', from_type)
125
126     if from_type == "String" or from_type == "Script":
127       self.text_da += data_name + "_config = {} \n"
128       self.text_da += data_name + "_config[\"Type\"] = \"" + data_type + "\" \n"
129       self.text_da += data_name + "_config[\"From\"] = \"" + from_type + "\" \n"
130       self.text_da += data_name + "_config[\"Data\"] = \"" + data      + "\" \n"
131       self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
132
133     if from_type == "FunctionDict":
134       self.text_da += data_name + "_FunctionDict = {} \n"
135       self.text_da += data_name + "_FunctionDict[\"Function\"] = [\"Direct\", \"Tangent\", \"Adjoint\"] \n"
136       self.text_da += data_name + "_FunctionDict[\"Script\"] = {} \n"
137       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Direct\"] = \""  + data + "\" \n"
138       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Tangent\"] = \"" + data + "\" \n"
139       self.text_da += data_name + "_FunctionDict[\"Script\"][\"Adjoint\"] = \"" + data + "\" \n"
140       self.text_da += data_name + "_config = {} \n"
141       self.text_da += data_name + "_config[\"Type\"] = \"Function\" \n"
142       self.text_da += data_name + "_config[\"From\"] = \"FunctionDict\" \n"
143       self.text_da += data_name + "_config[\"Data\"] = " + data_name + "_FunctionDict \n"
144       self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
145
146   def add_algorithm_parameters(self):
147
148     data_name = "AlgorithmParameters"
149     data_type = "Dict"
150     from_type = "Script"
151     data = self.dictMCVal["__ASSIMILATION_STUDY__AlgorithmParameters__Dict__data__SCRIPT_DATA__SCRIPT_FILE"]
152
153     self.text_da += data_name + "_config = {} \n"
154     self.text_da += data_name + "_config[\"Type\"] = \"" + data_type + "\" \n"
155     self.text_da += data_name + "_config[\"From\"] = \"" + from_type + "\" \n"
156     self.text_da += data_name + "_config[\"Data\"] = \"" + data + "\" \n"
157     self.text_da += "study_config[\"" + data_name + "\"] = " + data_name + "_config \n"
158
159   def add_init(self):
160
161       init_file_data = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__INIT_FILE"]
162       init_target_list = self.dictMCVal["__ASSIMILATION_STUDY__UserDataInit__TARGET_LIST"]
163
164       self.text_da += "Init_config = {} \n"
165       self.text_da += "Init_config[\"Type\"] = \"Dict\" \n"
166       self.text_da += "Init_config[\"From\"] = \"Script\" \n"
167       self.text_da += "Init_config[\"Data\"] = \"" + init_file_data + "\"\n"
168       self.text_da += "Init_config[\"Target\"] = ["
169       for target in init_target_list:
170         self.text_da += "\"" + target + "\","
171       self.text_da += "] \n"
172       self.text_da += "study_config[\"UserDataInit\"] = Init_config \n"
173
174   def add_UserPostAnalysis(self):
175
176     from_type = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__FROM"]
177     data = ""
178     if from_type == "String":
179       data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__STRING_DATA__STRING"]
180       self.text_da += "Analysis_config = {} \n"
181       self.text_da += "Analysis_config[\"From\"] = \"String\" \n"
182       self.text_da += "Analysis_config[\"Data\"] = \"\"\"" + data + "\"\"\" \n"
183       self.text_da += "study_config[\"UserPostAnalysis\"] = Analysis_config \n"
184     elif from_type == "Script":
185       data = self.dictMCVal["__ASSIMILATION_STUDY__UserPostAnalysis__SCRIPT_DATA__SCRIPT_FILE"]
186       self.text_da += "Analysis_config = {} \n"
187       self.text_da += "Analysis_config[\"From\"] = \"Script\" \n"
188       self.text_da += "Analysis_config[\"Data\"] = \"" + data + "\" \n"
189       self.text_da += "study_config[\"UserPostAnalysis\"] = Analysis_config \n"
190     else:
191       raise Exception('From Type unknown', from_type)