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_client_selecttlb.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  3.3 KB  |  117 lines

  1. """Utilities for selecting and enumerating the Type Libraries installed on the system
  2. """
  3.  
  4. import win32api, win32con, string
  5.  
  6. class TypelibSpec:
  7.     def __init__(self, clsid, lcid, major, minor, flags=0):
  8.         self.clsid = str(clsid)
  9.         self.lcid = int(lcid)
  10.         self.major = int(major)
  11.         self.minor = int(minor)
  12.         self.dll = None
  13.         self.desc = None
  14.         self.ver_desc = None
  15.         self.flags = flags
  16.     # For the SelectList
  17.     def __getitem__(self, item):
  18.         if item==0:
  19.             return self.ver_desc
  20.         raise IndexError, "Cant index me!"
  21.     def __cmp__(self, other):
  22.         rc = cmp(string.lower(self.ver_desc or ""), string.lower(other.ver_desc or ""))
  23.         if rc==0:
  24.             rc = cmp(string.lower(self.desc), string.lower(other.desc))
  25.         if rc==0:
  26.             rc = cmp(self.major, other.major)
  27.         if rc==0:
  28.             rc = cmp(self.major, other.minor)
  29.         return rc
  30.         
  31.     def FromTypelib(self, typelib, dllName = None):
  32.         la = typelib.GetLibAttr()
  33.         self.clsid = str(la[0])
  34.         self.lcid = la[1]
  35.         self.major = la[3]
  36.         self.minor = la[4]
  37.         if dllName:
  38.             self.dll = dllName
  39.  
  40. def EnumKeys(root):
  41.     index = 0
  42.     ret = []
  43.     while 1:
  44.         try:
  45.             item = win32api.RegEnumKey(root, index)
  46.         except win32api.error:
  47.             break
  48.         try:
  49.             val = win32api.RegQueryValue(root, item)
  50.         except win32api.error:
  51.             val = None
  52.             
  53.         ret.append((item, val))
  54.         index = index + 1
  55.     return ret
  56.  
  57. FLAG_RESTRICTED=1
  58. FLAG_CONTROL=2
  59. FLAG_HIDDEN=4
  60.  
  61. def EnumTlbs(excludeFlags = 0):
  62.     """Return a list of TypelibSpec objects, one for each registered library.
  63.     """
  64.     key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, "Typelib")
  65.     iids = EnumKeys(key)
  66.     results = []
  67.     for iid, crap in iids:
  68.         key2 = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, "Typelib\\%s" % (iid))
  69.         for version, tlbdesc in EnumKeys(key2):
  70.             key3 = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, "Typelib\\%s\\%s" % (iid, version))
  71.             try:
  72.                 # The "FLAGS" are at this point
  73.                 flags = int(win32api.RegQueryValue(key3, "FLAGS"))
  74.             except (win32api.error, ValueError):
  75.                 flags = 0
  76.             if flags & excludeFlags==0:
  77.                 for lcid, crap in EnumKeys(key3):
  78.                     key4 = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, "Typelib\\%s\\%s\\%s" % (iid, version, lcid))
  79.                     for platform, dll in EnumKeys(key4):
  80.                         if platform=="win32":
  81.                             major = string.split(version, '.', 1)
  82.                             if len(major) < 2:
  83.                                 major.append('0')
  84.                             major, minor = string.atoi(major[0], 16), string.atoi(major[1], 16)
  85.                             lcid = string.atoi(lcid,16)
  86.                             spec = TypelibSpec(iid, lcid, major, minor, flags)
  87.                             spec.desc = tlbdesc
  88.                             spec.ver_desc = tlbdesc + " (" + version + ")"
  89.                             spec.dll = dll
  90.                             results.append(spec)
  91.     return results
  92.  
  93. def FindTlbsWithDescription(desc):
  94.     """Find all installed type libraries with the specified description
  95.     """
  96.     ret = []
  97.     items = EnumTlbs()
  98.     for item in items:
  99.         if item.desc==desc:
  100.             ret.append(item)
  101.     return ret
  102.  
  103. def SelectTlb(title="Select Library", excludeFlags = 0):
  104.     """Display a list of all the type libraries, and select one.   Returns None if cancelled
  105.     """
  106.     import pywin.dialogs.list
  107.     items = EnumTlbs(excludeFlags)
  108.     items.sort()
  109.     rc = pywin.dialogs.list.SelectFromLists(title, items, ["Type Library"])
  110.     if rc is None:
  111.         return None
  112.     return items[rc]
  113.  
  114. # Test code.
  115. if __name__=='__main__':
  116.     print SelectTlb()
  117.