Salome HOME
bcce9d53bb16033faed0e7c63c2775f32a23b322
[modules/gui.git] / src / SalomeApp / addvars2notebook.py
1 # Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
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 import os
21 import os.path
22
23 def addvars2notebook(filename):
24     #
25     vars_and_values = []
26     contents = []
27     #
28     directory = os.path.dirname(filename)
29     base = os.path.basename(filename)
30     prefix = base[:-3] # remove ".py"
31     prefix = prefix + '_'
32     from os import listdir
33     l = listdir(directory)
34     for f in l:
35         if f.find(prefix) != 0: continue
36         if f[-3:] != ".py": continue
37         module = f[len(prefix):-3]
38         try:
39             mod = __import__("addvars2notebook_%s"%(module))
40         except ImportError:
41             continue
42         try:
43             func = mod.addvars2notebook
44         except AttributeError:
45             continue
46         fff = os.path.join(directory, f)
47         content = func(fff, vars_and_values)
48         contents.append([fff, content])
49         pass
50     #
51     if vars_and_values:
52         stream = open(filename)
53         content = stream.read()
54         stream.close()
55         fields = content.split("import iparameters")
56         if len(fields) == 2:
57             begin = fields[0]
58             if begin.find("## NoteBook E.A.") >= 0:
59                 begin = begin.split("## NoteBook E.A.")[0]
60                 pass
61             end = "import iparameters" + fields[1]
62             content = begin
63             content += "## NoteBook E.A.\n"
64             for var, value in vars_and_values:
65                 content += "notebook.set(%s,%s)\n"%(var.__repr__(), value)
66                 pass
67             content += end
68             stream = open(filename, "w")
69             stream.write(content)
70             stream.close()
71             #
72             for fff, content in contents:
73                 stream = open(fff, "w")
74                 stream.write(content)
75                 stream.close()
76                 pass
77             pass
78         pass
79     #
80     return
81
82 if __name__ == "__main__":
83     import sys
84     addvars2notebook(sys.argv[1])
85     pass