From f324ff491ea9465126f01b510f169eac7ff65679 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Thu, 3 Aug 2017 20:47:58 -0400 Subject: [PATCH] Properly reuse the "compat" unit --- send2trash/compat.py | 17 +++++++++++------ send2trash/plat_other.py | 16 +++++++--------- tests/test_plat_other.py | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/send2trash/compat.py b/send2trash/compat.py index 0f3a489..0792898 100644 --- a/send2trash/compat.py +++ b/send2trash/compat.py @@ -1,13 +1,18 @@ # Copyright 2013 Hardcoded Software (http://www.hardcoded.net) -# 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 +# 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 import sys -if sys.version < '3': - text_type = unicode - binary_type = str -else: +import os + +PY3 = sys.version_info[0] >= 3 +if PY3: text_type = str binary_type = bytes + environb = os.environb +else: + text_type = unicode + binary_type = str + environb = os.environ diff --git a/send2trash/plat_other.py b/send2trash/plat_other.py index 47cf333..0cdab96 100644 --- a/send2trash/plat_other.py +++ b/send2trash/plat_other.py @@ -1,7 +1,7 @@ # Copyright 2013 Hardcoded Software (http://www.hardcoded.net) -# 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 +# 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 # This is a reimplementation of plat_other.py with reference to the @@ -27,9 +27,7 @@ except ImportError: # Python 2 from urllib import quote -# PY2-PY3 compatibilty -text_type = str if sys.version_info[0] == 3 else unicode -environb = os.environb if sys.version_info[0] >= 3 else os.environ +from .compat import text_type, environb try: fsencode = os.fsencode # Python 3 @@ -96,10 +94,10 @@ def trash_move(src, dst, topdir=None): while op.exists(op.join(filespath, destname)) or op.exists(op.join(infopath, destname + INFO_SUFFIX)): counter += 1 destname = base_name + b' ' + text_type(counter).encode('ascii') + ext - + check_create(filespath) check_create(infopath) - + os.rename(src, op.join(filespath, destname)) f = open(op.join(infopath, destname + INFO_SUFFIX), 'w') f.write(info_for(src, topdir)) @@ -119,7 +117,7 @@ def find_ext_volume_global_trash(volume_root): trash_dir = op.join(volume_root, TOPDIR_TRASH) if not op.exists(trash_dir): return None - + mode = os.lstat(trash_dir).st_mode # vol/.Trash must be a directory, cannot be a symlink, and must have the # sticky bit set. @@ -171,7 +169,7 @@ def send2trash(path): # if the file to be trashed is on the same device as HOMETRASH we # want to move it there. path_dev = get_dev(path_b) - + # If XDG_DATA_HOME or HOMETRASH do not yet exist we need to stat the # home directory, and these paths will be created further on if needed. trash_dev = get_dev(op.expanduser(b'~')) diff --git a/tests/test_plat_other.py b/tests/test_plat_other.py index 933a522..b129846 100644 --- a/tests/test_plat_other.py +++ b/tests/test_plat_other.py @@ -5,6 +5,7 @@ import os from os import path as op import send2trash.plat_other from send2trash.plat_other import send2trash as s2t +from send2trash.compat import PY3 from configparser import ConfigParser from tempfile import mkdtemp, NamedTemporaryFile, mktemp import shutil @@ -13,7 +14,6 @@ import sys # Could still use cleaning up. But no longer relies on ramfs. HOMETRASH = send2trash.plat_other.HOMETRASH -PY3 = sys.version_info[0] >= 3 def touch(path): with open(path, 'a'):