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 newSources.append(source)
35 newSources.append(base+target_ext)
36 vtkSources.append(base+'.h')
37 vtkTargets[base+'.h']=newSources[-1]
39 newSources.append(source)
42 wrapper=self.FindvtkWrapPython()
43 if(not self.__extWrap.vtkHints):
44 self.__extWrap.vtkHints="toto"
47 for source in vtkSources:
48 target=vtkTargets[source]
49 distutil.announce("VTK wrapping %s to %s" % (source,target))
50 distutil.spawn([wrapper,source,self.__extWrap.vtkHints,target])
52 # Compilation of vtkWrapPythonInit
53 vtkWrapInit=self.__extWrap.vtkModule+"Init"+target_ext
54 distutil.announce("VTK init wrapping to %s" % vtkWrapInit)
55 self.WrapInit(vtkSources,vtkWrapInit)
56 newSources.append(vtkWrapInit)
60 def FindvtkWrapPython(self):
61 assert(os.path.isfile(self.__extWrap.vtkWrapper),
62 "Write an heuristic in FindvtkWrapPython")
63 return(self.__extWrap.vtkWrapper)
65 def WrapInit(self,vtkSource,target):
66 dllName=string.split(self.__extWrap.vtkModule,'.')[-1]
69 f.write('#include <string.h>\n')
70 f.write('#include "Python.h"\n\n')
73 src=os.path.split(src)[-1]
74 (src,_)=os.path.splitext(src)
75 f.write('extern "C" { ')
77 f.write('__declspec( dllexport ) ')
78 f.write('PyObject *PyVTKClass_%sNew(char *); }\n'% src)
81 f.write('\nstatic PyMethodDef Py%s_ClassMethods[] = {\n'% dllName)
82 f.write('{NULL, NULL}};\n\n')
84 f.write('extern "C" { ')
86 f.write('__declspec( dllexport ) ')
87 f.write('void init%s();}\n\n'% dllName)
89 f.write('void init%s()\n{\n'% dllName)
90 f.write(' PyObject *m, *d, *c;\n\n')
91 f.write(' static char modulename[] = "%s";\n'% dllName)
92 f.write(' m = Py_InitModule(modulename, Py%s_ClassMethods);\n'% dllName)
94 f.write(' d = PyModule_GetDict(m);\n')
95 f.write(' if (!d) Py_FatalError("can''t get dictionary for module %s!");\n\n'% dllName)
99 src=os.path.split(src)[-1]
100 (src,_)=os.path.splitext(src)
101 f.write(' if ((c = PyVTKClass_%sNew(modulename)))\n'% src)
102 f.write(' if (-1 == PyDict_SetItemString(d, "%s", c))\n'% src)
103 f.write(' Py_FatalError("can''t add class %s to dictionary!");\n\n'% src)
108 class VTKExtension(ExtensionWrap):
110 This class extends basic distutils Extension class, adding two keyword
112 * swig_cpp, which triggers -c++ mode when swigging
113 * swig_include, which specifies -I flag when swigging
114 This class is meant to be build with mybuild_ext distutils command.
116 def __init__(self,name,vtkHints=None,
117 vtkWrapper=None,**args):
118 ExtensionWrap.__init__(self,name=name,wrapper=VTKWrapper(),**args)
120 assert(type(name)==types.StringType,"vtk Module must be a string")
122 self.vtkHints=vtkHints
124 self.vtkWrapper=vtkWrapper