0001 """General client side utilities. 0002 0003 This module contains utility functions, used primarily by advanced COM 0004 programmers, or other COM modules. 0005 """ 0006 import pythoncom 0007 from win32com.client import Dispatch, _get_good_object_ 0008 0009 PyIDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch] 0010 0011 def WrapEnum(ob, resultCLSID = None): 0012 """Wrap an object in a VARIANT enumerator. 0013 0014 All VT_DISPATCHs returned by the enumerator are converted to wrapper objects 0015 (which may be either a class instance, or a dynamic.Dispatch type object). 0016 0017 """ 0018 if type(ob) != pythoncom.TypeIIDs[pythoncom.IID_IEnumVARIANT]: 0019 ob = ob.QueryInterface(pythoncom.IID_IEnumVARIANT) 0020 return EnumVARIANT(ob, resultCLSID) 0021 0022 class Enumerator: 0023 """A class that provides indexed access into an Enumerator 0024 0025 By wrapping a PyIEnum* object in this class, you can perform 0026 natural looping and indexing into the Enumerator. 0027 0028 Looping is very efficient, but it should be noted that although random 0029 access is supported, the underlying object is still an enumerator, so 0030 this will force many reset-and-seek operations to find the requested index. 0031 0032 """ 0033 def __init__(self, enum): 0034 self._oleobj_ = enum # a PyIEnumVARIANT 0035 self.index = -1 0036 def __getitem__(self, index): 0037 return self.__GetIndex(index) 0038 def __call__(self, index): 0039 return self.__GetIndex(index) 0040 0041 def __GetIndex(self, index): 0042 if type(index)!=type(0): raise TypeError, "Only integer indexes are supported for enumerators" 0043 # NOTE 0044 # In this context, self.index is users purely as a flag to say 0045 # "am I still in sequence". The user may call Next() or Reset() if they 0046 # so choose, in which case self.index will not be correct (although we 0047 # still want to stay in sequence) 0048 if index != self.index + 1: 0049 # Index requested out of sequence. 0050 self._oleobj_.Reset() 0051 if index: self._oleobj_.Skip(index) # if asked for item 1, must skip 1, Python always zero based. 0052 self.index = index 0053 result = self._oleobj_.Next(1) 0054 if len(result): 0055 return self._make_retval_(result[0]) 0056 raise IndexError, "list index out of range" 0057 def Next(self, count=1): 0058 ret = self._oleobj_.Next(count) 0059 realRets = [] 0060 for r in ret: 0061 realRets.append(self._make_retval_(r)) 0062 return tuple(realRets) # Convert back to tuple. 0063 def Reset(self): 0064 return self._oleobj_.Reset() 0065 def Clone(self): 0066 return self.__class__( self._oleobj_.Clone(), self.resultCLSID) 0067 def _make_retval_(self, result): 0068 return result 0069 0070 class EnumVARIANT(Enumerator): 0071 def __init__(self, enum, resultCLSID = None): 0072 self.resultCLSID = resultCLSID 0073 Enumerator.__init__(self, enum) 0074 def _make_retval_(self, result): 0075 return _get_good_object_(result, resultCLSID = self.resultCLSID) 0076 0077 class Iterator: 0078 def __init__(self, enum): 0079 self._iter_ = iter(enum.QueryInterface(pythoncom.IID_IEnumVARIANT)) 0080 def __iter__(self): 0081 return self 0082 def next(self): 0083 return _get_good_object_(self._iter_.next()) 0084
Generated by PyXR 0.9.4