home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Shareware / Comunicatii / jyte / jyte.exe / selecttlb.py < prev    next >
Text File  |  2003-04-21  |  4KB  |  147 lines

  1. """Utilities for selecting and enumerating the Type Libraries installed on the system
  2. """
  3.  
  4. import win32api, win32con, string, pythoncom
  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 Resolve(self):
  32.         if self.dll is None:
  33.             return 0
  34.         tlb = pythoncom.LoadTypeLib(self.dll)
  35.         self.FromTypelib(tlb, None)
  36.         return 1
  37.  
  38.     def FromTypelib(self, typelib, dllName = None):
  39.         la = typelib.GetLibAttr()
  40.         self.clsid = str(la[0])
  41.         self.lcid = la[1]
  42.         self.major = la[3]
  43.         self.minor = la[4]
  44.         if dllName:
  45.             self.dll = dllName
  46.  
  47. def EnumKeys(root):
  48.     index = 0
  49.     ret = []
  50.     while 1:
  51.         try:
  52.             item = win32api.RegEnumKey(root, index)
  53.         except win32api.error:
  54.             break
  55.         try:
  56.             val = win32api.RegQueryValue(root, item)
  57.         except win32api.error:
  58.             val = None
  59.             
  60.         ret.append((item, val))
  61.         index = index + 1
  62.     return ret
  63.  
  64. FLAG_RESTRICTED=1
  65. FLAG_CONTROL=2
  66. FLAG_HIDDEN=4
  67.  
  68. def EnumTlbs(excludeFlags = 0):
  69.     """Return a list of TypelibSpec objects, one for each registered library.
  70.     """
  71.     key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, "Typelib")
  72.     iids = EnumKeys(key)
  73.     results = []
  74.     for iid, crap in iids:
  75.         try:
  76.             key2 = win32api.RegOpenKey(key, str(iid))
  77.         except win32api.error:
  78.             # A few good reasons for this, including "access denied".
  79.             continue
  80.         for version, tlbdesc in EnumKeys(key2):
  81.             major_minor = string.split(version, '.', 1)
  82.             if len(major_minor) < 2:
  83.                 major_minor.append('0')
  84.             try:
  85.                 # For some reason, this code used to assume the values were hex.
  86.                 # This seems to not be true - particularly for CDO 1.21
  87.                 # *sigh* - it appears there are no rules here at all, so when we need
  88.                 # to know the info, we must load the tlb by filename and request it.
  89.                 # The Resolve() method on the TypelibSpec does this.
  90.                 major = int(major_minor[0])
  91.                 minor = int(major_minor[1])
  92.             except ValueError: # crap in the registry!
  93.                 continue
  94.             
  95.             key3 = win32api.RegOpenKey(key2, str(version))
  96.             try:
  97.                 # The "FLAGS" are at this point
  98.                 flags = int(win32api.RegQueryValue(key3, "FLAGS"))
  99.             except (win32api.error, ValueError):
  100.                 flags = 0
  101.             if flags & excludeFlags==0:
  102.                 for lcid, crap in EnumKeys(key3):
  103.                     try:
  104.                         lcid = int(lcid)
  105.                     except ValueError: # crap in the registry!
  106.                         continue
  107.                     # Only care about "{lcid}\win32" key - jump straight there.
  108.                     try:
  109.                         key4 = win32api.RegOpenKey(key3, "%s\win32" % (lcid,))
  110.                     except win32api.error:
  111.                         continue
  112.                     try:
  113.                         dll = win32api.RegQueryValue(key4, None)
  114.                     except win32api.error:
  115.                         dll = None
  116.                     spec = TypelibSpec(iid, lcid, major, minor, flags)
  117.                     spec.dll = dll
  118.                     spec.desc = tlbdesc
  119.                     spec.ver_desc = tlbdesc + " (" + version + ")"
  120.                     results.append(spec)
  121.     return results
  122.  
  123. def FindTlbsWithDescription(desc):
  124.     """Find all installed type libraries with the specified description
  125.     """
  126.     ret = []
  127.     items = EnumTlbs()
  128.     for item in items:
  129.         if item.desc==desc:
  130.             ret.append(item)
  131.     return ret
  132.  
  133. def SelectTlb(title="Select Library", excludeFlags = 0):
  134.     """Display a list of all the type libraries, and select one.   Returns None if cancelled
  135.     """
  136.     import pywin.dialogs.list
  137.     items = EnumTlbs(excludeFlags)
  138.     items.sort()
  139.     rc = pywin.dialogs.list.SelectFromLists(title, items, ["Type Library"])
  140.     if rc is None:
  141.         return None
  142.     return items[rc]
  143.  
  144. # Test code.
  145. if __name__=='__main__':
  146.     print SelectTlb().__dict__
  147.