refactor(Track): Remove swap() method and it's uses

Re-using the same track path and file name with a different output file, is not ideal as the files contents are different and the target file name specifies what processing it had done on it, which is useful during debugging when browsing the temp directory.
This commit is contained in:
rlaphoenix 2024-03-01 03:03:12 +00:00
parent 470e051100
commit ba693e214b
3 changed files with 4 additions and 35 deletions

View File

@ -214,7 +214,7 @@ class Subtitle(Track):
output_path.write_text(subtitle_text, encoding="utf8")
self.swap(output_path)
self.path = output_path
self.codec = codec
if callable(self.OnConverted):

View File

@ -299,8 +299,7 @@ class Track:
else:
raise
self.swap(output_path)
self.move(original_path)
self.path = output_path
def move(self, target: Union[Path, str]) -> bool:
"""
@ -330,34 +329,5 @@ class Track:
return success
def swap(self, target: Union[Path, str]) -> bool:
"""
Delete the Track's file and swap to the Target file.
Raises:
TypeError: If the target argument is not the expected type.
OSError: If the file somehow failed to move.
Returns True if the swap succeeded, or False if the track is not yet downloaded,
or the target path does not exist.
"""
if not isinstance(target, (str, Path)):
raise TypeError(f"Expected {target} to be a {Path} or {str}, not {type(target)}")
target = Path(target)
if not target.exists() or not self.path:
return False
self.path.unlink()
moved_to = Path(shutil.move(target, self.path))
success = moved_to.resolve() == self.path.resolve()
if not success:
return False
return self.move(target)
__all__ = ("Track",)

View File

@ -200,8 +200,7 @@ class Video(Track):
str(output_path)
], check=True)
self.swap(output_path)
self.move(original_path)
self.path = output_path
def ccextractor(
self, track_id: Any, out_path: Union[Path, str], language: Language, original: bool = False
@ -335,7 +334,7 @@ class Video(Track):
log.info(" + Removed")
self.swap(cleaned_path)
self.path = cleaned_path
return True