codekingpro/portable-devtools
114k
1#2# Initialization for the win32com package3#4 5import os6import sys7 8import pythoncom9import win32api10 11# flag if we are in a "frozen" build.12_frozen = getattr(sys, "frozen", 1 == 0)13# pythoncom dumbly defaults this to zero - we believe sys.frozen over it.14if _frozen and not getattr(pythoncom, "frozen", 0):15 pythoncom.frozen = sys.frozen16 17# Add support for an external "COM Extensions" path.18# Concept is that you can register a seperate path to be used for19# COM extensions, outside of the win32com directory. These modules, however,20# look identical to win32com built-in modules.21# This is the technique that we use for the "standard" COM extensions.22# eg "win32com.mapi" or "win32com.axscript" both work, even though they do not23# live under the main win32com directory.24__gen_path__ = ""25__build_path__ = None26### TODO - Load _all_ \\Extensions subkeys - for now, we only read the default27### Modules will work if loaded into "win32comext" path.28 29 30def SetupEnvironment():31 HKEY_LOCAL_MACHINE = -2147483646 # Avoid pulling in win32con for just these...32 KEY_QUERY_VALUE = 0x133 # Open the root key once, as this is quite slow on NT.34 try:35 keyName = "SOFTWARE\\Python\\PythonCore\\%s\\PythonPath\\win32com" % sys.winver36 key = win32api.RegOpenKey(HKEY_LOCAL_MACHINE, keyName, 0, KEY_QUERY_VALUE)37 except (win32api.error, AttributeError):38 key = None39 40 try:41 found = 042 if key is not None:43 try:44 __path__.append(win32api.RegQueryValue(key, "Extensions"))45 found = 146 except win32api.error:47 # Nothing registered48 pass49 if not found:50 try:51 __path__.append(52 win32api.GetFullPathName(__path__[0] + "\\..\\win32comext")53 )54 except win32api.error:55 # Give up in disgust!56 pass57 58 # For the sake of developers, we also look up a "BuildPath" key59 # If extension modules add support, we can load their .pyd's from a completely60 # different directory (see the comments below)61 try:62 if key is not None:63 global __build_path__64 __build_path__ = win32api.RegQueryValue(key, "BuildPath")65 __path__.append(__build_path__)66 except win32api.error:67 # __build_path__ neednt be defined.68 pass69 global __gen_path__70 if key is not None:71 try:72 __gen_path__ = win32api.RegQueryValue(key, "GenPath")73 except win32api.error:74 pass75 finally:76 if key is not None:77 key.Close()78 79 80# A Helper for developers. A sub-package's __init__ can call this help function,81# which allows the .pyd files for the extension to live in a special "Build" directory82# (which the win32com developers do!)83def __PackageSupportBuildPath__(package_path):84 # See if we have a special directory for the binaries (for developers)85 if not _frozen and __build_path__:86 package_path.append(__build_path__)87 88 89if not _frozen:90 SetupEnvironment()91 92# If we don't have a special __gen_path__, see if we have a gen_py as a93# normal module and use that (ie, "win32com.gen_py" may already exist as94# a package.95if not __gen_path__:96 try:97 import win32com.gen_py98 99 # hrmph - 3.3 throws: TypeError: '_NamespacePath' object does not support indexing100 # attempting to get __path__[0] - but I can't quickly repro this stand-alone.101 # Work around it by using an iterator.102 __gen_path__ = next(iter(sys.modules["win32com.gen_py"].__path__))103 except ImportError:104 # If a win32com\gen_py directory already exists, then we use it105 # (gencache doesn't insist it have an __init__, but our __import__106 # above does!107 __gen_path__ = os.path.abspath(os.path.join(__path__[0], "gen_py"))108 if not os.path.isdir(__gen_path__):109 # We used to dynamically create a directory under win32com -110 # but this sucks. If the dir doesn't already exist, we we111 # create a version specific directory under the user temp112 # directory.113 __gen_path__ = os.path.join(114 win32api.GetTempPath(),115 "gen_py",116 "%d.%d" % (sys.version_info[0], sys.version_info[1]),117 )118 119# we must have a __gen_path__, but may not have a gen_py module -120# set that up.121if "win32com.gen_py" not in sys.modules:122 # Create a "win32com.gen_py", but with a custom __path__123 import types124 125 gen_py = types.ModuleType("win32com.gen_py")126 gen_py.__path__ = [__gen_path__]127 sys.modules[gen_py.__name__] = gen_py128 del types129gen_py = sys.modules["win32com.gen_py"]130 131# get rid of these for module users132del os, sys, win32api, pythoncom133 