Salome HOME
Copyright update 2021
[modules/smesh.git] / doc / salome / gui / SMESH / merge_mesh_class.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2017-2021  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 import inspect
21 import sys
22 from types import FunctionType
23 import copy
24
25 ORIGIN_MODULE_SUFFIX = "_origin"
26 DYNAMIC_MODULE_SUFFIX = "_dynamic"
27
28
29 def main(module_name, output_file = "smeshBuilder.py"):
30     oringin_module_name = module_name + ORIGIN_MODULE_SUFFIX
31     dynamic_module_name = module_name + DYNAMIC_MODULE_SUFFIX
32     try:
33         exec( "import %s" % oringin_module_name )
34         origin_module = locals()[ oringin_module_name ]
35         origin_module_lines = inspect.getsourcelines( origin_module )[0]
36         origin_meshClass_lines = inspect.getsourcelines(origin_module.Mesh)[0]
37         origin_module_text = "".join( origin_module_lines )
38         origin_meshClass_text = "".join( origin_meshClass_lines )
39         
40         exec( "import %s" % dynamic_module_name )
41         dynanmic_module = locals()[ dynamic_module_name ]
42         dynanmic_meshClass = dynanmic_module.Mesh
43     
44         new_meshClass_lines = copy.copy(origin_meshClass_lines)
45         # remove end of class 'pass'
46         if new_meshClass_lines[-1].find("pass") > 0:
47             new_meshClass_lines.pop()
48             
49         dynanmic_meshClass_methods = [x for x, y in dynanmic_meshClass.__dict__.items() if type(y) == FunctionType]
50         for method in dynanmic_meshClass_methods:
51             exec( "method_lines = inspect.getsourcelines(dynanmic_module.Mesh.%s)[0]"  % method)
52             new_meshClass_lines+=locals()['method_lines']
53             pass
54         new_meshClass_text = "".join( new_meshClass_lines )            
55
56         f = open( output_file, "w" )
57         
58         f.write( origin_module_text.replace( origin_meshClass_text, new_meshClass_text) )
59         f.close()        
60     except Exception as e:
61         print(e)
62         pass
63     pass
64
65
66 if __name__ == "__main__":
67     import optparse
68     parser = optparse.OptionParser(usage="%prog [options] modulename")
69     h  = "Output file (smeshBuilder.py by default)"
70     parser.add_option("-o", "--output", dest="output",
71                       action="store", default="smeshBuilder.py", metavar="file",
72                       help=h)
73
74     (options, args) = parser.parse_args()
75
76     if len( args ) < 1: sys.exit("Module name is not specified")
77     main( args[0], options.output )
78     pass