vtkLoadPythonTkWidgets.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import sys, os
  2. from vtkmodules.vtkCommonCore import vtkVersion
  3. def vtkLoadPythonTkWidgets(interp):
  4. """vtkLoadPythonTkWidgets(interp) -- load vtk-tk widget extensions
  5. This is a mess of mixed python and tcl code that searches for the
  6. shared object file that contains the python-vtk-tk widgets. Both
  7. the python path and the tcl path are searched.
  8. """
  9. X = vtkVersion.GetVTKMajorVersion()
  10. Y = vtkVersion.GetVTKMinorVersion()
  11. modname = 'vtkRenderingTk'
  12. name = '%s-%d.%d' % (modname,X,Y)
  13. pkgname = modname.lower().capitalize()
  14. # find out if the module is already loaded
  15. loadedpkgs = interp.call('info', 'loaded')
  16. found = False
  17. try:
  18. # check for result returned as a string
  19. found = (loadedpkgs.find(pkgname) >= 0)
  20. except AttributeError:
  21. # check for result returned as nested tuples
  22. for pkgtuple in loadedpkgs:
  23. found |= (pkgname in pkgtuple)
  24. if found:
  25. return
  26. # create the platform-dependent file name
  27. prefix = ''
  28. if sys.platform == 'cygwin':
  29. prefix = 'cyg'
  30. elif os.name == 'posix':
  31. prefix = 'lib'
  32. extension = interp.call('info', 'sharedlibextension')
  33. filename = prefix+name+extension
  34. # create an extensive list of paths to search
  35. pathlist = [os.path.join(p, 'vtkmodules') for p in sys.path]
  36. # add tcl paths, ensure that {} is handled properly
  37. try:
  38. auto_paths = interp.getvar('auto_path').split()
  39. except AttributeError:
  40. auto_paths = interp.getvar('auto_path')
  41. for path in auto_paths:
  42. prev = str(pathlist[-1])
  43. try:
  44. # try block needed when one uses Gordon McMillan's Python
  45. # Installer.
  46. if len(prev) > 0 and prev[0] == '{' and prev[-1] != '}':
  47. pathlist[-1] = prev+' '+path
  48. else:
  49. pathlist.append(path)
  50. except AttributeError:
  51. pass
  52. # a common place for these sorts of things
  53. if os.name == 'posix':
  54. pathlist.append('/usr/local/lib')
  55. # if python 3, there is no separate "unicode" type
  56. if sys.hexversion >= 0x03000000:
  57. unicode = str
  58. else:
  59. unicode = sys.modules['__builtin__'].unicode
  60. # attempt to load
  61. for path in pathlist:
  62. try:
  63. # If the path object is not str, it means that it is a
  64. # Tkinter path object.
  65. if (not isinstance(path, str) and not isinstance(path, unicode)):
  66. path = path.string
  67. # try block needed when one uses Gordon McMillan's Python
  68. # Installer.
  69. if len(path) > 0 and path[0] == '{' and path[-1] == '}':
  70. path = path[1:-1]
  71. fullpath = os.path.join(path, filename)
  72. except AttributeError:
  73. pass
  74. if ' ' in fullpath:
  75. fullpath = '{'+fullpath+'}'
  76. if interp.eval('catch {load '+fullpath+' '+pkgname+'}') == '0':
  77. return
  78. # re-generate the error
  79. interp.call('load', filename, pkgname)