2010-04-07 06:52:24 +00:00
|
|
|
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
|
|
|
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
|
|
|
|
2010-10-17 17:00:56 +00:00
|
|
|
from ctypes import windll, Structure, byref, c_uint
|
|
|
|
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL
|
2010-04-06 16:04:32 +00:00
|
|
|
import os.path as op
|
2010-10-17 17:00:56 +00:00
|
|
|
|
|
|
|
shell32 = windll.shell32
|
|
|
|
SHFileOperationW = shell32.SHFileOperationW
|
|
|
|
|
|
|
|
class SHFILEOPSTRUCTW(Structure):
|
|
|
|
_fields_ = [
|
|
|
|
("hwnd", HWND),
|
|
|
|
("wFunc", UINT),
|
|
|
|
("pFrom", LPCWSTR),
|
|
|
|
("pTo", LPCWSTR),
|
|
|
|
("fFlags", c_uint),
|
|
|
|
("fAnyOperationsAborted", BOOL),
|
|
|
|
("hNameMappings", c_uint),
|
|
|
|
("lpszProgressTitle", LPCWSTR),
|
|
|
|
]
|
|
|
|
|
|
|
|
FO_MOVE = 1
|
|
|
|
FO_COPY = 2
|
|
|
|
FO_DELETE = 3
|
|
|
|
FO_RENAME = 4
|
|
|
|
|
|
|
|
FOF_MULTIDESTFILES = 1
|
|
|
|
FOF_SILENT = 4
|
|
|
|
FOF_NOCONFIRMATION = 16
|
|
|
|
FOF_ALLOWUNDO = 64
|
|
|
|
FOF_NOERRORUI = 1024
|
2010-04-06 16:04:32 +00:00
|
|
|
|
|
|
|
def send2trash(path):
|
2010-07-07 14:12:13 +00:00
|
|
|
if not isinstance(path, str):
|
|
|
|
path = str(path, 'mbcs')
|
2010-04-06 16:04:32 +00:00
|
|
|
if not op.isabs(path):
|
|
|
|
path = op.abspath(path)
|
2010-10-17 17:00:56 +00:00
|
|
|
fileop = SHFILEOPSTRUCTW()
|
|
|
|
fileop.hwnd = 0
|
|
|
|
fileop.wFunc = FO_DELETE
|
|
|
|
fileop.pFrom = LPCWSTR(path + '\0')
|
|
|
|
fileop.pTo = None
|
|
|
|
fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
|
|
|
|
fileop.fAnyOperationsAborted = 0
|
|
|
|
fileop.hNameMappings = 0
|
|
|
|
fileop.lpszProgressTitle = None
|
|
|
|
result = SHFileOperationW(byref(fileop))
|
|
|
|
if result:
|
|
|
|
msg = "Couldn't perform operation. Error code: %d" % result
|
|
|
|
raise OSError(msg)
|
|
|
|
|