Made a few minor style fixes, and added a proper error in cases where the target path of send2trash() doesn't exist.

This commit is contained in:
Virgil Dupras 2011-03-12 11:48:19 +01:00
parent eedbe258cb
commit 358b705cbc
2 changed files with 11 additions and 10 deletions

View File

@ -17,7 +17,6 @@
import sys import sys
import os import os
import os.path as op import os.path as op
import logging
from datetime import datetime from datetime import datetime
import stat import stat
from urllib.parse import quote from urllib.parse import quote
@ -27,21 +26,17 @@ INFO_DIR = 'info'
INFO_SUFFIX = '.trashinfo' INFO_SUFFIX = '.trashinfo'
# Default of ~/.local/share [3] # Default of ~/.local/share [3]
XDG_DATA_HOME = os.environ.get('XDG_DATA_HOME') or '~/.local/share' XDG_DATA_HOME = op.expanduser(os.environ.get('XDG_DATA_HOME', '~/.local/share'))
HOMETRASH = op.expanduser(op.join(XDG_DATA_HOME,'Trash')) HOMETRASH = op.join(XDG_DATA_HOME, 'Trash')
uid = os.getuid() uid = os.getuid()
TOPDIR_TRASH = '.Trash' TOPDIR_TRASH = '.Trash'
TOPDIR_FALLBACK = '.Trash-' + str(uid) TOPDIR_FALLBACK = '.Trash-' + str(uid)
def is_parent(parent, path): def is_parent(parent, path):
path = op.abspath(path) path = op.realpath(path) # In case it's a symlink
parent = op.abspath(parent) parent = op.realpath(parent)
while path != '/': return path.startswith(parent)
path = op.abspath(op.join(path, '..'))
if path == parent:
return True
return False
def format_date(date): def format_date(date):
return date.strftime("%Y-%m-%dT%H:%M:%S") return date.strftime("%Y-%m-%dT%H:%M:%S")
@ -128,6 +123,8 @@ def find_ext_volume_trash(volume_root):
def send2trash(path): def send2trash(path):
if not isinstance(path, str): if not isinstance(path, str):
path = str(path, sys.getfilesystemencoding()) path = str(path, sys.getfilesystemencoding())
if not op.exists(path):
raise OSError("File not found: %s" % path)
try: try:
trash_move(path, HOMETRASH, XDG_DATA_HOME) trash_move(path, HOMETRASH, XDG_DATA_HOME)
except OSError: except OSError:

View File

@ -3,6 +3,10 @@ import os
from os import path as op from os import path as op
from send2trash.plat_other import send2trash from send2trash.plat_other import send2trash
from configparser import ConfigParser from configparser import ConfigParser
# XXX Although this unittest is better than no unit test at all, it would be better to mock
# os.path.mountpoint() rather than going through ramfs (and requiring admin rights).
# #
# Warning: This test will shit up your Trash folder with test.txt files. # Warning: This test will shit up your Trash folder with test.txt files.
# #