diff --git a/tests/test_project.py b/tests/test_project.py index 0da6854..594987f 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -11,7 +11,7 @@ from transpose.utils import check_path, create_cache, get_cache, move, remove, s TARGET_DIR = "source" -STORE_DIR = "destination" +STORE_DIR = "store" SYMLINK_DIR = "symlink_test" CACHE_FILE_CONTENTS = {"version": version, "original_path": TARGET_DIR} @@ -153,3 +153,9 @@ def test_transpose_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() +def test_transpose_restore_force(): + # TODO + pass diff --git a/transpose/console.py b/transpose/console.py index 2f37837..00f6d74 100644 --- a/transpose/console.py +++ b/transpose/console.py @@ -14,6 +14,7 @@ def entry_point() -> None: target_path=args.target_path, store_path=store_path, cache_filename=cache_filename, + force=args.force, ) if args.action == "restore": @@ -30,6 +31,12 @@ 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/transpose/transpose.py b/transpose/transpose.py index 06ce425..53da516 100644 --- a/transpose/transpose.py +++ b/transpose/transpose.py @@ -6,7 +6,11 @@ from .utils import check_path, create_cache, get_cache, move, remove, symlink class Transpose: def __init__( - self, target_path: str, store_path: str, cache_filename: str = None + self, + 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) @@ -16,6 +20,8 @@ class Transpose: self.cache_filename = cache_filename self.cache_path = Path(self.target_path).joinpath(cache_filename) + self.force = force + def restore(self) -> None: """ Restores a previously Transpose managed directory to it's previous location. @@ -37,6 +43,13 @@ class Transpose: f"Original path in cache file already exists: {original_path}" ) + try: + original_path.parent.mkdir(parents=self.force, exist_ok=True) + except FileNotFoundError: + raise TransposeError( + f"The parent directory for the original path, {original_path.parent} does not exist. Use '-f' to force the creation of this directory" + ) + move(source=self.target_path, destination=original_path) new_cache_path = Path(original_path).joinpath(self.cache_filename)