util.py 2.9 KB

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