diff options
author | Christian Zangl <laktak@cdak.net> | 2019-07-21 16:11:10 +0200 |
---|---|---|
committer | Christian Zangl <laktak@cdak.net> | 2019-07-21 16:11:10 +0200 |
commit | 34ec0780589a257b061e04be12ea3e101ad83c4f (patch) | |
tree | fb2ca61207243e41ceac8ee3125e38c5e6540d08 | |
parent | 65f95287d42869952e3bc0ae9ffefa94221f7faa (diff) | |
download | ranger-34ec0780589a257b061e04be12ea3e101ad83c4f.tar.gz |
set default make_safe_path=get_safe_path
-rw-r--r-- | ranger/core/actions.py | 3 | ||||
-rw-r--r-- | ranger/core/loader.py | 3 | ||||
-rw-r--r-- | ranger/ext/safe_path.py | 22 | ||||
-rw-r--r-- | ranger/ext/shutil_generatorized.py | 32 |
4 files changed, 34 insertions, 26 deletions
diff --git a/ranger/core/actions.py b/ranger/core/actions.py index de441733..96829f27 100644 --- a/ranger/core/actions.py +++ b/ranger/core/actions.py @@ -24,6 +24,7 @@ import ranger from ranger.ext.direction import Direction from ranger.ext.relative_symlink import relative_symlink from ranger.ext.keybinding_parser import key_to_string, construct_keybinding +from ranger.ext.safe_path import get_safe_path from ranger.ext.shell_escape import shell_quote from ranger.ext.next_available_filename import next_available_filename from ranger.ext.rifle import squash_flags, ASK_COMMAND @@ -1591,7 +1592,7 @@ class Actions( # pylint: disable=too-many-instance-attributes,too-many-public-m link(source_path, next_available_filename(target_path)) - def paste(self, overwrite=False, append=False, dest=None, make_safe_path=None): + def paste(self, overwrite=False, append=False, dest=None, make_safe_path=get_safe_path): """:paste Paste the selected items into the current directory or to dest diff --git a/ranger/core/loader.py b/ranger/core/loader.py index fadff787..7d4b21e8 100644 --- a/ranger/core/loader.py +++ b/ranger/core/loader.py @@ -19,6 +19,7 @@ except ImportError: HAVE_CHARDET = False from ranger.core.shared import FileManagerAware +from ranger.ext.safe_path import get_safe_path from ranger.ext.signals import SignalDispatcher from ranger.ext.human_readable import human_readable @@ -52,7 +53,7 @@ class CopyLoader(Loadable, FileManagerAware): # pylint: disable=too-many-instan progressbar_supported = True def __init__(self, copy_buffer, do_cut=False, overwrite=False, dest=None, - make_safe_path=None): + make_safe_path=get_safe_path): self.copy_buffer = tuple(copy_buffer) self.do_cut = do_cut self.original_copy_buffer = copy_buffer diff --git a/ranger/ext/safe_path.py b/ranger/ext/safe_path.py new file mode 100644 index 00000000..ed2cf2b6 --- /dev/null +++ b/ranger/ext/safe_path.py @@ -0,0 +1,22 @@ +# This file is part of ranger, the console file manager. +# License: GNU GPL version 3, see the file "AUTHORS" for details. + +import os + +APPENDIX = '_' + + +def get_safe_path(dst): + if not os.path.exists(dst): + return dst + if not dst.endswith(APPENDIX): + dst += APPENDIX + if not os.path.exists(dst): + return dst + n = 0 + test_dst = dst + str(n) + while os.path.exists(test_dst): + n += 1 + test_dst = dst + str(n) + + return test_dst diff --git a/ranger/ext/shutil_generatorized.py b/ranger/ext/shutil_generatorized.py index bcd381ea..a97a4d2d 100644 --- a/ranger/ext/shutil_generatorized.py +++ b/ranger/ext/shutil_generatorized.py @@ -7,11 +7,11 @@ import os import stat import sys from shutil import (_samefile, rmtree, _basename, _destinsrc, Error, SpecialFileError) +from ranger.ext.safe_path import get_safe_path __all__ = ["copyfileobj", "copyfile", "copystat", "copy2", "BLOCK_SIZE", "copytree", "move", "rmtree", "Error", "SpecialFileError"] -APPENDIX = '_' BLOCK_SIZE = 16 * 1024 @@ -103,22 +103,6 @@ else: pass -def get_safe_path(dst): - if not os.path.exists(dst): - return dst - if not dst.endswith(APPENDIX): - dst += APPENDIX - if not os.path.exists(dst): - return dst - n = 0 - test_dst = dst + str(n) - while os.path.exists(test_dst): - n += 1 - test_dst = dst + str(n) - - return test_dst - - def copyfileobj(fsrc, fdst, length=BLOCK_SIZE): """copy data from file-like object fsrc to file-like object fdst""" done = 0 @@ -153,7 +137,7 @@ def copyfile(src, dst): yield done -def copy2(src, dst, overwrite=False, symlinks=False, make_safe_path=None): +def copy2(src, dst, overwrite=False, symlinks=False, make_safe_path=get_safe_path): """Copy data and all stat info ("cp -p src dst"). The destination may be a directory. @@ -162,7 +146,7 @@ def copy2(src, dst, overwrite=False, symlinks=False, make_safe_path=None): if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) if not overwrite: - dst = (make_safe_path or get_safe_path)(dst) + dst = make_safe_path(dst) if symlinks and os.path.islink(src): linkto = os.readlink(src) if overwrite and os.path.lexists(dst): @@ -175,7 +159,7 @@ def copy2(src, dst, overwrite=False, symlinks=False, make_safe_path=None): def copytree(src, dst, # pylint: disable=too-many-locals,too-many-branches - symlinks=False, ignore=None, overwrite=False, make_safe_path=None): + symlinks=False, ignore=None, overwrite=False, make_safe_path=get_safe_path): """Recursively copy a directory tree using copy2(). The destination directory must not already exist. @@ -211,7 +195,7 @@ def copytree(src, dst, # pylint: disable=too-many-locals,too-many-branches os.makedirs(dst) except OSError: if not overwrite: - dst = (make_safe_path or get_safe_path)(dst) + dst = make_safe_path(dst) os.makedirs(dst) errors = [] done = 0 @@ -258,7 +242,7 @@ def copytree(src, dst, # pylint: disable=too-many-locals,too-many-branches raise Error(errors) -def move(src, dst, overwrite=False, make_safe_path=None): +def move(src, dst, overwrite=False, make_safe_path=get_safe_path): """Recursively move a file or directory to another location. This is similar to the Unix "mv" command. @@ -285,7 +269,7 @@ def move(src, dst, overwrite=False, make_safe_path=None): real_dst = os.path.join(dst, _basename(src)) if not overwrite: - real_dst = (make_safe_path or get_safe_path)(real_dst) + real_dst = make_safe_path(real_dst) try: os.rename(src, real_dst) except OSError: @@ -298,6 +282,6 @@ def move(src, dst, overwrite=False, make_safe_path=None): rmtree(src) else: for done in copy2(src, real_dst, symlinks=True, overwrite=overwrite, - make_safe_path=None): + make_safe_path=make_safe_path): yield done os.unlink(src) |