Salome HOME
Merge remote-tracking branch 'andre/V6_main' into V6_main
[modules/adao.git] / src / daSalome / daYacsSchemaCreator / help_methods.py
1 #-*- coding: utf-8 -*-
2 # Copyright (C) 2010-2011 EDF R&D
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author: AndrĂ© Ribes, andre.ribes@edf.fr, EDF R&D
21
22 import sys
23 import os
24 import traceback
25 import logging
26
27 from daYacsSchemaCreator.infos_daComposant import *
28
29 def check_args(args):
30
31   logging.debug("Arguments are :" + str(args))
32   if len(args) != 2:
33     logging.fatal("Bad number of arguments: you have to provide two arguments (%d given)" % (len(args)))
34     sys.exit(1)
35
36 def check_study(study_config):
37
38   logging.debug("[check_env] study_config : " + str(study_config))
39
40   # Check study_config
41   if not isinstance(study_config, dict):
42     logging.fatal("Study configuration is not a dictionnary")
43     sys.exit(1)
44
45   # Name
46   if "Name" not in study_config:
47     logging.fatal("Cannot found Name in the study configuration")
48     sys.exit(1)
49
50   # Algorithm
51   if "Algorithm" not in study_config:
52     logging.fatal("Cannot found Algorithm in the study configuration")
53     sys.exit(1)
54   else:
55     if study_config["Algorithm"] not in AssimAlgos:
56       logging.fatal("Algorithm provided is unknow : " + str(study_config["Algorithm"]) +
57                     "\n You can choose between : " + str(AssimAlgos))
58       sys.exit(1)
59
60   # Debug
61   if "Debug" not in study_config:
62     study_config["Debug"] = "0"
63
64   # Repertory
65   check_repertory = False
66   repertory = ""
67   if "Repertory" in study_config.keys():
68     repertory = study_config["Repertory"]
69     check_repertory = True
70     if not os.path.isabs(repertory):
71       logging.fatal("Study repertory should be an absolute path")
72       logging.fatal("Repertory provided is %s" % repertory)
73       sys.exit(1)
74
75   # Check if all the data is provided
76   for key in AlgoDataRequirements[study_config["Algorithm"]]:
77     if key not in study_config.keys():
78       logging.fatal("Cannot found " +  key + " in your study configuration !" +
79                     "\n This key is mandatory into a study with " + study_config["Algorithm"] + " algorithm." +
80                     "\n " + study_config["Algorithm"] + " requirements are " + str(AlgoDataRequirements[study_config["Algorithm"]]))
81       sys.exit(1)
82
83   # Data
84   for key in study_config.keys():
85     if key in AssimData:
86       check_data(key, study_config[key], check_repertory, repertory)
87
88   # UserDataInit
89   if "UserDataInit" in study_config.keys():
90     check_data("UserDataInit", study_config["UserDataInit"], check_repertory, repertory)
91
92   # Variables
93   check_variables("InputVariables", study_config)
94   check_variables("OutputVariables", study_config)
95
96   # Analyse
97   if "UserPostAnalysis" in study_config.keys():
98     analysis_config = study_config["UserPostAnalysis"]
99     if "From" not in analysis_config:
100       logging.fatal("UserPostAnalysis found but From is not defined in the analysis configuration !")
101       sys.exit(1)
102     else:
103       if analysis_config["From"] not in AnalysisFromList:
104         logging.fatal("Analysis From defined in the study configuration does not have a correct type : " + str(analysis_config["From"])
105                       + "\n You can have : " + str(AnalysisFromList))
106         sys.exit(1)
107     if "Data" not in analysis_config:
108       logging.fatal("Analysis found but Data is not defined in the analysis configuration !")
109       sys.exit(1)
110
111     if analysis_config["From"] == "Script":
112       check_file_name = ""
113       if check_repertory:
114         check_file_name = os.path.join(repertory, os.path.basename(analysis_config["Data"]))
115       else:
116         check_file_name = analysis_config["Data"]
117       if not os.path.exists(check_file_name):
118         logging.fatal("A script file cannot be found")
119         logging.fatal("File is %s" % check_file_name)
120         sys.exit(1)
121
122   # Check observers
123   if "Observers" in study_config.keys():
124     for obs_var in study_config["Observers"]:
125       # Check du type
126       if not isinstance(study_config["Observers"][obs_var], type({})):
127         logging.fatal("An observer description has to be a Python dictionary")
128         logging.fatal("Observer is %s" % obs_var)
129         sys.exit(1)
130       if "nodetype" not in study_config["Observers"][obs_var].keys():
131         logging.fatal("An observer description must provide a nodetype")
132         logging.fatal("Observer is %s" % obs_var)
133         sys.exit(1)
134       nodetype = study_config["Observers"][obs_var]["nodetype"]
135       if not isinstance(study_config["Observers"][obs_var]["nodetype"], type("")):
136         logging.fatal("An observer nodetype description must be a string")
137         logging.fatal("Observer is %s" % obs_var)
138         sys.exit(1)
139       if nodetype != "pyscript" and nodetype != "userfile":
140         logging.fatal("An observer nodetype must be equal to 'pyscript' or 'userfile'")
141         logging.fatal("Observer is %s" % obs_var)
142         sys.exit(1)
143       if nodetype == "pyscript":
144         if "pyscript" not in study_config["Observers"][obs_var].keys():
145           logging.fatal("An observer with nodetype pyscript must provide a pyscript")
146           logging.fatal("Observer is %s" % obs_var)
147           sys.exit(1)
148         if not isinstance(study_config["Observers"][obs_var]["pyscript"], type("")):
149           logging.fatal("An observer pyscript description must be a string")
150           logging.fatal("Observer is %s" % obs_var)
151           sys.exit(1)
152       if nodetype == "userfile":
153         if "userfile" not in study_config["Observers"][obs_var].keys():
154           logging.fatal("An observer with nodetype userfileuserfilemust provide a pyscript")
155           logging.fatal("Observer is %s" % obs_var)
156           sys.exit(1)
157         if not isinstance(study_config["Observers"][obs_var]["userfile"], type("")):
158           logging.fatal("An observer userfile description must be a string")
159           logging.fatal("Observer is %s" % obs_var)
160           sys.exit(1)
161       if "scheduler" in study_config["Observers"][obs_var].keys():
162         if not isinstance(study_config["Observers"][obs_var]["scheduler"], type("")):
163           logging.fatal("An observer scheduler description must be a string")
164           logging.fatal("Observer is %s" % obs_var)
165           sys.exit(1)
166
167 def check_variables(name, study_config):
168
169   if name not in study_config.keys():
170     logging.fatal("%s not found in your study configuration!" % name)
171     sys.exit(1)
172
173   variable_config = study_config[name]
174   if "Order" not in variable_config.keys():
175     logging.fatal("Order not found in the %s configuration!" % name)
176     sys.exit(1)
177
178   list_of_variables = variable_config["Order"]
179   if not isinstance(list_of_variables, type([])):
180     logging.fatal("Order sould be a list in the %s configuration!" % name)
181     sys.exit(1)
182   if len(list_of_variables) < 1:
183     logging.fatal("Order should contain one or more names in the %s configuration!" % name)
184     sys.exit(1)
185
186   for var in list_of_variables:
187     if var not in variable_config.keys():
188       logging.fatal("Variable %s not found in the %s configuration!" % name)
189       sys.exit(1)
190     value = variable_config[var]
191     try:
192       value = int(value)
193     except:
194       loggind.fatal("Variable %s value cannot be converted in an integer in the %s configuration!" % name)
195       sys.exit(1)
196
197 def check_data(data_name, data_config, repertory_check=False, repertory=""):
198
199   logging.debug("[check_data] " + data_name)
200   data_name_data = "Data"
201   data_name_type = "Type"
202   data_name_from = "From"
203
204   if data_name_data not in data_config:
205     logging.fatal(data_name +" found but " + data_name_data +" is not defined in the study configuration !")
206     sys.exit(1)
207
208   if data_name_type not in data_config:
209     logging.fatal(data_name +" found but " + data_name_type  +" is not defined in the study configuration !")
210     sys.exit(1)
211   else:
212     if data_config[data_name_type] not in AssimType[data_name]:
213       logging.fatal(data_name_type + " defined in the study configuration does not have a correct type : " + str(data_config[data_name_type]) 
214                     + "\n You can have : " + str(AssimType[data_name]))
215       sys.exit(1)
216   if data_name_from not in data_config:
217     logging.fatal(data_name + " found but " + data_name_from + " is not defined in the study configuration !")
218     sys.exit(1)
219   else:
220     if data_config[data_name_from] not in FromNumpyList[data_config[data_name_type]]:
221       logging.fatal(data_name_from + " defined in the study configuration does not have a correct value : " + str(data_config[data_name_from]) 
222                     + "\n You can have : " + str(FromNumpyList[data_config[data_name_type]]))
223       sys.exit(1)
224
225   # Check des fichiers
226   from_type = data_config["From"]
227   if from_type == "Script":
228     check_file_name = ""
229     if repertory_check:
230       check_file_name = os.path.join(repertory, os.path.basename(data_config["Data"]))
231     else:
232       check_file_name = data_config["Data"]
233     if not os.path.exists(check_file_name):
234       logging.fatal("A script file cannot be found")
235       logging.fatal("File is %s" % check_file_name)
236       sys.exit(1)
237   elif from_type == "FunctionDict":
238     FunctionDict = data_config["Data"]
239     for FunctionName in FunctionDict["Function"]:
240       check_file_name = ""
241       if repertory_check:
242         check_file_name = os.path.join(repertory, os.path.basename(FunctionDict["Script"][FunctionName]))
243       else:
244         check_file_name = FunctionDict["Script"][FunctionName]
245       if not os.path.exists(check_file_name):
246         logging.fatal("A script file cannot be found")
247         logging.fatal("File is %s" % check_file_name)
248         sys.exit(1)