__init__.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import win32com
  2. import win32com.client
  3. if type(__path__)==type(''):
  4. # For freeze to work!
  5. import sys
  6. try:
  7. import adsi
  8. sys.modules['win32com.adsi.adsi'] = adsi
  9. except ImportError:
  10. pass
  11. else:
  12. # See if we have a special directory for the binaries (for developers)
  13. win32com.__PackageSupportBuildPath__(__path__)
  14. # Some helpers
  15. # We want to _look_ like the ADSI module, but provide some additional
  16. # helpers.
  17. # Of specific note - most of the interfaces supported by ADSI
  18. # derive from IDispatch - thus, you get the custome methods from the
  19. # interface, as well as via IDispatch.
  20. import pythoncom
  21. from .adsi import *
  22. LCID = 0
  23. IDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
  24. IADsContainerType = pythoncom.TypeIIDs[adsi.IID_IADsContainer]
  25. def _get_good_ret(ob,
  26. # Named arguments used internally
  27. resultCLSID = None):
  28. assert resultCLSID is None, "Now have type info for ADSI objects - fix me!"
  29. # See if the object supports IDispatch
  30. if hasattr(ob, "Invoke"):
  31. import win32com.client.dynamic
  32. name = "Dispatch wrapper around %r" % ob
  33. return win32com.client.dynamic.Dispatch(ob, name, ADSIDispatch)
  34. return ob
  35. class ADSIEnumerator:
  36. def __init__(self, ob):
  37. # Query the object for the container interface.
  38. self._cont_ = ob.QueryInterface(IID_IADsContainer)
  39. self._oleobj_ = ADsBuildEnumerator(self._cont_) # a PyIADsEnumVARIANT
  40. self.index = -1
  41. def __getitem__(self, index):
  42. return self.__GetIndex(index)
  43. def __call__(self, index):
  44. return self.__GetIndex(index)
  45. def __GetIndex(self, index):
  46. if type(index)!=type(0): raise TypeError("Only integer indexes are supported for enumerators")
  47. if index != self.index + 1:
  48. # Index requested out of sequence.
  49. raise ValueError("You must index this object sequentially")
  50. self.index = index
  51. result = ADsEnumerateNext(self._oleobj_, 1)
  52. if len(result):
  53. return _get_good_ret(result[0])
  54. # Failed - reset for next time around.
  55. self.index = -1
  56. self._oleobj_ = ADsBuildEnumerator(self._cont_) # a PyIADsEnumVARIANT
  57. raise IndexError("list index out of range")
  58. class ADSIDispatch(win32com.client.CDispatch):
  59. def _wrap_dispatch_(self, ob, userName = None, returnCLSID = None, UnicodeToString=None):
  60. assert UnicodeToString is None, "this is deprectated and will be removed"
  61. if not userName:
  62. userName = "ADSI-object"
  63. olerepr = win32com.client.dynamic.MakeOleRepr(ob, None, None)
  64. return ADSIDispatch(ob, olerepr, userName)
  65. def _NewEnum(self):
  66. try:
  67. return ADSIEnumerator(self)
  68. except pythoncom.com_error:
  69. # doesnt support it - let our base try!
  70. return win32com.client.CDispatch._NewEnum(self)
  71. def __getattr__(self, attr):
  72. try:
  73. return getattr(self._oleobj_, attr)
  74. except AttributeError:
  75. return win32com.client.CDispatch.__getattr__(self, attr)
  76. def QueryInterface(self, iid):
  77. ret = self._oleobj_.QueryInterface(iid)
  78. return _get_good_ret(ret)
  79. # We override the global methods to do the right thing.
  80. _ADsGetObject = ADsGetObject # The one in the .pyd
  81. def ADsGetObject(path, iid = pythoncom.IID_IDispatch):
  82. ret = _ADsGetObject(path, iid)
  83. return _get_good_ret(ret)
  84. _ADsOpenObject = ADsOpenObject
  85. def ADsOpenObject(path, username, password, reserved = 0, iid = pythoncom.IID_IDispatch):
  86. ret = _ADsOpenObject(path, username, password, reserved, iid)
  87. return _get_good_ret(ret)