home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_win32com___init__.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  3.1 KB  |  100 lines

  1. #
  2. # Initialization for the win32com package
  3. #
  4.  
  5. import win32api, sys
  6.  
  7. # Add support for an external "COM Extensions" path.
  8. #  Concept is that you can register a seperate path to be used for
  9. #  COM extensions, outside of the win32com directory.  These modules, however,
  10. #  look identical to win32com built-in modules.
  11. #  This is the technique that we use for the "standard" COM extensions.
  12. #  eg "win32com.mapi" or "win32com.axscript" both work, even though they do not
  13. #  live under the main win32com directory.
  14.  
  15. ### TODO - Load _all_ \\Extensions subkeys - for now, we only read the default
  16. ### Modules will work if loaded into "win32comext" path.
  17.  
  18. def SetupEnvironment():
  19.     HKEY_LOCAL_MACHINE = 0x80000002 # Avoid pulling in win32con for just these...
  20.     KEY_QUERY_VALUE = 0x1
  21.     # Open the root key once, as this is quite slow on NT.
  22.     keyName = "SOFTWARE\\Python\\PythonCore\\%s\\PythonPath\\win32com" % sys.winver
  23.     try:
  24.         key = win32api.RegOpenKey(HKEY_LOCAL_MACHINE , keyName, 0, KEY_QUERY_VALUE)
  25.     except win32api.error:
  26.         key = None
  27.         
  28.     try:
  29.         found = 0
  30.         if key is not None:
  31.             try:
  32.                 __path__.append( win32api.RegQueryValue(key, "Extensions" ))
  33.                 found = 1
  34.             except win32api.error:
  35.                 # Nothing registered
  36.                 pass
  37.         if not found:
  38.             try:
  39.                 __path__.append( win32api.GetFullPathName( __path__[0] + "\\..\\win32comext") )
  40.             except win32api.error:
  41.                 # Give up in disgust!
  42.                 pass
  43.     
  44.         # For the sake of developers, we also look up a "BuildPath" key
  45.         # If extension modules add support, we can load their .pyd's from a completely
  46.         # different directory (see the comments below)
  47.         try:
  48.             if key is not None:
  49.                 global __build_path__
  50.                 __build_path__ = win32api.RegQueryValue(key, "BuildPath")
  51.                 __path__.append(__build_path__)
  52.         except win32api.error:
  53.             # __build_path__ neednt be defined.
  54.             pass
  55.             
  56.         found = 0
  57.         global __gen_path__
  58.         try:
  59.             if key is not None:
  60.                 __gen_path__ = win32api.RegQueryValue(key, "GenPath")
  61.                 found = 1
  62.                 # Import a special module, Otherwise it is already all setup for us.
  63.                 import new
  64.                 global gen_py # Exists in the win32com namespace.
  65.                 gen_py = new.module("win32com.gen_py")
  66.                 gen_py.__path__ = [ __gen_path__ ]
  67.                 sys.modules[gen_py.__name__]=gen_py
  68.                 
  69.         except win32api.error:
  70.             found = 0
  71.         
  72.         if not found:
  73.             __gen_path__ = win32api.GetFullPathName( __path__[0] + "\\gen_py")
  74.     finally:
  75.         if key is not None:
  76.             key.Close()
  77.  
  78. # A Helper for developers.  A sub-package's __init__ can call this help function,
  79. # which allows the .pyd files for the extension to live in a special "Build" directory
  80. # (which the win32com developers do!)
  81. def __PackageSupportBuildPath__(package_path):
  82.     # See if we have a special directory for the binaries (for developers)
  83.     try:
  84.         package_path.append(__build_path__)
  85.     except (NameError, AttributeError):
  86.         # AttributeError may be raised in a frozen EXE.
  87.         pass
  88.  
  89. try:
  90.     __path__.append # NOT a method call - check a list!
  91.     ok = 1
  92. except (NameError,AttributeError):
  93.     ok = 0
  94. if ok:
  95.     SetupEnvironment()
  96. del ok
  97.  
  98. # get rid of these for module users
  99. del sys, win32api
  100.