Skip to content

Commit

Permalink
Replace filename
Browse files Browse the repository at this point in the history
  • Loading branch information
talmo committed Apr 14, 2024
1 parent ef4d30a commit 2933124
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions sleap_io/model/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,22 @@ def close(self):
if self.backend is not None:
del self.backend
self.backend = None

def replace_filename(self, new_filename: str | Path, open: bool = True):
"""Update the filename of the video, optionally opening the backend.
Args:
new_filename: New filename to set for the video.
open: If `True` (the default), open the backend with the new filename. If
the new filename does not exist, no error is raised.
"""
if isinstance(new_filename, Path):
new_filename = str(new_filename)

self.filename = new_filename

if open:
if self.exists():
self.open()
else:
self.close()
19 changes: 19 additions & 0 deletions tests/model/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,22 @@ def test_video_open_close(centered_pair_low_quality_path):
assert video.shape == (1100, 384, 384, 3)
video.open(grayscale=True)
assert video.shape == (1100, 384, 384, 1)


def test_video_replace_filename(centered_pair_low_quality_path):
video = Video.from_filename("test.mp4")
assert video.exists() is False

video.replace_filename(centered_pair_low_quality_path)
assert video.exists() is True
assert video.is_open is True
assert type(video.backend) == MediaVideo

video.replace_filename("test.mp4")
assert video.exists() is False
assert video.is_open is False

video.replace_filename(centered_pair_low_quality_path, open=False)
assert video.exists() is True
assert video.is_open is False
assert video.backend is None

0 comments on commit 2933124

Please sign in to comment.