Spaces:
Sleeping
Sleeping
File size: 5,153 Bytes
10865e1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# Sample implementation of IFileOperationProgressSink that just prints
# some basic info
import pythoncom
from win32com.server.policy import DesignatedWrapPolicy
from win32com.shell import shell, shellcon
tsf_flags = list(
(k, v) for k, v in list(shellcon.__dict__.items()) if k.startswith("TSF_")
)
def decode_flags(flags):
if flags == 0:
return "TSF_NORMAL"
flag_txt = ""
for k, v in tsf_flags:
if flags & v:
if flag_txt:
flag_txt = flag_txt + "|" + k
else:
flag_txt = k
return flag_txt
class FileOperationProgressSink(DesignatedWrapPolicy):
_com_interfaces_ = [shell.IID_IFileOperationProgressSink]
_public_methods_ = [
"StartOperations",
"FinishOperations",
"PreRenameItem",
"PostRenameItem",
"PreMoveItem",
"PostMoveItem",
"PreCopyItem",
"PostCopyItem",
"PreDeleteItem",
"PostDeleteItem",
"PreNewItem",
"PostNewItem",
"UpdateProgress",
"ResetTimer",
"PauseTimer",
"ResumeTimer",
]
def __init__(self):
self._wrap_(self)
def StartOperations(self):
print("StartOperations")
def FinishOperations(self, Result):
print("FinishOperations: HRESULT ", Result)
def PreRenameItem(self, Flags, Item, NewName):
print(
"PreRenameItem: Renaming "
+ Item.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ " to "
+ NewName
)
def PostRenameItem(self, Flags, Item, NewName, hrRename, NewlyCreated):
if NewlyCreated is not None:
newfile = NewlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING)
else:
newfile = "not renamed, HRESULT " + str(hrRename)
print(
"PostRenameItem: renamed "
+ Item.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ " to "
+ newfile
)
def PreMoveItem(self, Flags, Item, DestinationFolder, NewName):
print(
"PreMoveItem: Moving "
+ Item.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ " to "
+ DestinationFolder.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ "\\"
+ str(NewName)
)
def PostMoveItem(
self, Flags, Item, DestinationFolder, NewName, hrMove, NewlyCreated
):
if NewlyCreated is not None:
newfile = NewlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING)
else:
newfile = "not copied, HRESULT " + str(hrMove)
print(
"PostMoveItem: Moved "
+ Item.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ " to "
+ newfile
)
def PreCopyItem(self, Flags, Item, DestinationFolder, NewName):
if not NewName:
NewName = ""
print(
"PreCopyItem: Copying "
+ Item.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ " to "
+ DestinationFolder.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ "\\"
+ NewName
)
print("Flags: ", decode_flags(Flags))
def PostCopyItem(
self, Flags, Item, DestinationFolder, NewName, hrCopy, NewlyCreated
):
if NewlyCreated is not None:
newfile = NewlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING)
else:
newfile = "not copied, HRESULT " + str(hrCopy)
print(
"PostCopyItem: Copied "
+ Item.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ " to "
+ newfile
)
print("Flags: ", decode_flags(Flags))
def PreDeleteItem(self, Flags, Item):
print(
"PreDeleteItem: Deleting " + Item.GetDisplayName(shellcon.SHGDN_FORPARSING)
)
def PostDeleteItem(self, Flags, Item, hrDelete, NewlyCreated):
print(
"PostDeleteItem: Deleted " + Item.GetDisplayName(shellcon.SHGDN_FORPARSING)
)
if NewlyCreated:
print(
" Moved to recycle bin - "
+ NewlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING)
)
def PreNewItem(self, Flags, DestinationFolder, NewName):
print(
"PreNewItem: Creating "
+ DestinationFolder.GetDisplayName(shellcon.SHGDN_FORPARSING)
+ "\\"
+ NewName
)
def PostNewItem(
self,
Flags,
DestinationFolder,
NewName,
TemplateName,
FileAttributes,
hrNew,
NewItem,
):
print(
"PostNewItem: Created " + NewItem.GetDisplayName(shellcon.SHGDN_FORPARSING)
)
def UpdateProgress(self, WorkTotal, WorkSoFar):
print("UpdateProgress: ", WorkSoFar, WorkTotal)
def ResetTimer(self):
print("ResetTimer")
def PauseTimer(self):
print("PauseTimer")
def ResumeTimer(self):
print("ResumeTimer")
def CreateSink():
return pythoncom.WrapObject(
FileOperationProgressSink(), shell.IID_IFileOperationProgressSink
)
|