From 9362f064f357010e48aef8aa36b59fe55cf9206a Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Fri, 1 Jul 2022 20:31:19 -0400 Subject: [PATCH] Replacing Path rename with shutil, removing need for -f --- src/transpose/console.py | 7 ------- src/transpose/transpose.py | 6 ------ src/transpose/utils.py | 3 ++- tests/test_transpose.py | 22 ---------------------- 4 files changed, 2 insertions(+), 36 deletions(-) diff --git a/src/transpose/console.py b/src/transpose/console.py index c56604b..97bdc18 100644 --- a/src/transpose/console.py +++ b/src/transpose/console.py @@ -14,7 +14,6 @@ def entry_point() -> None: target_path=args.target_path, store_path=store_path, cache_filename=cache_filename, - force=args.force, ) if args.action == "apply": @@ -33,12 +32,6 @@ def parse_arguments(): Move and symlink a path for easier management """, ) - parser.add_argument( - "-f", - "--force", - action="store_true", - help="Force directory moves. For instance, create the store path if it does not exist", - ) parser.add_argument("--version", action="version", version=f"Transpose {version}") subparsers = parser.add_subparsers( diff --git a/src/transpose/transpose.py b/src/transpose/transpose.py index f03f896..36879cd 100644 --- a/src/transpose/transpose.py +++ b/src/transpose/transpose.py @@ -10,7 +10,6 @@ class Transpose: target_path: str, store_path: str, cache_filename: str = None, - force: bool = False, ) -> None: self.target_path = Path(target_path) self.store_path = Path(store_path) @@ -20,8 +19,6 @@ class Transpose: self.cache_filename = cache_filename self.cache_path = Path(self.target_path).joinpath(cache_filename) - self.force = force - def apply(self) -> None: """ Recreate the symlink from an existing cache file @@ -60,9 +57,6 @@ class Transpose: f"Original path in cache file already exists: {original_path}" ) - if not original_path.parent.exists() and self.force: - original_path.parent.mkdir(parents=True) - try: move(source=self.target_path, destination=original_path) except FileNotFoundError: diff --git a/src/transpose/utils.py b/src/transpose/utils.py index 9aa1a98..9c3e31d 100644 --- a/src/transpose/utils.py +++ b/src/transpose/utils.py @@ -2,6 +2,7 @@ from pathlib import Path from typing import Dict import json +import shutil from . import version @@ -62,7 +63,7 @@ def move(source: Path, destination: Path) -> None: """ Move a file using pathlib """ - source.rename(destination) + shutil.move(source, destination) def remove(path: Path) -> None: diff --git a/tests/test_transpose.py b/tests/test_transpose.py index 54d7050..1f45274 100644 --- a/tests/test_transpose.py +++ b/tests/test_transpose.py @@ -66,25 +66,3 @@ def test_store_restore(): assert not store_path.exists() assert target_path.is_dir() and not target_path.is_symlink() assert not t.cache_path.exists() - - -@setup_env() -def test_restore_force(): - nonexistent_path = Path(STORE_DIR).joinpath("long/path") - - # Overwrite cache file with nonexistent directory - cache_path = Path(TARGET_DIR).joinpath(DEFAULT_CACHE_FILENAME) - cache = {"version": version, "original_path": str(nonexistent_path)} - with open(str(cache_path), "w") as f: - json.dump(cache, f) - - # Force not enabled, should raise exception - t = Transpose(target_path=TARGET_DIR, store_path=STORE_DIR) - with pytest.raises(TransposeError): - t.restore() - assert not nonexistent_path.exists() - - # Force enabled, should create directory tree - t = Transpose(target_path=TARGET_DIR, store_path=STORE_DIR, force=True) - t.restore() - assert nonexistent_path.exists() -- 2.30.1