Browse Source

Adding option to force creation of restore path

pull/2/head
Ryan Reed 2 years ago
parent
commit
e718e1cb43
3 changed files with 28 additions and 2 deletions
  1. +7
    -1
      tests/test_project.py
  2. +7
    -0
      transpose/console.py
  3. +14
    -1
      transpose/transpose.py

+ 7
- 1
tests/test_project.py View File

@ -11,7 +11,7 @@ from transpose.utils import check_path, create_cache, get_cache, move, remove, s
TARGET_DIR = "source" TARGET_DIR = "source"
STORE_DIR = "destination"
STORE_DIR = "store"
SYMLINK_DIR = "symlink_test" SYMLINK_DIR = "symlink_test"
CACHE_FILE_CONTENTS = {"version": version, "original_path": TARGET_DIR} CACHE_FILE_CONTENTS = {"version": version, "original_path": TARGET_DIR}
@ -153,3 +153,9 @@ def test_transpose_store_restore():
assert not store_path.exists() assert not store_path.exists()
assert target_path.is_dir() and not target_path.is_symlink() assert target_path.is_dir() and not target_path.is_symlink()
assert not t.cache_path.exists() assert not t.cache_path.exists()
@setup()
def test_transpose_restore_force():
# TODO
pass

+ 7
- 0
transpose/console.py View File

@ -14,6 +14,7 @@ def entry_point() -> None:
target_path=args.target_path, target_path=args.target_path,
store_path=store_path, store_path=store_path,
cache_filename=cache_filename, cache_filename=cache_filename,
force=args.force,
) )
if args.action == "restore": if args.action == "restore":
@ -30,6 +31,12 @@ def parse_arguments():
Move and symlink a path for easier management 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}") parser.add_argument("--version", action="version", version=f"Transpose {version}")
subparsers = parser.add_subparsers( subparsers = parser.add_subparsers(


+ 14
- 1
transpose/transpose.py View File

@ -6,7 +6,11 @@ from .utils import check_path, create_cache, get_cache, move, remove, symlink
class Transpose: class Transpose:
def __init__( 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: ) -> None:
self.target_path = Path(target_path) self.target_path = Path(target_path)
self.store_path = Path(store_path) self.store_path = Path(store_path)
@ -16,6 +20,8 @@ class Transpose:
self.cache_filename = cache_filename self.cache_filename = cache_filename
self.cache_path = Path(self.target_path).joinpath(cache_filename) self.cache_path = Path(self.target_path).joinpath(cache_filename)
self.force = force
def restore(self) -> None: def restore(self) -> None:
""" """
Restores a previously Transpose managed directory to it's previous location. 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}" 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) move(source=self.target_path, destination=original_path)
new_cache_path = Path(original_path).joinpath(self.cache_filename) new_cache_path = Path(original_path).joinpath(self.cache_filename)


Loading…
Cancel
Save