Browse Source

Adding support for 'apply' action

pull/2/head
Ryan Reed 2 years ago
parent
commit
14fec49b42
3 changed files with 50 additions and 4 deletions
  1. +13
    -1
      src/transpose/console.py
  2. +17
    -0
      src/transpose/transpose.py
  3. +20
    -3
      tests/test_transpose.py

+ 13
- 1
src/transpose/console.py View File

@ -17,7 +17,9 @@ def entry_point() -> None:
force=args.force,
)
if args.action == "restore":
if args.action == "apply":
t.apply()
elif args.action == "restore":
t.restore()
elif args.action == "store":
t.store(name=args.name)
@ -43,6 +45,16 @@ def parse_arguments():
help="Transpose Action", dest="action", required=True
)
apply_parser = subparsers.add_parser(
"apply",
help="Recreate the symlink from the cache file (useful after moving store loction)",
parents=[base_parser],
)
apply_parser.add_argument(
"target_path",
help="The path to the directory to locate the cache file",
)
restore_parser = subparsers.add_parser(
"restore",
help="Move a transposed directory back to it's original location",


+ 17
- 0
src/transpose/transpose.py View File

@ -22,6 +22,23 @@ class Transpose:
self.force = force
def apply(self) -> None:
"""
Recreate the symlink from an existing cache file
"""
if not self.cache_path.exists():
raise TransposeError(
f"Cache file does not exist indicating target is not managed by Transpose: {self.cache_path}"
)
cache = get_cache(self.cache_path)
original_path = Path(cache["original_path"])
if original_path.is_symlink():
remove(original_path)
symlink(target_path=self.cache_path.parent, symlink_path=original_path)
def restore(self) -> None:
"""
Restores a previously Transpose managed directory to it's previous location.


+ 20
- 3
tests/test_transpose.py View File

@ -10,7 +10,7 @@ from .utils import STORE_DIR, TARGET_DIR, setup_env
@setup_env()
def test_transpose_init():
def test_init():
t = Transpose(
target_path=TARGET_DIR,
store_path=STORE_DIR,
@ -26,7 +26,24 @@ def test_transpose_init():
@setup_env()
def test_transpose_store_restore():
def test_apply():
store_path = Path(STORE_DIR)
target_path = Path(TARGET_DIR)
store_path.rmdir()
target_path.rename(store_path)
t = Transpose(
target_path=STORE_DIR,
store_path=STORE_DIR,
)
t.apply()
assert store_path.is_dir() and not store_path.is_symlink()
assert target_path.is_dir() and target_path.is_symlink()
@setup_env()
def test_store_restore():
t = Transpose(
target_path=TARGET_DIR,
store_path=STORE_DIR,
@ -52,7 +69,7 @@ def test_transpose_store_restore():
@setup_env()
def test_transpose_restore_force():
def test_restore_force():
nonexistent_path = Path(STORE_DIR).joinpath("long/path")
# Overwrite cache file with nonexistent directory


Loading…
Cancel
Save