Salome HOME
Simplifying python files installation scheme
[modules/adao.git] / src / daSalome / daYacsSchemaCreator / help_methods.py
1 #-*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2018 EDF R&D
4 #
5 # This file is part of SALOME ADAO module
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24
25 import sys
26 import os
27 import traceback
28 import logging
29
30 from daYacsSchemaCreator.infos_daComposant import *
31
32 def check_args(args):
33
34   logging.debug("Arguments are :" + str(args))
35   if len(args) != 2:
36     raise ValueError("\n\n Bad number of arguments: you have to provide two arguments (%d given)\n" % (len(args)))
37
38 def check_study(study_config):
39
40   logging.debug("[check_env] study_config : " + str(study_config))
41
42   # Check study_config
43   if not isinstance(study_config, dict):
44     raise ValueError("\n\n Study configuration is not a dictionnary!\n")
45
46   # Name
47   if "Name" not in study_config:
48     raise ValueError("\n\n Cannot find Name in the study configuration!\n")
49
50   # Algorithm
51   if "Algorithm" not in study_config:
52     raise ValueError("\n\n Cannot find Algorithm in the study configuration!\n")
53   else:
54     if not (study_config["Algorithm"] in AssimAlgos or study_config["Algorithm"] in CheckAlgos):
55       raise ValueError("\n\n Algorithm provided is unknow : " + str(study_config["Algorithm"]) +
56                          "\n You can choose between : " + str(AssimAlgos)+" "+str(CheckAlgos) + "\n")
57
58   # Debug
59   if "Debug" not in study_config:
60     study_config["Debug"] = "0"
61
62   # Repertory
63   check_repertory = False
64   repertory = ""
65   if "Repertory" in study_config.keys():
66     repertory = study_config["Repertory"]
67     check_repertory = True
68     if not os.path.isabs(repertory):
69       raise ValueError("\n\n Study repertory should be an absolute path\n"+
70                            " Repertory provided is %s\n" % repertory)
71
72   # Check if all the data is provided
73   for key in AlgoDataRequirements[study_config["Algorithm"]]:
74     if key not in study_config.keys():
75       raise ValueError("\n\nCannot find " +  key + " in your study configuration !" +
76                     "\n This key is mandatory into a study with " + study_config["Algorithm"] + " algorithm." +
77                     "\n " + study_config["Algorithm"] + " requirements are " + str(AlgoDataRequirements[study_config["Algorithm"]]) + "\n")
78
79   # Data
80   for key in study_config.keys():
81     if key in AssimData:
82       check_data(key, study_config[key], check_repertory, repertory)
83
84   # UserDataInit
85   if "UserDataInit" in study_config.keys():
86     check_data("UserDataInit", study_config["UserDataInit"], check_repertory, repertory)
87
88   # Variables
89   check_variables("InputVariables", study_config)
90   check_variables("OutputVariables", study_config)
91
92   # Analyse
93   if "UserPostAnalysis" in study_config.keys():
94     analysis_config = study_config["UserPostAnalysis"]
95     if "From" not in analysis_config:
96       raise ValueError("\n\n UserPostAnalysis found but From is not defined \n in the analysis configuration!\n")
97     else:
98       if analysis_config["From"] not in AnalysisFromList:
99         raise ValueError("\n\n Analysis From defined in the study configuration does not have a correct type : " + str(analysis_config["From"])
100                          + "\n You can have: " + str(AnalysisFromList) + "\n")
101     if "Data" not in analysis_config:
102       raise ValueError("\n\nAnalysis found but Data is not defined in the analysis configuration!\n")
103
104     if analysis_config["From"] == "Script":
105       check_file_name = analysis_config["Data"]
106       if check_repertory and not os.path.exists(check_file_name):
107         check_file_name = os.path.join(repertory, os.path.basename(analysis_config["Data"]))
108       if not os.path.exists(check_file_name):
109         raise ValueError("\n\n The script file cannot be found for UserPostAnalysis,\n please check its availability.\n"+
110                              " The given user file is:\n %s\n" % check_file_name)
111
112   # Check observers
113   if "Observers" in study_config.keys():
114     for obs_var in study_config["Observers"]:
115       # Check du type
116       if not isinstance(study_config["Observers"][obs_var], type({})):
117         raise ValueError("\n\n An observer description has to be a Python dictionary\n"+
118                              " Observer is %s\n" % obs_var)
119       if "nodetype" not in study_config["Observers"][obs_var].keys():
120         raise ValueError("\n\n An observer description must provide a nodetype\n"+
121                              " Observer is %s\n" % obs_var)
122       nodetype = study_config["Observers"][obs_var]["nodetype"]
123       if not isinstance(study_config["Observers"][obs_var]["nodetype"], type("")):
124         raise ValueError("\n\n An observer nodetype description must be a string\n"+
125                              " Observer is %s\n" % obs_var)
126       if nodetype != "String" and nodetype != "Script":
127         raise ValueError("\n\n An observer nodetype must be equal to 'String' or 'Script'\n"+
128                              " Observer is %s\n" % obs_var)
129       if nodetype == "String":
130         if "String" not in study_config["Observers"][obs_var].keys():
131           raise ValueError("\n\n An observer with nodetype String must provide a String\n"+
132                                " Observer is %s\n" % obs_var)
133         if not isinstance(study_config["Observers"][obs_var]["String"], type("")):
134           raise ValueError("\n\n An observer String description must be a string\n"+
135                                " Observer is %s\n" % obs_var)
136       if nodetype == "Script":
137         if "Script" not in study_config["Observers"][obs_var].keys():
138           raise ValueError("\n\n An observer with nodetype Script provide a Script\n"+
139                                " Observer is %s\n" % obs_var)
140         if not isinstance(study_config["Observers"][obs_var]["Script"], type("")):
141           raise ValueError("\n\n An observer Script description must be a string\n"+
142                                " Observer is %s\n" % obs_var)
143       if "scheduler" in study_config["Observers"][obs_var].keys():
144         if not isinstance(study_config["Observers"][obs_var]["scheduler"], type("")):
145           raise ValueError("\n\n An observer scheduler description must be a string\n"+
146                               " Observer is %s\n" % obs_var)
147
148 def check_variables(name, study_config):
149
150   if name not in study_config.keys():
151     raise ValueError("\n\n %s not found in your study configuration!\n" % name)
152
153   variable_config = study_config[name]
154   if "Order" not in variable_config.keys():
155     raise ValueError("\n\n Order not found in the %s configuration!\n" % name)
156
157   list_of_variables = variable_config["Order"]
158   if not isinstance(list_of_variables, type([])):
159     raise ValueError("\n\n Order sould be a list in the %s configuration!\n" % name)
160   if len(list_of_variables) < 1:
161     raise ValueError("\n\nOrder should contain one or more names in the %s configuration!\n" % name)
162
163   for var in list_of_variables:
164     if var not in variable_config.keys():
165       raise ValueError("\n\n Variable %s not found in the %s configuration!\n" % name)
166     value = variable_config[var]
167     try:
168       value = int(value)
169     except:
170       raise ValueError("\n\n Variable %s value cannot be converted in an integer \n in the %s configuration!\n" % name)
171
172 def check_data(data_name, data_config, repertory_check=False, repertory=""):
173
174   logging.debug("[check_data] " + data_name)
175   data_name_data = "Data"
176   data_name_type = "Type"
177   data_name_from = "From"
178
179   if data_name_data not in data_config:
180     raise ValueError("\n\n" + data_name +" found but " + data_name_data +" is not defined in the study configuration !\n")
181
182   if data_name_type not in data_config:
183     raise ValueError("\n\n" + data_name +" found but " + data_name_type  +" is not defined in the study configuration !\n")
184   else:
185     if data_config[data_name_type] not in AssimType[data_name]:
186       raise ValueError("\n\n" + data_name_type + " of " + data_name + " defined in the study configuration does not have a correct type : " + str(data_config[data_name_type])
187                     + "\n You can have : " + str(AssimType[data_name]) + "\n")
188   if data_name_from not in data_config:
189     raise ValueError("\n\n" + data_name + " found but " + data_name_from + " is not defined in the study configuration !\n")
190   else:
191     if data_config[data_name_from] not in FromNumpyList[data_config[data_name_type]]:
192       raise ValueError("\n\n" + data_name_from + " of " + data_name + " defined in the study configuration does not have a correct value : " + str(data_config[data_name_from])
193                     + "\n You can have : " + str(FromNumpyList[data_config[data_name_type]]) + "\n")
194
195   # Check des fichiers
196   from_type = data_config["From"]
197   if from_type == "Script":
198     check_file_name = data_config["Data"]
199     if repertory_check and not os.path.exists(check_file_name):
200       check_file_name = os.path.join(repertory, os.path.basename(data_config["Data"]))
201     if not os.path.exists(check_file_name):
202       raise ValueError("\n\n The script file cannot be found for the \"%s\" keyword, please \n check its availability. The given user file is:\n %s\n"%(from_type,check_file_name))
203   elif (from_type == "FunctionDict" or from_type == "ScriptWithSwitch" or from_type == "ScriptWithFunctions" or from_type == "ScriptWithOneFunction"):
204     TheData = data_config["Data"]
205     for FunctionName in TheData["Function"]:
206       check_file_name = TheData["Script"][FunctionName]
207       if repertory_check and not os.path.exists(check_file_name):
208         check_file_name = os.path.join(repertory, os.path.basename(TheData["Script"][FunctionName]))
209       if not os.path.exists(check_file_name):
210         raise ValueError("\n\n The script file cannot be found for the \"%s\" keyword, please \n check its availability. The given user file is:\n %s\n"%(from_type,check_file_name))