Browse Source

Moving exceptions and creating TransposeError

pull/2/head
Ryan Reed 2 years ago
parent
commit
75c774aed9
3 changed files with 35 additions and 89 deletions
  1. +2
    -0
      transpose/exceptions.py
  2. +15
    -63
      transpose/transpose.py
  3. +18
    -26
      transpose/utils.py

+ 2
- 0
transpose/exceptions.py View File

@ -0,0 +1,2 @@
class TransposeError(Exception):
pass

+ 15
- 63
transpose/transpose.py View File

@ -1,5 +1,6 @@
from pathlib import Path, PurePath from pathlib import Path, PurePath
from .exceptions import TransposeError
from .utils import check_path, create_cache, get_cache, move, remove, symlink from .utils import check_path, create_cache, get_cache, move, remove, symlink
@ -26,53 +27,28 @@ class Transpose:
a. Remove if true a. Remove if true
4. Verify `original_path` doesn't exist 4. Verify `original_path` doesn't exist
5. Move `target_path` to `original_path` based on cache file settings 5. Move `target_path` to `original_path` based on cache file settings
Args:
None
Returns:
None
Raises:
ValueError: Any paths not existing
RuntimeError: Any error during the actual path changes
""" """
if not self.cache_path.exists(): if not self.cache_path.exists():
raise ValueError(
raise TransposeError(
f"Cache file does not exist indicating target is not managed by Transpose: {self.cache_path}" f"Cache file does not exist indicating target is not managed by Transpose: {self.cache_path}"
) )
if not self.target_path.exists(): if not self.target_path.exists():
raise ValueError(f"Target path does not exist: {self.target_path}")
raise TransposeError(f"Target path does not exist: {self.target_path}")
cache = get_cache(self.cache_path) cache = get_cache(self.cache_path)
original_path = Path(cache["original_path"]) original_path = Path(cache["original_path"])
if original_path.is_symlink(): if original_path.is_symlink():
try:
remove(original_path)
except: # noqa: E722 # TODO
raise RuntimeError(
f"Failed to remove symlink in original path: {original_path}"
)
remove(original_path)
elif original_path.exists(): elif original_path.exists():
raise ValueError(
raise TransposeError(
f"Original path in cache file already exists: {original_path}" f"Original path in cache file already exists: {original_path}"
) )
try:
move(source=self.target_path, destination=original_path)
except: # noqa: E722 # TODO
raise RuntimeError(
f"Failed to move target to original location: {self.target_path} -> {original_path}"
)
move(source=self.target_path, destination=original_path)
new_cache_path = Path(PurePath(original_path, self.cache_filename)) new_cache_path = Path(PurePath(original_path, self.cache_filename))
try:
remove(new_cache_path)
except: # noqa: E722 # TODO
raise RuntimeError(
f"Failed to remove previous cache file: {new_cache_path}"
)
remove(new_cache_path)
def store(self, name: str) -> None: def store(self, name: str) -> None:
""" """
@ -84,46 +60,22 @@ class Transpose:
3. Create the cache file 3. Create the cache file
4. Move the `target_path` to `store_path/name` 4. Move the `target_path` to `store_path/name`
5. Create symlink `target_path` -> `store_path/name` 5. Create symlink `target_path` -> `store_path/name`
Args:
name: The directory name to give the new location
Returns:
None
Raises
ValueError: Any paths not existing
RuntimeError: Any error during the actual path changes
""" """
new_location = Path(PurePath(self.store_path, name)) new_location = Path(PurePath(self.store_path, name))
if not check_path(path=self.target_path): if not check_path(path=self.target_path):
raise ValueError(
raise TransposeError(
f"Target path, {self.target_path}, does not exist. Cannot continue." f"Target path, {self.target_path}, does not exist. Cannot continue."
) )
if check_path(path=new_location): if check_path(path=new_location):
raise ValueError(
raise TransposeError(
f"Store path, {new_location}, already exists. Cannot continue." f"Store path, {new_location}, already exists. Cannot continue."
) )
try:
create_cache(
cache_path=self.cache_path,
original_path=self.target_path,
)
except: # noqa: E722 # TODO
raise RuntimeError("Failed to create cache file: {self.cache_path}")
try:
move(source=self.target_path, destination=new_location)
except: # noqa: E722 # TODO
raise RuntimeError(
f"Failed to move target to store path: {self.target_path} -> {self.new_location}"
)
create_cache(
cache_path=self.cache_path,
original_path=self.target_path,
)
try:
symlink(target_path=new_location, symlink_path=self.target_path)
except: # noqa: E722 # TODO
raise RuntimeError(
f"Failed to symlink store path to target: {new_location} -> {self.target_path}"
)
move(source=self.target_path, destination=new_location)
symlink(target_path=new_location, symlink_path=self.target_path)

+ 18
- 26
transpose/utils.py View File

@ -4,6 +4,7 @@ from typing import Dict
import json import json
from . import version from . import version
from .exceptions import TransposeError
def check_path(path: Path, is_symlink: bool = False) -> bool: def check_path(path: Path, is_symlink: bool = False) -> bool:
@ -61,42 +62,33 @@ def get_cache(cache_path: Path) -> Dict:
def move(source: Path, destination: Path) -> None: def move(source: Path, destination: Path) -> None:
""" """
Move a file using pathlib Move a file using pathlib
Args:
source: Path to original source of the file/directory
destination: Path to new destination
Returns:
None
""" """
source.rename(destination)
try:
source.rename(destination)
except FileExistsError:
raise TransposeError(f"Destination already exists: {destination}")
def remove(path: Path) -> None: def remove(path: Path) -> None:
""" """
Remove a file or symlink Remove a file or symlink
Does not support directories as a precaution and lack of need
Args:
path: Path to the file or symlink
Returns:
None
""" """
if path.is_symlink() or path.is_file():
if not path.is_symlink() and not path.is_file():
return
try:
path.unlink() path.unlink()
except FileNotFoundError:
raise TransposeError(f"Could not locate file or symlink: {path}")
def symlink(target_path: Path, symlink_path: Path) -> None: def symlink(target_path: Path, symlink_path: Path) -> None:
""" """
Symlinks a file or directory
Args:
target_path: Path to the target that is being symlinked to
symlink_path: Path to the symlink
Returns:
None
Symlink a file or directory
""" """
symlink_path.symlink_to(target_path)
try:
symlink_path.symlink_to(target_path)
except FileNotFoundError:
raise TransposeError(
f"Could not create symlink: {symlink_path} -> {target_path}"
)

Loading…
Cancel
Save