Salome HOME
542746c825ae653a078d13d558d20cf5ef0982fa
[modules/shaper.git] / src / PythonAPI / model / parameter / import_parameter.py
1 # Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 from salome.shaper import model
21 import codecs, sys
22
23 def changeTab(theLine):
24     aResult = theLine.split("#")[0].replace("\t"," ")
25     aResult += theLine[len(aResult):]
26     return aResult
27
28 def importParameters(theDocument, theFileName):
29
30     aResult = []
31     try:
32         aFile = codecs.open(theFileName, 'r', encoding = 'utf_8_sig')
33     except IOError as e:
34         print("Failed to read file: %s" % str(e))
35         return aResult
36
37     for aLine in aFile:
38         aLine = aLine.rstrip("\n")
39         aLine = changeTab(aLine)
40
41         aName = ""
42         aParameter = ""
43         aComment = ""
44
45         aFirstText = aLine.split(" ")[0]
46
47         aName = aFirstText.split("#")[0].strip()
48
49         aLine = aLine.lstrip(aName)
50
51         aParameter = aLine.split("#")[0]
52
53         aLine = aLine.lstrip(aParameter)
54         aLine = aLine.lstrip("#")
55
56         aComment = aLine
57
58         if(len(aName) > 0):
59             try:
60                 aResult.append(model.addParameter(theDocument, aName, aParameter.strip(), aComment.strip()))
61             except SyntaxError as anError:
62                 print(anError, file = sys.stderr)
63
64     aFile.close()
65     return aResult