|
1 # Copyright 2013 Hardcoded Software (http://www.hardcoded.net) |
|
2 |
|
3 # This software is licensed under the "BSD" License as described in the "LICENSE" file, |
|
4 # which should be included with this package. The terms are also available at |
|
5 # http://www.hardcoded.net/licenses/bsd_license |
|
6 |
|
7 from __future__ import unicode_literals |
|
8 |
|
9 from ctypes import windll, Structure, byref, c_uint |
|
10 from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL |
|
11 import os.path as op |
|
12 |
|
13 from .compat import text_type |
|
14 |
|
15 shell32 = windll.shell32 |
|
16 SHFileOperationW = shell32.SHFileOperationW |
|
17 |
|
18 class SHFILEOPSTRUCTW(Structure): |
|
19 _fields_ = [ |
|
20 ("hwnd", HWND), |
|
21 ("wFunc", UINT), |
|
22 ("pFrom", LPCWSTR), |
|
23 ("pTo", LPCWSTR), |
|
24 ("fFlags", c_uint), |
|
25 ("fAnyOperationsAborted", BOOL), |
|
26 ("hNameMappings", c_uint), |
|
27 ("lpszProgressTitle", LPCWSTR), |
|
28 ] |
|
29 |
|
30 FO_MOVE = 1 |
|
31 FO_COPY = 2 |
|
32 FO_DELETE = 3 |
|
33 FO_RENAME = 4 |
|
34 |
|
35 FOF_MULTIDESTFILES = 1 |
|
36 FOF_SILENT = 4 |
|
37 FOF_NOCONFIRMATION = 16 |
|
38 FOF_ALLOWUNDO = 64 |
|
39 FOF_NOERRORUI = 1024 |
|
40 |
|
41 def send2trash(path): |
|
42 if not isinstance(path, text_type): |
|
43 path = text_type(path, 'mbcs') |
|
44 if not op.isabs(path): |
|
45 path = op.abspath(path) |
|
46 fileop = SHFILEOPSTRUCTW() |
|
47 fileop.hwnd = 0 |
|
48 fileop.wFunc = FO_DELETE |
|
49 fileop.pFrom = LPCWSTR(path + '\0') |
|
50 fileop.pTo = None |
|
51 fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT |
|
52 fileop.fAnyOperationsAborted = 0 |
|
53 fileop.hNameMappings = 0 |
|
54 fileop.lpszProgressTitle = None |
|
55 result = SHFileOperationW(byref(fileop)) |
|
56 if result: |
|
57 msg = "Couldn't perform operation. Error code: %d" % result |
|
58 raise OSError(msg) |
|
59 |