Salome HOME
efa22149667481ca36ea5dd2eedfaa82da3e2afd
[modules/hydrosolver.git] / src / salome_hydro / study.py
1 #  Copyright (C) 2012-2013 EDF
2 #
3 #  This file is part of SALOME HYDRO module.
4 #
5 #  SALOME HYDRO module is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9 #
10 #  SALOME HYDRO module is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU General Public License for more details.
14 #
15 #  You should have received a copy of the GNU General Public License
16 #  along with SALOME HYDRO module.  If not, see <http://www.gnu.org/licenses/>.
17
18 import os
19 import types
20
21 from salome.kernel.studyedit import getStudyEditor
22 from salome.kernel.parametric import study_exchange_vars
23
24 import HYDROSOLVER_ORB
25
26 # module constants
27 MODULE_NAME = "HYDROSOLVER"
28
29 COMPONENT_NAME = "HydroSolver"
30 COMPONENT_ICON = "HYDRO_small.png"
31
32 VARS_ICON = "icon_variables.png"
33
34 TELMA_FILE_TYPE = "TELMA_EFICAS_FILE"
35 TELMA_ICON = "telma_file.png"
36 TELMA_TYPE_ID = 1
37
38 STUDY_FILE_TYPE = "STUDY_EFICAS_FILE"
39 STUDY_ICON = "study_file.png"
40 STUDY_TYPE_ID = 2
41
42 PARAM_STUDY_FILE_TYPE = "PARAM_STUDY_EFICAS_FILE"
43 PARAM_STUDY_ICON = "param_study_file.png"
44 PARAM_STUDY_TYPE_ID = 3
45
46 def jdc_to_dict(jdc, command_list):
47     """
48     This tricky function transforms a JdC with a single command into a
49     dictionary that can be used more easily from a Python context (thanks to
50     M. Courtois and G. Boulant).
51     """
52     context = {}
53     for command in command_list:
54         context[command] = _args_to_dict
55     exec("parameters = " + jdc.strip(), context)
56     return context['parameters']
57
58 def _args_to_dict(**kwargs):
59     return kwargs
60
61 def get_jdc_dict_var_as_tuple(jdc_dict, varname):
62     if varname not in jdc_dict:
63         return tuple()
64     elif not isinstance(jdc_dict[varname], tuple):
65         return (jdc_dict[varname],)
66     else:
67         return jdc_dict[varname]
68
69 class HydroStudyEditor:
70     """
71     This class provides utility methods to edit the component "Hydro" in
72     the study. The parameter `studyId` defines the ID of the study to edit. If
73     it is :const:`None`, the edited study will be the current study.
74     """
75     def __init__(self, studyId = None):
76         self.editor = getStudyEditor()
77         self.hydroComp = None
78
79     def find_or_create_hydro_component(self):
80         """
81         Find the component "Hydro" or create it if none is found
82         :return: the SComponent found or created.
83         """
84         if self.hydroComp is None:
85             self.hydroComp = self.editor.findOrCreateComponent(MODULE_NAME,
86                                                                COMPONENT_NAME,
87                                                                COMPONENT_ICON)
88         return self.hydroComp
89
90     def find_or_create_telma(self, filePath):
91         self.find_or_create_hydro_component()
92         itemName = os.path.splitext(os.path.basename(filePath))[0]
93         sobj = self.editor.findOrCreateItem(self.hydroComp,
94                                             name=itemName,
95                                             fileType=TELMA_FILE_TYPE,
96                                             fileName=filePath,
97                                             icon=TELMA_ICON,
98                                             comment=str(filePath),
99                                             typeId=TELMA_TYPE_ID)
100
101     def find_or_create_param_study(self, filePath):
102         self.find_or_create_hydro_component()
103         itemName = os.path.splitext(os.path.basename(filePath))[0]
104         sobj = self.editor.findOrCreateItem(self.hydroComp,
105                                             name=itemName,
106                                             fileType=PARAM_STUDY_FILE_TYPE,
107                                             fileName=filePath,
108                                             icon=PARAM_STUDY_ICON,
109                                             comment=str(filePath),
110                                             typeId=PARAM_STUDY_TYPE_ID)
111
112     def find_or_create_run_study(self, filePath):
113         self.find_or_create_hydro_component()
114         itemName = os.path.splitext(os.path.basename(filePath))[0]
115         sobj = self.editor.findOrCreateItem(self.hydroComp,
116                                             name=itemName,
117                                             fileType=STUDY_FILE_TYPE,
118                                             fileName=filePath,
119                                             icon=STUDY_ICON,
120                                             comment=str(filePath),
121                                             typeId=STUDY_TYPE_ID)
122
123     def generate_study_script(self, filePath):
124         """
125         Generating a python script from the eficas info
126         """
127         # Create "Python script" item
128         from TelApy.api.generate_study import generate_study_script
129         with open(filePath) as jdcfile:
130             jdc = jdcfile.read()
131         params = jdc_to_dict(jdc, ["TELEMAC2D", "_F"])
132
133         # Generation script string
134         python_script = generate_study_script(params)
135
136         # Computing name of the file (same as filePath)
137         file_dir, filename = os.path.split(filePath)
138         root, _ = os.path.splitext(filename)
139         python_file = os.path.join(file_dir, root+".py")
140
141         # Writting to file
142         with open(python_file, 'w') as pfile:
143             pfile.write(python_script)
144
145     def generate_study_yacs(self, filePath):
146         """
147         Generating a yacs file from the eficas info
148         """
149         # Create "Python script" item
150         from TelApy.api.generate_study import generate_study_yacs
151         with open(filePath) as jdcfile:
152             jdc = jdcfile.read()
153         params = jdc_to_dict(jdc, ["TELEMAC2D", "_F"])
154
155         # Generation YACS scheme
156         yacs_scheme = generate_study_yacs(params)
157
158         # Computing name of the file (same as filePath)
159         file_dir, filename = os.path.split(filePath)
160         root, _ = os.path.splitext(filename)
161         yacs_file = os.path.join(file_dir, root+".xml")
162
163         # Writting to file
164         yacs_scheme.saveSchema(yacs_file)