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_util.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  2.7 KB  |  80 lines

  1. """General client side utilities.
  2.  
  3. This module contains utility functions, used primarily by advanced COM
  4. programmers, or other COM modules.
  5. """
  6. import pythoncom
  7. from win32com.client import Dispatch
  8.  
  9. PyIDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
  10.  
  11. def WrapEnum(ob, resultCLSID = None):
  12.     """Wrap an object in a VARIANT enumerator.  
  13.  
  14.     All VT_DISPATCHs returned by the enumerator are converted to wrapper objects
  15.     (which may be either a class instance, or a dynamic.Dispatch type object).
  16.  
  17.     """
  18.     if type(ob) != pythoncom.TypeIIDs[pythoncom.IID_IEnumVARIANT]:
  19.         ob = ob.QueryInterface(pythoncom.IID_IEnumVARIANT)
  20.     return EnumVARIANT(ob, resultCLSID)
  21.  
  22. class Enumerator:
  23.     """A class that provides indexed access into an Enumerator
  24.  
  25.     By wrapping a PyIEnum* object in this class, you can perform
  26.     natural looping and indexing into the Enumerator.
  27.  
  28.     Looping is very efficient, but it should be noted that although random 
  29.     access is supported, the underlying object is still an enumerator, so 
  30.     this will force many reset-and-seek operations to find the requested index.
  31.  
  32.     """
  33.     def __init__(self, enum):
  34.         self._oleobj_ = enum # a PyIEnumVARIANT
  35.         self.index = -1
  36.     def __getitem__(self, index):
  37.         return self.__GetIndex(index)
  38.     def __call__(self, index):
  39.         return self.__GetIndex(index)
  40.     
  41.     def __GetIndex(self, index):
  42.         if type(index)!=type(0): raise TypeError, "Only integer indexes are supported for enumerators"
  43.         # NOTE
  44.         # In this context, self.index is users purely as a flag to say 
  45.         # "am I still in sequence".  The user may call Next() or Reset() if they
  46.         # so choose, in which case self.index will not be correct (although we
  47.         # still want to stay in sequence)
  48.         if index != self.index + 1:
  49.             # Index requested out of sequence.
  50.             self._oleobj_.Reset()
  51.             if index: self._oleobj_.Skip(index) # if asked for item 1, must skip 1, Python always zero based.
  52.         self.index = index
  53.         result = self._oleobj_.Next(1)
  54.         if len(result):
  55.             return self._make_retval_(result[0])
  56.         raise IndexError, "list index out of range"
  57.     def Next(self, count=1):
  58.         ret = self._oleobj_.Next(count)
  59.         if ret is None: return None
  60.         realRets = []
  61.         for r in ret:
  62.             realRets.append(self._make_retval_(r))
  63.         return tuple(realRets) # Convert back to tuple.
  64.     def Reset(self):
  65.         return self._oleobj_.Reset()
  66.     def Clone(self):
  67.         return self.__class__( self._oleobj_.Clone(), self.resultCLSID)
  68.     def _make_retval_(self, result):
  69.         return result
  70.  
  71. class EnumVARIANT(Enumerator):
  72.     def __init__(self, enum, resultCLSID = None):
  73.         self.resultCLSID = resultCLSID
  74.         Enumerator.__init__(self, enum)
  75.     def _make_retval_(self, result):
  76.         if type(result)==PyIDispatchType:
  77.             result = Dispatch(result, resultCLSID = self.resultCLSID)
  78.         return result
  79.  
  80.