ThirdParty/Send2Trash/send2trash/plat_win.py

Fri, 20 Jun 2014 16:05:45 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 20 Jun 2014 16:05:45 +0200
changeset 3644
a2c88b9b1d16
child 5994
cf0b37d2a28d
permissions
-rw-r--r--

Added code to Project to move deleted files/directories to the recycle bin falling back to removing them (os.remove), if send2trash cannot be imported due to missing dependencies.

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

eric ide

mercurial