|
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import unicode_literals |
|
import os.path as op |
|
from send2trash.compat import text_type |
|
from send2trash.util import preprocess_paths |
|
from platform import version |
|
import pythoncom |
|
import pywintypes |
|
from win32com.shell import shell, shellcon |
|
from send2trash.win.IFileOperationProgressSink import create_sink |
|
|
|
|
|
def send2trash(paths): |
|
paths = preprocess_paths(paths) |
|
if not paths: |
|
return |
|
|
|
paths = [text_type(path, "mbcs") if not isinstance(path, text_type) else path for path in paths] |
|
|
|
paths = [op.abspath(path) if not op.isabs(path) else path for path in paths] |
|
|
|
paths = [path[4:] if path.startswith("\\\\?\\") else path for path in paths] |
|
|
|
pythoncom.CoInitialize() |
|
|
|
fileop = pythoncom.CoCreateInstance( |
|
shell.CLSID_FileOperation, |
|
None, |
|
pythoncom.CLSCTX_ALL, |
|
shell.IID_IFileOperation, |
|
) |
|
|
|
flags = shellcon.FOF_NOCONFIRMATION | shellcon.FOF_NOERRORUI | shellcon.FOF_SILENT | shellcon.FOFX_EARLYFAILURE |
|
|
|
|
|
if int(version().split(".", 1)[0]) >= 8: |
|
flags |= 0x20000000 | 0x00080000 |
|
else: |
|
flags |= shellcon.FOF_ALLOWUNDO |
|
|
|
fileop.SetOperationFlags(flags) |
|
|
|
|
|
|
|
sink = create_sink() |
|
try: |
|
for path in paths: |
|
item = shell.SHCreateItemFromParsingName(path, None, shell.IID_IShellItem) |
|
fileop.DeleteItem(item, sink) |
|
result = fileop.PerformOperations() |
|
aborted = fileop.GetAnyOperationsAborted() |
|
|
|
if result or aborted: |
|
raise OSError(None, None, paths, result) |
|
except pywintypes.com_error as error: |
|
|
|
|
|
raise OSError(None, error.strerror, path, error.hresult) |
|
finally: |
|
|
|
pythoncom.CoUninitialize() |
|
|