Skip to content

Commit

Permalink
refactor(Track): Remove swap() method and it's uses
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rlaphoenix committed Mar 1, 2024
1 parent 470e051 commit ba693e2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 35 deletions.
2 changes: 1 addition & 1 deletion devine/core/tracks/subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def convert(self, codec: Subtitle.Codec) -> Path:

output_path.write_text(subtitle_text, encoding="utf8")

self.swap(output_path)
self.path = output_path
self.codec = codec

if callable(self.OnConverted):
Expand Down
32 changes: 1 addition & 31 deletions devine/core/tracks/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ def _ffmpeg(extra_args: list[str] = None):
else:
raise

self.swap(output_path)
self.move(original_path)
self.path = output_path

def move(self, target: Union[Path, str]) -> bool:
"""
Expand Down Expand Up @@ -330,34 +329,5 @@ def move(self, target: Union[Path, str]) -> bool:

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",)
5 changes: 2 additions & 3 deletions devine/core/tracks/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ def change_color_range(self, range_: int) -> None:
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
Expand Down Expand Up @@ -335,7 +334,7 @@ def remove_eia_cc(self) -> bool:

log.info(" + Removed")

self.swap(cleaned_path)
self.path = cleaned_path

return True

Expand Down

0 comments on commit ba693e2

Please sign in to comment.