mirror of
https://github.com/arsenetar/send2trash.git
synced 2024-12-06 20:59:03 +00:00
Minor code quality updates
This commit is contained in:
parent
2aa834be94
commit
78a536abba
@ -37,54 +37,10 @@ class FileOperationProgressSink(DesignatedWrapPolicy):
|
||||
# but that may need some additional considerations before implementing.
|
||||
return 0 if flags & shellcon.TSF_DELETE_RECYCLE_IF_POSSIBLE else 0x80004005 # S_OK, or E_FAIL
|
||||
|
||||
def PostDeleteItem(self, flags, item, hrDelete, newlyCreated):
|
||||
if newlyCreated:
|
||||
self.newItem = newlyCreated.GetDisplayName(shellcon.SHGDN_FORPARSING)
|
||||
|
||||
def StartOperations(self):
|
||||
pass
|
||||
|
||||
def FinishOperations(self, Result):
|
||||
pass
|
||||
|
||||
def PreRenameItem(self, Flags, Item, NewName):
|
||||
pass
|
||||
|
||||
def PostRenameItem(self, Flags, Item, NewName, hrRename, NewlyCreated):
|
||||
pass
|
||||
|
||||
def PreMoveItem(self, Flags, Item, DestinationFolder, NewName):
|
||||
pass
|
||||
|
||||
def PostMoveItem(self, Flags, Item, DestinationFolder, NewName, hrMove, NewlyCreated):
|
||||
pass
|
||||
|
||||
def PreCopyItem(self, Flags, Item, DestinationFolder, NewName):
|
||||
pass
|
||||
|
||||
def PostCopyItem(self, Flags, Item, DestinationFolder, NewName, hrCopy, NewlyCreated):
|
||||
pass
|
||||
|
||||
def PreNewItem(self, Flags, DestinationFolder, NewName):
|
||||
pass
|
||||
|
||||
def PostNewItem(
|
||||
self, Flags, DestinationFolder, NewName, TemplateName, FileAttributes, hrNew, NewItem,
|
||||
):
|
||||
pass
|
||||
|
||||
def UpdateProgress(self, WorkTotal, WorkSoFar):
|
||||
pass
|
||||
|
||||
def ResetTimer(self):
|
||||
pass
|
||||
|
||||
def PauseTimer(self):
|
||||
pass
|
||||
|
||||
def ResumeTimer(self):
|
||||
pass
|
||||
def PostDeleteItem(self, flags, item, hr_delete, newly_created):
|
||||
if newly_created:
|
||||
self.newItem = newly_created.GetDisplayName(shellcon.SHGDN_FORPARSING)
|
||||
|
||||
|
||||
def CreateSink():
|
||||
def create_sink():
|
||||
return pythoncom.WrapObject(FileOperationProgressSink(), shell.IID_IFileOperationProgressSink)
|
||||
|
@ -12,7 +12,7 @@ from platform import version
|
||||
import pythoncom
|
||||
import pywintypes
|
||||
from win32com.shell import shell, shellcon
|
||||
from .IFileOperationProgressSink import CreateSink
|
||||
from .IFileOperationProgressSink import create_sink
|
||||
|
||||
|
||||
def send2trash(paths):
|
||||
@ -42,7 +42,7 @@ def send2trash(paths):
|
||||
# actually try to perform the operation, this section may throw a
|
||||
# pywintypes.com_error which does not seem to create as nice of an
|
||||
# error as OSError so wrapping with try to convert
|
||||
sink = CreateSink()
|
||||
sink = create_sink()
|
||||
try:
|
||||
for path in paths:
|
||||
item = shell.SHCreateItemFromParsingName(path, None, shell.IID_IShellItem)
|
||||
|
@ -84,7 +84,7 @@ def _filesys_enc():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def testUnicodefile():
|
||||
def gen_unicode_file():
|
||||
name = u"send2trash_tést1"
|
||||
file = op.join(op.expanduser(b"~"), name.encode("utf-8"))
|
||||
touch(file)
|
||||
@ -101,35 +101,35 @@ def testUnicodefile():
|
||||
|
||||
|
||||
@pytest.mark.skipif(_filesys_enc() == "ascii", reason="Requires Unicode filesystem")
|
||||
def test_trash_bytes(testUnicodefile):
|
||||
s2t(testUnicodefile)
|
||||
assert not op.exists(testUnicodefile)
|
||||
def test_trash_bytes(gen_unicode_file):
|
||||
s2t(gen_unicode_file)
|
||||
assert not op.exists(gen_unicode_file)
|
||||
|
||||
|
||||
@pytest.mark.skipif(_filesys_enc() == "ascii", reason="Requires Unicode filesystem")
|
||||
def test_trash_unicode(testUnicodefile):
|
||||
s2t(testUnicodefile.decode(sys.getfilesystemencoding()))
|
||||
assert not op.exists(testUnicodefile)
|
||||
def test_trash_unicode(gen_unicode_file):
|
||||
s2t(gen_unicode_file.decode(sys.getfilesystemencoding()))
|
||||
assert not op.exists(gen_unicode_file)
|
||||
|
||||
|
||||
class ExtVol:
|
||||
def __init__(self, path):
|
||||
self.trashTopdir = path
|
||||
self.trash_topdir = path
|
||||
if PY3:
|
||||
self.trashTopdir_b = os.fsencode(self.trashTopdir)
|
||||
self.trash_topdir_b = os.fsencode(self.trash_topdir)
|
||||
else:
|
||||
self.trashTopdir_b = self.trashTopdir
|
||||
self.trash_topdir_b = self.trash_topdir
|
||||
|
||||
def s_getdev(path):
|
||||
from send2trash.plat_other import is_parent
|
||||
|
||||
st = os.lstat(path)
|
||||
if is_parent(self.trashTopdir, path):
|
||||
if is_parent(self.trash_topdir, path):
|
||||
return "dev"
|
||||
return st.st_dev
|
||||
|
||||
def s_ismount(path):
|
||||
if op.realpath(path) in (op.realpath(self.trashTopdir), op.realpath(self.trashTopdir_b),):
|
||||
if op.realpath(path) in (op.realpath(self.trash_topdir), op.realpath(self.trash_topdir_b),):
|
||||
return True
|
||||
return old_ismount(path)
|
||||
|
||||
@ -141,58 +141,62 @@ class ExtVol:
|
||||
def cleanup(self):
|
||||
send2trash.plat_other.get_dev = self.old_getdev
|
||||
send2trash.plat_other.os.path.ismount = self.old_ismount
|
||||
shutil.rmtree(self.trashTopdir)
|
||||
shutil.rmtree(self.trash_topdir)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def testExtVol():
|
||||
trashTopdir = mkdtemp(prefix="s2t")
|
||||
volume = ExtVol(trashTopdir)
|
||||
fileName = "test.txt"
|
||||
filePath = op.join(volume.trashTopdir, fileName)
|
||||
touch(filePath)
|
||||
assert op.exists(filePath) is True
|
||||
yield volume, fileName, filePath
|
||||
def gen_ext_vol():
|
||||
trash_topdir = mkdtemp(prefix="s2t")
|
||||
volume = ExtVol(trash_topdir)
|
||||
file_name = "test.txt"
|
||||
file_path = op.join(volume.trash_topdir, file_name)
|
||||
touch(file_path)
|
||||
assert op.exists(file_path) is True
|
||||
yield volume, file_name, file_path
|
||||
volume.cleanup()
|
||||
|
||||
|
||||
def test_trash_topdir(testExtVol):
|
||||
trashDir = op.join(testExtVol[0].trashTopdir, ".Trash")
|
||||
os.mkdir(trashDir, 0o777 | stat.S_ISVTX)
|
||||
def test_trash_topdir(gen_ext_vol):
|
||||
trash_dir = op.join(gen_ext_vol[0].trash_topdir, ".Trash")
|
||||
os.mkdir(trash_dir, 0o777 | stat.S_ISVTX)
|
||||
|
||||
s2t(testExtVol[2])
|
||||
assert op.exists(testExtVol[2]) is False
|
||||
assert op.exists(op.join(trashDir, str(os.getuid()), "files", testExtVol[1])) is True
|
||||
assert op.exists(op.join(trashDir, str(os.getuid()), "info", testExtVol[1] + ".trashinfo",)) is True
|
||||
s2t(gen_ext_vol[2])
|
||||
assert op.exists(gen_ext_vol[2]) is False
|
||||
assert op.exists(op.join(trash_dir, str(os.getuid()), "files", gen_ext_vol[1])) is True
|
||||
assert op.exists(op.join(trash_dir, str(os.getuid()), "info", gen_ext_vol[1] + ".trashinfo",)) is True
|
||||
# info relative path (if another test is added, with the same fileName/Path,
|
||||
# then it gets renamed etc.)
|
||||
cfg = ConfigParser()
|
||||
cfg.read(op.join(trashDir, str(os.getuid()), "info", testExtVol[1] + ".trashinfo"))
|
||||
assert (testExtVol[1] == cfg.get("Trash Info", "Path", raw=True)) is True
|
||||
cfg.read(op.join(trash_dir, str(os.getuid()), "info", gen_ext_vol[1] + ".trashinfo"))
|
||||
assert (gen_ext_vol[1] == cfg.get("Trash Info", "Path", raw=True)) is True
|
||||
|
||||
|
||||
def test_trash_topdir_fallback(testExtVol):
|
||||
s2t(testExtVol[2])
|
||||
assert op.exists(testExtVol[2]) is False
|
||||
assert op.exists(op.join(testExtVol[0].trashTopdir, ".Trash-" + str(os.getuid()), "files", testExtVol[1],)) is True
|
||||
def test_trash_topdir_fallback(gen_ext_vol):
|
||||
s2t(gen_ext_vol[2])
|
||||
assert op.exists(gen_ext_vol[2]) is False
|
||||
assert (
|
||||
op.exists(op.join(gen_ext_vol[0].trash_topdir, ".Trash-" + str(os.getuid()), "files", gen_ext_vol[1],)) is True
|
||||
)
|
||||
|
||||
|
||||
def test_trash_topdir_failure(testExtVol):
|
||||
os.chmod(testExtVol[0].trashTopdir, 0o500) # not writable to induce the exception
|
||||
pytest.raises(TrashPermissionError, s2t, [testExtVol[2]])
|
||||
os.chmod(testExtVol[0].trashTopdir, 0o700) # writable to allow deletion
|
||||
def test_trash_topdir_failure(gen_ext_vol):
|
||||
os.chmod(gen_ext_vol[0].trash_topdir, 0o500) # not writable to induce the exception
|
||||
pytest.raises(TrashPermissionError, s2t, [gen_ext_vol[2]])
|
||||
os.chmod(gen_ext_vol[0].trash_topdir, 0o700) # writable to allow deletion
|
||||
|
||||
|
||||
def test_trash_symlink(testExtVol):
|
||||
def test_trash_symlink(gen_ext_vol):
|
||||
# Use mktemp (race conditioney but no symlink equivalent)
|
||||
# Since is_parent uses realpath(), and our getdev uses is_parent,
|
||||
# this should work
|
||||
slDir = mktemp(prefix="s2t", dir=op.expanduser("~"))
|
||||
os.mkdir(op.join(testExtVol[0].trashTopdir, "subdir"), 0o700)
|
||||
filePath = op.join(testExtVol[0].trashTopdir, "subdir", testExtVol[1])
|
||||
touch(filePath)
|
||||
os.symlink(op.join(testExtVol[0].trashTopdir, "subdir"), slDir)
|
||||
s2t(op.join(slDir, testExtVol[1]))
|
||||
assert op.exists(filePath) is False
|
||||
assert op.exists(op.join(testExtVol[0].trashTopdir, ".Trash-" + str(os.getuid()), "files", testExtVol[1],)) is True
|
||||
os.remove(slDir)
|
||||
sl_dir = mktemp(prefix="s2t", dir=op.expanduser("~"))
|
||||
os.mkdir(op.join(gen_ext_vol[0].trashTopdir, "subdir"), 0o700)
|
||||
file_path = op.join(gen_ext_vol[0].trashTopdir, "subdir", gen_ext_vol[1])
|
||||
touch(file_path)
|
||||
os.symlink(op.join(gen_ext_vol[0].trashTopdir, "subdir"), sl_dir)
|
||||
s2t(op.join(sl_dir, gen_ext_vol[1]))
|
||||
assert op.exists(file_path) is False
|
||||
assert (
|
||||
op.exists(op.join(gen_ext_vol[0].trashTopdir, ".Trash-" + str(os.getuid()), "files", gen_ext_vol[1],)) is True
|
||||
)
|
||||
os.remove(sl_dir)
|
||||
|
Loading…
Reference in New Issue
Block a user