mirror of
https://github.com/arsenetar/send2trash.git
synced 2025-05-08 09:49:52 +00:00
In plat_other, added support for sending to trash on external volumes.
This commit is contained in:
parent
cc15b628a2
commit
ae126c47bf
@ -18,17 +18,48 @@ else:
|
|||||||
logging.warning("Can't find path for Trash")
|
logging.warning("Can't find path for Trash")
|
||||||
TRASH_PATH = op.expanduser('~/.Trash')
|
TRASH_PATH = op.expanduser('~/.Trash')
|
||||||
|
|
||||||
# XXX Make this work on external volumes
|
EXTERNAL_CANDIDATES = [
|
||||||
|
'.Trash-1000/files',
|
||||||
|
'.Trash/files',
|
||||||
|
'.Trash-1000',
|
||||||
|
'.Trash',
|
||||||
|
]
|
||||||
|
|
||||||
def send2trash(path):
|
def find_mount_point(path):
|
||||||
if not isinstance(path, unicode):
|
# Even if something's wrong, "/" is a mount point, so the loop will exit.
|
||||||
path = unicode(path, sys.getfilesystemencoding())
|
while not op.ismount(path):
|
||||||
filename = op.basename(path)
|
path = op.join(*op.split(path)[:-1])
|
||||||
destpath = op.join(TRASH_PATH, filename)
|
return path
|
||||||
|
|
||||||
|
def find_ext_volume_trash(volume_root):
|
||||||
|
for candidate in EXTERNAL_CANDIDATES:
|
||||||
|
candidate_path = op.join(volume_root, candidate)
|
||||||
|
if op.exists(candidate_path):
|
||||||
|
return candidate_path
|
||||||
|
else:
|
||||||
|
# Something's wrong here. Screw that, just create a .Trash folder
|
||||||
|
trash_path = op.join(volume_root, '.Trash')
|
||||||
|
os.mkdir(trash_path)
|
||||||
|
return trash_path
|
||||||
|
|
||||||
|
def move_without_conflict(src, dst):
|
||||||
|
filename = op.basename(src)
|
||||||
|
destpath = op.join(dst, filename)
|
||||||
counter = 0
|
counter = 0
|
||||||
while op.exists(destpath):
|
while op.exists(destpath):
|
||||||
counter += 1
|
counter += 1
|
||||||
base_name, ext = op.splitext(filename)
|
base_name, ext = op.splitext(filename)
|
||||||
new_filename = '{0} {1}{2}'.format(base_name, counter, ext)
|
new_filename = '{0} {1}{2}'.format(base_name, counter, ext)
|
||||||
destpath = op.join(TRASH_PATH, new_filename)
|
destpath = op.join(TRASH_PATH, new_filename)
|
||||||
os.rename(path, destpath)
|
os.rename(src, destpath)
|
||||||
|
|
||||||
|
def send2trash(path):
|
||||||
|
if not isinstance(path, unicode):
|
||||||
|
path = unicode(path, sys.getfilesystemencoding())
|
||||||
|
try:
|
||||||
|
move_without_conflict(path, TRASH_PATH)
|
||||||
|
except OSError:
|
||||||
|
# We're probably on an external volume
|
||||||
|
mount_point = find_mount_point(path)
|
||||||
|
dest_trash = find_ext_volume_trash(mount_point)
|
||||||
|
move_without_conflict(path, dest_trash)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user