IFileOperationProgressSink.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Sample implementation of IFileOperationProgressSink that just prints
  2. # some basic info
  3. import pythoncom
  4. from win32com.shell import shell, shellcon
  5. from win32com.server.policy import DesignatedWrapPolicy
  6. tsf_flags = list((k,v) for k,v in list(shellcon.__dict__.items()) if k.startswith('TSF_'))
  7. def decode_flags(flags):
  8. if flags == 0:
  9. return 'TSF_NORMAL'
  10. flag_txt = ''
  11. for k,v in tsf_flags:
  12. if flags & v:
  13. if flag_txt:
  14. flag_txt = flag_txt + '|' + k
  15. else:
  16. flag_txt = k
  17. return flag_txt
  18. class FileOperationProgressSink(DesignatedWrapPolicy):
  19. _com_interfaces_ = [shell.IID_IFileOperationProgressSink]
  20. _public_methods_ = [
  21. "StartOperations", "FinishOperations", "PreRenameItem", "PostRenameItem",
  22. "PreMoveItem", "PostMoveItem", "PreCopyItem", "PostCopyItem", "PreDeleteItem",
  23. "PostDeleteItem", "PreNewItem", "PostNewItem", "UpdateProgress",
  24. "ResetTimer", "PauseTimer", "ResumeTimer"
  25. ]
  26. def __init__(self):
  27. self._wrap_(self)
  28. def StartOperations(self):
  29. print ('StartOperations')
  30. def FinishOperations(self, Result):
  31. print('FinishOperations: HRESULT ', Result)
  32. def PreRenameItem(self, Flags, Item, NewName):
  33. print('PreRenameItem: Renaming ' + Item.GetDisplayName(shellcon.SHGDN_FORPARSING) + \
  34. ' to ' + NewName)
  35. def PostRenameItem(self, Flags, Item, NewName, hrRename, NewlyCreated):
  36. if NewlyCreated is not None:
  37. newfile = NewlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING)
  38. else:
  39. newfile = 'not renamed, HRESULT ' + str(hrRename)
  40. print('PostRenameItem: renamed ' + \
  41. Item.GetDisplayName(shellcon.SHGDN_FORPARSING) + ' to ' + newfile)
  42. def PreMoveItem(self, Flags, Item, DestinationFolder, NewName):
  43. print('PreMoveItem: Moving ' + \
  44. Item.GetDisplayName(shellcon.SHGDN_FORPARSING) + ' to ' + \
  45. DestinationFolder.GetDisplayName(shellcon.SHGDN_FORPARSING) + '\\' + str(NewName))
  46. def PostMoveItem(self, Flags, Item, DestinationFolder, NewName, hrMove, NewlyCreated):
  47. if NewlyCreated is not None:
  48. newfile = NewlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING)
  49. else:
  50. newfile = 'not copied, HRESULT ' + str(hrMove)
  51. print('PostMoveItem: Moved ' + \
  52. Item.GetDisplayName(shellcon.SHGDN_FORPARSING) + ' to ' + newfile)
  53. def PreCopyItem(self, Flags, Item, DestinationFolder, NewName):
  54. if not NewName:
  55. NewName = ''
  56. print('PreCopyItem: Copying ' + \
  57. Item.GetDisplayName(shellcon.SHGDN_FORPARSING) + ' to ' + \
  58. DestinationFolder.GetDisplayName(shellcon.SHGDN_FORPARSING) + '\\' + NewName)
  59. print('Flags: ', decode_flags(Flags))
  60. def PostCopyItem(self, Flags, Item, DestinationFolder, NewName, hrCopy, NewlyCreated):
  61. if NewlyCreated is not None:
  62. newfile = NewlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING)
  63. else:
  64. newfile = 'not copied, HRESULT ' + str(hrCopy)
  65. print('PostCopyItem: Copied ' + Item.GetDisplayName(shellcon.SHGDN_FORPARSING) + ' to ' + newfile)
  66. print('Flags: ', decode_flags(Flags))
  67. def PreDeleteItem(self, Flags, Item):
  68. print('PreDeleteItem: Deleting ' + Item.GetDisplayName(shellcon.SHGDN_FORPARSING))
  69. def PostDeleteItem(self, Flags, Item, hrDelete, NewlyCreated):
  70. print('PostDeleteItem: Deleted ' + Item.GetDisplayName(shellcon.SHGDN_FORPARSING))
  71. if NewlyCreated:
  72. print(' Moved to recycle bin - ' + NewlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING))
  73. def PreNewItem(self, Flags, DestinationFolder, NewName):
  74. print('PreNewItem: Creating ' + DestinationFolder.GetDisplayName(shellcon.SHGDN_FORPARSING) + '\\' + NewName)
  75. def PostNewItem(self, Flags, DestinationFolder, NewName, TemplateName, FileAttributes, hrNew, NewItem):
  76. print('PostNewItem: Created ' + NewItem.GetDisplayName(shellcon.SHGDN_FORPARSING))
  77. def UpdateProgress(self, WorkTotal, WorkSoFar):
  78. print('UpdateProgress: ', WorkSoFar, WorkTotal)
  79. def ResetTimer(self):
  80. print ('ResetTimer')
  81. def PauseTimer(self):
  82. print ('PauseTimer')
  83. def ResumeTimer(self):
  84. print ('ResumeTimer')
  85. def CreateSink():
  86. return pythoncom.WrapObject(FileOperationProgressSink(), shell.IID_IFileOperationProgressSink)