mirror of
https://github.com/arsenetar/send2trash.git
synced 2025-05-07 09:29:48 +00:00
Add ability to handle pathlib paths
- Handle pathlib paths across all implementations, plat_other already did - Move preprocessing code to common location
This commit is contained in:
parent
84c220cbd9
commit
94e1ec007a
@ -6,11 +6,11 @@
|
|||||||
|
|
||||||
from gi.repository import GObject, Gio
|
from gi.repository import GObject, Gio
|
||||||
from .exceptions import TrashPermissionError
|
from .exceptions import TrashPermissionError
|
||||||
|
from .util import preprocess_paths
|
||||||
|
|
||||||
|
|
||||||
def send2trash(paths):
|
def send2trash(paths):
|
||||||
if not isinstance(paths, list):
|
paths = preprocess_paths(paths)
|
||||||
paths = [paths]
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
try:
|
try:
|
||||||
f = Gio.File.new_for_path(path)
|
f = Gio.File.new_for_path(path)
|
||||||
|
@ -10,6 +10,7 @@ from ctypes import cdll, byref, Structure, c_char, c_char_p
|
|||||||
from ctypes.util import find_library
|
from ctypes.util import find_library
|
||||||
|
|
||||||
from .compat import binary_type
|
from .compat import binary_type
|
||||||
|
from .util import preprocess_paths
|
||||||
|
|
||||||
Foundation = cdll.LoadLibrary(find_library("Foundation"))
|
Foundation = cdll.LoadLibrary(find_library("Foundation"))
|
||||||
CoreServices = cdll.LoadLibrary(find_library("CoreServices"))
|
CoreServices = cdll.LoadLibrary(find_library("CoreServices"))
|
||||||
@ -40,8 +41,7 @@ def check_op_result(op_result):
|
|||||||
|
|
||||||
|
|
||||||
def send2trash(paths):
|
def send2trash(paths):
|
||||||
if not isinstance(paths, list):
|
paths = preprocess_paths(paths)
|
||||||
paths = [paths]
|
|
||||||
paths = [
|
paths = [
|
||||||
path.encode("utf-8") if not isinstance(path, binary_type) else path
|
path.encode("utf-8") if not isinstance(path, binary_type) else path
|
||||||
for path in paths
|
for path in paths
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
from Foundation import NSFileManager, NSURL
|
from Foundation import NSFileManager, NSURL
|
||||||
from .compat import text_type
|
from .compat import text_type
|
||||||
|
from .util import preprocess_paths
|
||||||
|
|
||||||
|
|
||||||
def check_op_result(op_result):
|
def check_op_result(op_result):
|
||||||
@ -16,8 +17,7 @@ def check_op_result(op_result):
|
|||||||
|
|
||||||
|
|
||||||
def send2trash(paths):
|
def send2trash(paths):
|
||||||
if not isinstance(paths, list):
|
paths = preprocess_paths(paths)
|
||||||
paths = [paths]
|
|
||||||
paths = [
|
paths = [
|
||||||
path.decode("utf-8") if not isinstance(path, text_type) else path
|
path.decode("utf-8") if not isinstance(path, text_type) else path
|
||||||
for path in paths
|
for path in paths
|
||||||
|
@ -30,6 +30,7 @@ except ImportError:
|
|||||||
from urllib import quote
|
from urllib import quote
|
||||||
|
|
||||||
from .compat import text_type, environb
|
from .compat import text_type, environb
|
||||||
|
from .util import preprocess_paths
|
||||||
from .exceptions import TrashPermissionError
|
from .exceptions import TrashPermissionError
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -172,8 +173,7 @@ def get_dev(path):
|
|||||||
|
|
||||||
|
|
||||||
def send2trash(paths):
|
def send2trash(paths):
|
||||||
if not isinstance(paths, list):
|
paths = preprocess_paths(paths)
|
||||||
paths = [paths]
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
if isinstance(path, text_type):
|
if isinstance(path, text_type):
|
||||||
path_b = fsencode(path)
|
path_b = fsencode(path)
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import os.path as op
|
import os.path as op
|
||||||
from .compat import text_type
|
from .compat import text_type
|
||||||
|
from .util import preprocess_paths
|
||||||
|
|
||||||
from ctypes import (
|
from ctypes import (
|
||||||
windll,
|
windll,
|
||||||
Structure,
|
Structure,
|
||||||
@ -101,8 +103,7 @@ def get_short_path_name(long_name):
|
|||||||
|
|
||||||
|
|
||||||
def send2trash(paths):
|
def send2trash(paths):
|
||||||
if not isinstance(paths, list):
|
paths = preprocess_paths(paths)
|
||||||
paths = [paths]
|
|
||||||
# convert data type
|
# convert data type
|
||||||
paths = [
|
paths = [
|
||||||
text_type(path, "mbcs") if not isinstance(path, text_type) else path
|
text_type(path, "mbcs") if not isinstance(path, text_type) else path
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import os.path as op
|
import os.path as op
|
||||||
from .compat import text_type
|
from .compat import text_type
|
||||||
|
from .util import preprocess_paths
|
||||||
from platform import version
|
from platform import version
|
||||||
import pythoncom
|
import pythoncom
|
||||||
import pywintypes
|
import pywintypes
|
||||||
@ -15,8 +16,7 @@ from .IFileOperationProgressSink import CreateSink
|
|||||||
|
|
||||||
|
|
||||||
def send2trash(paths):
|
def send2trash(paths):
|
||||||
if not isinstance(paths, list):
|
paths = preprocess_paths(paths)
|
||||||
paths = [paths]
|
|
||||||
# convert data type
|
# convert data type
|
||||||
paths = [
|
paths = [
|
||||||
text_type(path, "mbcs") if not isinstance(path, text_type) else path
|
text_type(path, "mbcs") if not isinstance(path, text_type) else path
|
||||||
|
16
send2trash/util.py
Normal file
16
send2trash/util.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# encoding: utf-8
|
||||||
|
# Copyright 2017 Virgil Dupras
|
||||||
|
|
||||||
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
||||||
|
# which should be included with this package. The terms are also available at
|
||||||
|
# http://www.hardcoded.net/licenses/bsd_license
|
||||||
|
|
||||||
|
|
||||||
|
def preprocess_paths(paths):
|
||||||
|
if not isinstance(paths, list):
|
||||||
|
paths = [paths]
|
||||||
|
# Convert items such as pathlib paths to strings
|
||||||
|
paths = [
|
||||||
|
path.__fspath__() if hasattr(path, "__fspath__") else path for path in paths
|
||||||
|
]
|
||||||
|
return paths
|
Loading…
x
Reference in New Issue
Block a user