refactor(Track): Return new path on move(), raise exceptions on errors

This commit is contained in:
rlaphoenix 2024-03-01 04:14:44 +00:00
parent 3ceabd0c74
commit 866de402fb
1 changed files with 12 additions and 9 deletions

View File

@ -307,33 +307,36 @@ class Track:
self.path = output_path self.path = output_path
def move(self, target: Union[Path, str]) -> bool: def move(self, target: Union[Path, str]) -> Path:
""" """
Move the Track's file from current location, to target location. Move the Track's file from current location, to target location.
This will overwrite anything at the target path. This will overwrite anything at the target path.
Raises: Raises:
TypeError: If the target argument is not the expected type. TypeError: If the target argument is not the expected type.
ValueError: If track has no file to move, or the target does not exist.
OSError: If the file somehow failed to move.
Returns True if the move succeeded, or False if there was no file to move, or Returns the new location of the track.
the file failed to move.
""" """
if not isinstance(target, (str, Path)): if not isinstance(target, (str, Path)):
raise TypeError(f"Expected {target} to be a {Path} or {str}, not {type(target)}") raise TypeError(f"Expected {target} to be a {Path} or {str}, not {type(target)}")
if not self.path: if not self.path:
return False raise ValueError("Track has no file to move")
if not isinstance(target, Path): if not isinstance(target, Path):
target = Path(target) target = Path(target)
if not target.exists():
raise ValueError(f"Target file {repr(target)} does not exist")
moved_to = Path(shutil.move(self.path, target)) moved_to = Path(shutil.move(self.path, target))
success = moved_to.resolve() == target.resolve() if moved_to.resolve() != target.resolve():
raise OSError(f"Failed to move {self.path} to {target}")
if success:
self.path = target self.path = target
return target
return success
__all__ = ("Track",) __all__ = ("Track",)