3 from types import FunctionType
6 ORIGIN_MODULE_SUFFIX = "_origin"
7 DYNAMIC_MODULE_SUFFIX = "_dynamic"
10 def main(module_name, output_file = "smeshBuilder.py"):
11 oringin_module_name = module_name + ORIGIN_MODULE_SUFFIX
12 dynamic_module_name = module_name + DYNAMIC_MODULE_SUFFIX
14 exec( "import %s" % oringin_module_name )
15 origin_module = locals()[ oringin_module_name ]
16 origin_module_lines = inspect.getsourcelines( origin_module )[0]
17 origin_meshClass_lines = inspect.getsourcelines(origin_module.Mesh)[0]
18 origin_module_text = "".join( origin_module_lines )
19 origin_meshClass_text = "".join( origin_meshClass_lines )
21 exec( "import %s" % dynamic_module_name )
22 dynanmic_module = locals()[ dynamic_module_name ]
23 dynanmic_meshClass = dynanmic_module.Mesh
25 new_meshClass_lines = copy.copy(origin_meshClass_lines)
26 # remove end of class 'pass'
27 if new_meshClass_lines[-1].find("pass") > 0:
28 new_meshClass_lines.pop()
30 dynanmic_meshClass_methods = [x for x, y in dynanmic_meshClass.__dict__.items() if type(y) == FunctionType]
31 for method in dynanmic_meshClass_methods:
32 exec( "method_lines = inspect.getsourcelines(dynanmic_module.Mesh.%s)[0]" % method)
33 new_meshClass_lines+=locals()['method_lines']
35 new_meshClass_text = "".join( new_meshClass_lines )
37 f = open( output_file, "w" )
39 f.write( origin_module_text.replace( origin_meshClass_text, new_meshClass_text) )
41 except Exception as e:
47 if __name__ == "__main__":
49 parser = optparse.OptionParser(usage="%prog [options] modulename")
50 h = "Output file (smeshBuilder.py by default)"
51 parser.add_option("-o", "--output", dest="output",
52 action="store", default="smeshBuilder.py", metavar="file",
55 (options, args) = parser.parse_args()
57 if len( args ) < 1: sys.exit("Module name is not specified")
58 main( args[0], options.output )