PyXR

c:\python24\lib\site-packages\win32 \ com \ client \ selecttlb.py



0001 """Utilities for selecting and enumerating the Type Libraries installed on the system
0002 """
0003 
0004 import win32api, win32con, string, pythoncom
0005 
0006 class TypelibSpec:
0007         def __init__(self, clsid, lcid, major, minor, flags=0):
0008                 self.clsid = str(clsid)
0009                 self.lcid = int(lcid)
0010                 self.major = int(major)
0011                 self.minor = int(minor)
0012                 self.dll = None
0013                 self.desc = None
0014                 self.ver_desc = None
0015                 self.flags = flags
0016         # For the SelectList
0017         def __getitem__(self, item):
0018                 if item==0:
0019                         return self.ver_desc
0020                 raise IndexError, "Cant index me!"
0021         def __cmp__(self, other):
0022                 rc = cmp(string.lower(self.ver_desc or ""), string.lower(other.ver_desc or ""))
0023                 if rc==0:
0024                         rc = cmp(string.lower(self.desc), string.lower(other.desc))
0025                 if rc==0:
0026                         rc = cmp(self.major, other.major)
0027                 if rc==0:
0028                         rc = cmp(self.major, other.minor)
0029                 return rc
0030 
0031         def Resolve(self):
0032                 if self.dll is None:
0033                         return 0
0034                 tlb = pythoncom.LoadTypeLib(self.dll)
0035                 self.FromTypelib(tlb, None)
0036                 return 1
0037 
0038         def FromTypelib(self, typelib, dllName = None):
0039                 la = typelib.GetLibAttr()
0040                 self.clsid = str(la[0])
0041                 self.lcid = la[1]
0042                 self.major = la[3]
0043                 self.minor = la[4]
0044                 if dllName:
0045                         self.dll = dllName
0046 
0047 def EnumKeys(root):
0048         index = 0
0049         ret = []
0050         while 1:
0051                 try:
0052                         item = win32api.RegEnumKey(root, index)
0053                 except win32api.error:
0054                         break
0055                 try:
0056                         # Note this doesn't handle REG_EXPAND_SZ, but the implementation
0057                         # here doesn't need to - that is handled as the data is read.
0058                         val = win32api.RegQueryValue(root, item)
0059                 except win32api.error:
0060                         val = "" # code using this assumes a string.
0061                         
0062                 ret.append((item, val))
0063                 index = index + 1
0064         return ret
0065 
0066 FLAG_RESTRICTED=1
0067 FLAG_CONTROL=2
0068 FLAG_HIDDEN=4
0069 
0070 def EnumTlbs(excludeFlags = 0):
0071         """Return a list of TypelibSpec objects, one for each registered library.
0072         """
0073         key = win32api.RegOpenKey(win32con.HKEY_CLASSES_ROOT, "Typelib")
0074         iids = EnumKeys(key)
0075         results = []
0076         for iid, crap in iids:
0077                 try:
0078                         key2 = win32api.RegOpenKey(key, str(iid))
0079                 except win32api.error:
0080                         # A few good reasons for this, including "access denied".
0081                         continue
0082                 for version, tlbdesc in EnumKeys(key2):
0083                         major_minor = string.split(version, '.', 1)
0084                         if len(major_minor) < 2:
0085                                 major_minor.append('0')
0086                         try:
0087                                 # For some reason, this code used to assume the values were hex.
0088                                 # This seems to not be true - particularly for CDO 1.21
0089                                 # *sigh* - it appears there are no rules here at all, so when we need
0090                                 # to know the info, we must load the tlb by filename and request it.
0091                                 # The Resolve() method on the TypelibSpec does this.
0092                                 major = int(major_minor[0])
0093                                 minor = int(major_minor[1])
0094                         except ValueError: # crap in the registry!
0095                                 continue
0096                         
0097                         key3 = win32api.RegOpenKey(key2, str(version))
0098                         try:
0099                                 # The "FLAGS" are at this point
0100                                 flags = int(win32api.RegQueryValue(key3, "FLAGS"))
0101                         except (win32api.error, ValueError):
0102                                 flags = 0
0103                         if flags & excludeFlags==0:
0104                                 for lcid, crap in EnumKeys(key3):
0105                                         try:
0106                                                 lcid = int(lcid)
0107                                         except ValueError: # not an LCID entry
0108                                                 continue
0109                                         # Only care about "{lcid}\win32" key - jump straight there.
0110                                         try:
0111                                                 key4 = win32api.RegOpenKey(key3, "%s\\win32" % (lcid,))
0112                                         except win32api.error:
0113                                                 continue
0114                                         try:
0115                                                 dll, typ = win32api.RegQueryValueEx(key4, None)
0116                                                 if typ==win32con.REG_EXPAND_SZ:
0117                                                         dll = win32api.ExpandEnvironmentStrings(dll)
0118                                         except win32api.error:
0119                                                 dll = None
0120                                         spec = TypelibSpec(iid, lcid, major, minor, flags)
0121                                         spec.dll = dll
0122                                         spec.desc = tlbdesc
0123                                         spec.ver_desc = tlbdesc + " (" + version + ")"
0124                                         results.append(spec)
0125         return results
0126 
0127 def FindTlbsWithDescription(desc):
0128         """Find all installed type libraries with the specified description
0129         """
0130         ret = []
0131         items = EnumTlbs()
0132         for item in items:
0133                 if item.desc==desc:
0134                         ret.append(item)
0135         return ret
0136 
0137 def SelectTlb(title="Select Library", excludeFlags = 0):
0138         """Display a list of all the type libraries, and select one.   Returns None if cancelled
0139         """
0140         import pywin.dialogs.list
0141         items = EnumTlbs(excludeFlags)
0142         items.sort()
0143         rc = pywin.dialogs.list.SelectFromLists(title, items, ["Type Library"])
0144         if rc is None:
0145                 return None
0146         return items[rc]
0147 
0148 # Test code.
0149 if __name__=='__main__':
0150         print SelectTlb().__dict__
0151 

Generated by PyXR 0.9.4
SourceForge.net Logo