]> Creatis software - gdcm.git/blob - gdcmPython/SetupOldies/WrapVTK.py
Temporay commenting out of Test JPEG2000 to avoid time out everywhere!
[gdcm.git] / gdcmPython / SetupOldies / WrapVTK.py
1 from distutilsWrapping import *
2 import types
3 import string
4 import os
5
6 class VTKWrapper(Wrapper):
7         """
8         This distutils command is meant to be used with MyExtension extension, which
9         defines a swig_include attribute.
10         """
11         def WrapSources(self,distutil,extWrap,sources):
12                 """
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.
18                 
19                 FIXME nierk
20                 """
21                 self.__extWrap=extWrap
22
23                 newSources=[]
24                 vtkSources=[]
25                 vtkTargets={}
26
27                 # Wrapping of sources
28                 target_ext='Python.cxx'
29
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]
39                         else:
40                                 newSources.append(source)
41
42                 # Find vtkWrapPython
43                 wrapper=self.FindvtkWrapPython()
44                 if(not self.__extWrap.vtkHints):
45                         self.__extWrap.vtkHints="dummyHints"
46
47                 wrapCmd=[wrapper]
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])
52
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)
58
59                 return newSources
60
61         def FindvtkWrapPython(self):
62                 assert(os.path.isfile(self.__extWrap.vtkWrapper),
63                        "Write an heuristic in FindvtkWrapPython")
64                 return(self.__extWrap.vtkWrapper)
65
66         def WrapInit(self,vtkSource,target):
67                 dllName=string.split(self.__extWrap.vtkModule,'.')[-1]
68                 f=open(target,"w")
69
70                 f.write('#include <string.h>\n')
71                 f.write('#include "Python.h"\n\n')
72
73                 for src in vtkSource:
74                         src=os.path.split(src)[-1]
75                         (src,_)=os.path.splitext(src)
76                         f.write('extern "C" { ')
77                         if(os.name!="posix"):
78                                 f.write('__declspec( dllexport ) ')
79                         f.write('PyObject *PyVTKClass_%sNew(char *); }\n'% src)
80
81                 # Lib Init
82                 f.write('\nstatic PyMethodDef Py%s_ClassMethods[] = {\n'% dllName)
83                 f.write('{NULL, NULL}};\n\n')
84
85                 f.write('extern "C" { ')
86                 if(os.name!="posix"):
87                         f.write('__declspec( dllexport ) ')
88                 f.write('void init%s();}\n\n'% dllName)
89
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)
94
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)
97
98                 # New function
99                 for src in vtkSource:
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)
105                 f.write('}\n\n')
106
107                 f.close()
108
109 class VTKExtension(ExtensionWrap):
110         """
111         This class extends basic distutils Extension class, adding two keyword
112         arguments :
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.
116         """
117         def __init__(self,name,vtkHints=None,
118                      vtkWrapper=None,**args):
119                 ExtensionWrap.__init__(self,name=name,wrapper=VTKWrapper(),**args)
120
121                 assert(type(name)==types.StringType,"vtk Module must be a string")
122
123                 self.vtkHints=vtkHints
124                 self.vtkModule=name
125                 self.vtkWrapper=vtkWrapper
126