1 from distutilsWrapping import *
6 class VTKWrapper(Wrapper):
8 This distutils command is meant to be used with MyExtension extension, which
9 defines a swig_include attribute.
11 def WrapSources(self,distutil,extWrap,sources):
13 Walk the list of source files in 'sources', looking for VTK
14 interface (vtk*.cxx) files. Compile vtkWrapPythonInit.
15 Run vtkWrapPython on all that are found, and
16 return a modified 'sources' list with SWIG source files replaced
17 by the generated C (or C++) files.
21 self.__extWrap=extWrap
28 target_ext='Python.cxx'
30 for source in sources:
31 (base,ext)=os.path.splitext(source)
32 fileName=os.path.split(base)
33 if((ext==".cxx")and(fileName[-1][0:3]=="vtk")
34 and(fileName[-1][-6:]!="Python")):
35 newSources.append(source)
36 newSources.append(base+target_ext)
37 vtkSources.append(base+'.h')
38 vtkTargets[base+'.h']=newSources[-1]
40 newSources.append(source)
43 wrapper=self.FindvtkWrapPython()
44 if(not self.__extWrap.vtkHints):
45 self.__extWrap.vtkHints="dummyHints"
48 for source in vtkSources:
49 target=vtkTargets[source]
50 distutil.announce("VTK wrapping %s to %s" % (source,target))
51 distutil.spawn([wrapper,source,self.__extWrap.vtkHints,target])
53 # Compilation of vtkWrapPythonInit
54 vtkWrapInit=self.__extWrap.vtkModule+"Init"+target_ext
55 distutil.announce("VTK init wrapping to %s" % vtkWrapInit)
56 self.WrapInit(vtkSources,vtkWrapInit)
57 newSources.append(vtkWrapInit)
61 def FindvtkWrapPython(self):
62 assert(os.path.isfile(self.__extWrap.vtkWrapper),
63 "Write an heuristic in FindvtkWrapPython")
64 return(self.__extWrap.vtkWrapper)
66 def WrapInit(self,vtkSource,target):
67 dllName=string.split(self.__extWrap.vtkModule,'.')[-1]
70 f.write('#include <string.h>\n')
71 f.write('#include "Python.h"\n\n')
74 src=os.path.split(src)[-1]
75 (src,_)=os.path.splitext(src)
76 f.write('extern "C" { ')
78 f.write('__declspec( dllexport ) ')
79 f.write('PyObject *PyVTKClass_%sNew(char *); }\n'% src)
82 f.write('\nstatic PyMethodDef Py%s_ClassMethods[] = {\n'% dllName)
83 f.write('{NULL, NULL}};\n\n')
85 f.write('extern "C" { ')
87 f.write('__declspec( dllexport ) ')
88 f.write('void init%s();}\n\n'% dllName)
90 f.write('void init%s()\n{\n'% dllName)
91 f.write(' PyObject *m, *d, *c;\n\n')
92 f.write(' static char modulename[] = "%s";\n'% dllName)
93 f.write(' m = Py_InitModule(modulename, Py%s_ClassMethods);\n'% dllName)
95 f.write(' d = PyModule_GetDict(m);\n')
96 f.write(' if (!d) Py_FatalError("can''t get dictionary for module %s!");\n\n'% dllName)
100 src=os.path.split(src)[-1]
101 (src,_)=os.path.splitext(src)
102 f.write(' if ((c = PyVTKClass_%sNew(modulename)))\n'% src)
103 f.write(' if (-1 == PyDict_SetItemString(d, "%s", c))\n'% src)
104 f.write(' Py_FatalError("can''t add class %s to dictionary!");\n\n'% src)
109 class VTKExtension(ExtensionWrap):
111 This class extends basic distutils Extension class, adding two keyword
113 * swig_cpp, which triggers -c++ mode when swigging
114 * swig_include, which specifies -I flag when swigging
115 This class is meant to be build with mybuild_ext distutils command.
117 def __init__(self,name,vtkHints=None,
118 vtkWrapper=None,**args):
119 ExtensionWrap.__init__(self,name=name,wrapper=VTKWrapper(),**args)
121 assert(type(name)==types.StringType,"vtk Module must be a string")
123 self.vtkHints=vtkHints
125 self.vtkWrapper=vtkWrapper