Back to index

salome-gui  6.5.0
addvars2notebook.py
Go to the documentation of this file.
00001 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 #
00003 # This library is free software; you can redistribute it and/or
00004 # modify it under the terms of the GNU Lesser General Public
00005 # License as published by the Free Software Foundation; either
00006 # version 2.1 of the License.
00007 #
00008 # This library is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 # Lesser General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU Lesser General Public
00014 # License along with this library; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 #
00017 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 #
00019 
00020 import os
00021 import os.path
00022 
00023 def addvars2notebook(filename):
00024     #
00025     vars_and_values = []
00026     contents = []
00027     #
00028     directory = os.path.dirname(filename)
00029     base = os.path.basename(filename)
00030     prefix = base[:-3] # remove ".py"
00031     prefix = prefix + '_'
00032     from os import listdir
00033     l = listdir(directory)
00034     for f in l:
00035         if f.find(prefix) != 0: continue
00036         if f[-3:] != ".py": continue
00037         module = f[len(prefix):-3]
00038         try:
00039             mod = __import__("addvars2notebook_%s"%(module))
00040         except ImportError:
00041             continue
00042         try:
00043             func = mod.addvars2notebook
00044         except AttributeError:
00045             continue
00046         fff = os.path.join(directory, f)
00047         content = func(fff, vars_and_values)
00048         contents.append([fff, content])
00049         pass
00050     #
00051     if vars_and_values:
00052         stream = open(filename)
00053         content = stream.read()
00054         stream.close()
00055         fields = content.split("import iparameters")
00056         if len(fields) == 2:
00057             begin = fields[0]
00058             if begin.find("## NoteBook E.A.") >= 0:
00059                 begin = begin.split("## NoteBook E.A.")[0]
00060                 pass
00061             end = "import iparameters" + fields[1]
00062             content = begin
00063             content += "## NoteBook E.A.\n"
00064             for var, value in vars_and_values:
00065                 content += "notebook.set(%s,%s)\n"%(var.__repr__(), value)
00066                 pass
00067             content += end
00068             stream = open(filename, "w")
00069             stream.write(content)
00070             stream.close()
00071             #
00072             for fff, content in contents:
00073                 stream = open(fff, "w")
00074                 stream.write(content)
00075                 stream.close()
00076                 pass
00077             pass
00078         pass
00079     #
00080     return
00081 
00082 if __name__ == "__main__":
00083     import sys
00084     addvars2notebook(sys.argv[1])
00085     pass