♻️ refactor to return FileLocation again

This commit is contained in:
Ondřej Samohel 2024-12-01 23:00:44 +01:00
parent cc543fb6a2
commit cf195c44a6
No known key found for this signature in database
GPG key ID: 02376E18990A97C6
2 changed files with 16 additions and 17 deletions

View file

@ -82,7 +82,6 @@ class FileLocation(TraitBase):
file_hash (str): File hash.
"""
name: ClassVar[str] = "FileLocation"
description: ClassVar[str] = "FileLocation Trait Model"
id: ClassVar[str] = "ayon.content.FileLocation.v1"
@ -122,11 +121,11 @@ class FileLocations(TraitBase):
for file_location in self.file_paths:
yield file_location.file_path
def get_file_for_frame(
def get_file_location_for_frame(
self,
frame: int,
sequence_trait: Optional[Sequence] = None,
) -> Optional[Path]:
) -> Optional[FileLocation]:
"""Get file location for a frame.
This method will return the file location for a given frame. If the
@ -138,7 +137,7 @@ class FileLocations(TraitBase):
frame range specs from.
Returns:
Optional[Path]: File location for the frame.
Optional[FileLocation]: File location for the frame.
"""
frame_regex = r"\.(?P<frame>(?P<padding>0*)\d+)\.\D+\d?$"
@ -146,13 +145,12 @@ class FileLocations(TraitBase):
frame_regex = sequence_trait.frame_regex
re.compile(frame_regex)
for file_path in self.get_files():
result = re.search(frame_regex, file_path.name)
for location in self.file_paths:
result = re.search(frame_regex, location.file_path.name)
if result:
frame_index = int(result.group("frame"))
if frame_index == frame:
return file_path
return location
return None
def validate(self, representation: Representation) -> None:

View file

@ -140,8 +140,9 @@ def test_file_locations_validation() -> None:
with pytest.raises(TraitValidationError):
representation.validate()
def test_get_file_from_frame() -> None:
"""Test get_file_from_frame method."""
def test_get_file_location_from_frame() -> None:
"""Test get_file_location_from_frame method."""
file_locations_list = [
FileLocation(
file_path=Path(f"/path/to/file.{frame}.exr"),
@ -154,11 +155,11 @@ def test_get_file_from_frame() -> None:
file_locations_trait: FileLocations = FileLocations(
file_paths=file_locations_list)
assert file_locations_trait.get_file_for_frame(frame=1001) == \
file_locations_list[0].file_path
assert file_locations_trait.get_file_for_frame(frame=1050) == \
file_locations_list[-1].file_path
assert file_locations_trait.get_file_for_frame(frame=1100) is None
assert file_locations_trait.get_file_location_for_frame(frame=1001) == \
file_locations_list[0]
assert file_locations_trait.get_file_location_for_frame(frame=1050) == \
file_locations_list[-1]
assert file_locations_trait.get_file_location_for_frame(frame=1100) is None
# test with custom regex
sequence = Sequence(
@ -176,6 +177,6 @@ def test_get_file_from_frame() -> None:
file_locations_trait: FileLocations = FileLocations(
file_paths=file_locations_list)
assert file_locations_trait.get_file_for_frame(
assert file_locations_trait.get_file_location_for_frame(
frame=1001, sequence_trait=sequence) == \
file_locations_list[0].file_path
file_locations_list[0]