Salome HOME
Horrible hack to allow a correct load of a Telemac case file when done after
[modules/hydrosolver.git] / src / salome_hydro / coupling1d2d / eficas / generator_coupling1d2d.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 re
19
20 from generator.generator_file_from_template import FileFromTemplateGenerator
21
22 def entryPoint():
23     return {'name': 'coupling1d2d',
24             'factory' : Coupling1D2DGenerator}
25
26 class Coupling1D2DGenerator(FileFromTemplateGenerator):
27     """
28     This generator is mostly identical to FileFromTemplateGenerator, but it
29     can also perform replacements using functions. The syntax for this kind
30     of replacement is %func(KEYWORD)%. The replacement method will call
31     the function "func" that must be defined in this class with the parameter
32     KEYWORD. The available function is parse_entry.
33     """
34
35     def generate_output_from_template(self):
36         """
37         Generate the output by replacing the keywords and the functions in the
38         template.
39         """
40         FileFromTemplateGenerator.generate_output_from_template(self)
41         self.output_text = self.replace_functions(self.output_text)
42
43     def replace_functions(self, template_string):
44         """
45         Replace the functions in the template string.
46         """
47         result = template_string
48         pattern = "%(.*)\((.*)\)%"
49         matchObj = re.search(pattern, result)
50         while matchObj:
51             (whole_string, func_name, param_keyword) = matchObj.group(0, 1, 2)
52             param_value = self.kw_dict[param_keyword]
53             func = eval(func_name)
54             value = func(param_value)
55             result = result.replace(whole_string, value)
56             matchObj = re.search(pattern, result)
57         return result
58
59 def parse_entry(selected_value):
60     """
61     Find entry if selected_value is something like "name (entry)"
62     """
63     entry = selected_value
64     match = re.search("\((.*)\)$", entry)
65     if match is not None:
66         entry = match.group(1)
67     return entry