From 31006b9763087cfbcceeef1b3dbb5a14156aede6 Mon Sep 17 00:00:00 2001 From: Ryan Reed Date: Wed, 6 Sep 2023 20:48:21 -0400 Subject: [PATCH] Cleaning up paths and error messages --- src/transpose/__init__.py | 2 +- src/transpose/transpose.py | 37 ++++++++++++++++--------------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/transpose/__init__.py b/src/transpose/__init__.py index 6526dc8..3e467f4 100644 --- a/src/transpose/__init__.py +++ b/src/transpose/__init__.py @@ -12,4 +12,4 @@ version = version("transpose") logger = create_logger(__package__) -from .transpose import Transpose # noqa: E402 +from .transpose import Transpose, TransposeConfig, TransposeEntry # noqa: E402 diff --git a/src/transpose/transpose.py b/src/transpose/transpose.py index c1e02b3..3fea08b 100644 --- a/src/transpose/transpose.py +++ b/src/transpose/transpose.py @@ -35,7 +35,7 @@ class TransposeConfig: if self.entries.get(name): raise TransposeError(f"'{name}' already exists") - self.entries[name] = TransposeEntry(name=name, path=path) + self.entries[name] = TransposeEntry(name=name, path=str(path)) def get(self, name: str) -> TransposeEntry: """ @@ -140,22 +140,22 @@ class Transpose: None """ if not self.config.entries.get(name): - raise TransposeError(f"Entry does not exist: '{name}'") + raise TransposeError(f"Entry does not exist: '{name}'") - entry = self.config.entries[name] - if entry.path.exists(): - if entry.path.is_symlink(): - remove(entry.path) + entry_path = Path(self.config.entries[name].path) + if entry_path.exists(): + if entry_path.is_symlink(): + remove(entry_path) elif force: # Backup the existing path, just in case - move(entry.path, entry.path.joinpath("-bak")) + move(entry_path, entry_path.with_suffix(".backup")) else: raise TransposeError( - f"Entry path already exists, cannot restore (force required): '{entry.path}'" + f"Entry path already exists, cannot apply (force required): '{entry_path}'" ) symlink( target_path=self.store_path.joinpath(name), - symlink_path=entry.path, + symlink_path=entry_path, ) def restore(self, name: str, force: bool = False) -> None: @@ -172,18 +172,18 @@ class Transpose: if not self.config.entries.get(name): raise TransposeError(f"Could not locate entry by name: '{name}'") - entry = self.config.entries[name] - if entry.path.exists(): - if entry.path.is_symlink(): - remove(entry.path) + entry_path = Path(self.config.entries[name].path) + if entry_path.exists(): + if entry_path.is_symlink(): + remove(entry_path) elif force: # Backup the existing path, just in case - move(entry.path, entry.path.joinpath("-bak")) + move(entry_path, entry_path.with_suffix(".backup")) else: raise TransposeError( - f"Entry path already exists, cannot restore (force required): '{entry.path}'" + f"Entry path already exists, cannot restore (force required): '{entry_path}'" ) - move(self.store_path.joinpath(name), entry.path) + move(self.store_path.joinpath(name), entry_path) self.config.remove(name) self.config.save(self.config_path) @@ -212,11 +212,6 @@ class Transpose: if not source_path.exists(): raise TransposeError(f"Source path does not exist: '{source_path}'") - if not source_path.is_dir() and not source_path.is_file(): - raise TransposeError( - f"Source path must be a directory or file: '{source_path}'" - ) - move(source=source_path, destination=storage_path) symlink(target_path=storage_path, symlink_path=source_path)