Salome HOME
Merge branch 'V9_9_BR'
[modules/shaper.git] / src / PythonAPI / model / parameter / import_parameter.py
1 # Copyright (C) 2014-2022  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:
34         return aResult
35
36     for aLine in aFile:
37         aLine = aLine.rstrip("\n")
38         aLine = changeTab(aLine)
39
40         aName = ""
41         aParameter = ""
42         aComment = ""
43
44         aFirstText = aLine.split(" ")[0]
45
46         aName = aFirstText.split("#")[0].strip()
47
48         aLine = aLine.lstrip(aName)
49
50         aParameter = aLine.split("#")[0]
51
52         aLine = aLine.lstrip(aParameter)
53         aLine = aLine.lstrip("#")
54
55         aComment = aLine
56
57         if(len(aName) > 0):
58             try:
59                 aResult.append(model.addParameter(theDocument, aName, aParameter.strip(), aComment.strip()))
60             except SyntaxError as anError:
61                 print(anError, file = sys.stderr)
62
63     aFile.close()
64     return aResult