__init__.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #
  2. # Initialization for the win32com package
  3. #
  4. import win32api, sys, os
  5. import pythoncom
  6. # flag if we are in a "frozen" build.
  7. _frozen = getattr(sys, "frozen", 1==0)
  8. # pythoncom dumbly defaults this to zero - we believe sys.frozen over it.
  9. if _frozen and not getattr(pythoncom, "frozen", 0):
  10. pythoncom.frozen = sys.frozen
  11. # Add support for an external "COM Extensions" path.
  12. # Concept is that you can register a seperate path to be used for
  13. # COM extensions, outside of the win32com directory. These modules, however,
  14. # look identical to win32com built-in modules.
  15. # This is the technique that we use for the "standard" COM extensions.
  16. # eg "win32com.mapi" or "win32com.axscript" both work, even though they do not
  17. # live under the main win32com directory.
  18. __gen_path__ = ''
  19. __build_path__ = None
  20. ### TODO - Load _all_ \\Extensions subkeys - for now, we only read the default
  21. ### Modules will work if loaded into "win32comext" path.
  22. def SetupEnvironment():
  23. HKEY_LOCAL_MACHINE = -2147483646 # Avoid pulling in win32con for just these...
  24. KEY_QUERY_VALUE = 0x1
  25. # Open the root key once, as this is quite slow on NT.
  26. try:
  27. keyName = "SOFTWARE\\Python\\PythonCore\\%s\\PythonPath\\win32com" % sys.winver
  28. key = win32api.RegOpenKey(HKEY_LOCAL_MACHINE , keyName, 0, KEY_QUERY_VALUE)
  29. except (win32api.error, AttributeError):
  30. key = None
  31. try:
  32. found = 0
  33. if key is not None:
  34. try:
  35. __path__.append( win32api.RegQueryValue(key, "Extensions" ))
  36. found = 1
  37. except win32api.error:
  38. # Nothing registered
  39. pass
  40. if not found:
  41. try:
  42. __path__.append( win32api.GetFullPathName( __path__[0] + "\\..\\win32comext") )
  43. except win32api.error:
  44. # Give up in disgust!
  45. pass
  46. # For the sake of developers, we also look up a "BuildPath" key
  47. # If extension modules add support, we can load their .pyd's from a completely
  48. # different directory (see the comments below)
  49. try:
  50. if key is not None:
  51. global __build_path__
  52. __build_path__ = win32api.RegQueryValue(key, "BuildPath")
  53. __path__.append(__build_path__)
  54. except win32api.error:
  55. # __build_path__ neednt be defined.
  56. pass
  57. global __gen_path__
  58. if key is not None:
  59. try:
  60. __gen_path__ = win32api.RegQueryValue(key, "GenPath")
  61. except win32api.error:
  62. pass
  63. finally:
  64. if key is not None:
  65. key.Close()
  66. # A Helper for developers. A sub-package's __init__ can call this help function,
  67. # which allows the .pyd files for the extension to live in a special "Build" directory
  68. # (which the win32com developers do!)
  69. def __PackageSupportBuildPath__(package_path):
  70. # See if we have a special directory for the binaries (for developers)
  71. if not _frozen and __build_path__:
  72. package_path.append(__build_path__)
  73. if not _frozen:
  74. SetupEnvironment()
  75. # If we don't have a special __gen_path__, see if we have a gen_py as a
  76. # normal module and use that (ie, "win32com.gen_py" may already exist as
  77. # a package.
  78. if not __gen_path__:
  79. try:
  80. import win32com.gen_py
  81. # hrmph - 3.3 throws: TypeError: '_NamespacePath' object does not support indexing
  82. # attempting to get __path__[0] - but I can't quickly repro this stand-alone.
  83. # Work around it by using an iterator.
  84. __gen_path__ = next(iter(sys.modules["win32com.gen_py"].__path__))
  85. except ImportError:
  86. # If a win32com\gen_py directory already exists, then we use it
  87. # (gencache doesn't insist it have an __init__, but our __import__
  88. # above does!
  89. __gen_path__ = os.path.abspath(os.path.join(__path__[0], "gen_py"))
  90. if not os.path.isdir(__gen_path__):
  91. # We used to dynamically create a directory under win32com -
  92. # but this sucks. If the dir doesn't already exist, we we
  93. # create a version specific directory under the user temp
  94. # directory.
  95. __gen_path__ = os.path.join(
  96. win32api.GetTempPath(), "gen_py",
  97. "%d.%d" % (sys.version_info[0], sys.version_info[1]))
  98. # we must have a __gen_path__, but may not have a gen_py module -
  99. # set that up.
  100. if "win32com.gen_py" not in sys.modules:
  101. # Create a "win32com.gen_py", but with a custom __path__
  102. import types
  103. gen_py = types.ModuleType("win32com.gen_py")
  104. gen_py.__path__ = [ __gen_path__ ]
  105. sys.modules[gen_py.__name__] = gen_py
  106. del types
  107. gen_py = sys.modules["win32com.gen_py"]
  108. # get rid of these for module users
  109. del os, sys, win32api, pythoncom