|
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 cdll, byref, Structure, c_char, c_char_p |
|
10 from ctypes.util import find_library |
|
11 |
|
12 from .compat import binary_type |
|
13 |
|
14 Foundation = cdll.LoadLibrary(find_library('Foundation')) |
|
15 CoreServices = cdll.LoadLibrary(find_library('CoreServices')) |
|
16 |
|
17 GetMacOSStatusCommentString = Foundation.GetMacOSStatusCommentString |
|
18 GetMacOSStatusCommentString.restype = c_char_p |
|
19 FSPathMakeRefWithOptions = CoreServices.FSPathMakeRefWithOptions |
|
20 FSMoveObjectToTrashSync = CoreServices.FSMoveObjectToTrashSync |
|
21 |
|
22 kFSPathMakeRefDefaultOptions = 0 |
|
23 kFSPathMakeRefDoNotFollowLeafSymlink = 0x01 |
|
24 |
|
25 kFSFileOperationDefaultOptions = 0 |
|
26 kFSFileOperationOverwrite = 0x01 |
|
27 kFSFileOperationSkipSourcePermissionErrors = 0x02 |
|
28 kFSFileOperationDoNotMoveAcrossVolumes = 0x04 |
|
29 kFSFileOperationSkipPreflight = 0x08 |
|
30 |
|
31 class FSRef(Structure): |
|
32 _fields_ = [('hidden', c_char * 80)] |
|
33 |
|
34 def check_op_result(op_result): |
|
35 if op_result: |
|
36 msg = GetMacOSStatusCommentString(op_result).decode('utf-8') |
|
37 raise OSError(msg) |
|
38 |
|
39 def send2trash(path): |
|
40 if not isinstance(path, binary_type): |
|
41 path = path.encode('utf-8') |
|
42 fp = FSRef() |
|
43 opts = kFSPathMakeRefDoNotFollowLeafSymlink |
|
44 op_result = FSPathMakeRefWithOptions(path, opts, byref(fp), None) |
|
45 check_op_result(op_result) |
|
46 opts = kFSFileOperationDefaultOptions |
|
47 op_result = FSMoveObjectToTrashSync(byref(fp), None, opts) |
|
48 check_op_result(op_result) |