Skip to content

Commit

Permalink
implement bytes_repr in pydra instead of in fileformats
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Mar 26, 2024
1 parent 2ed3efa commit bdf6abf
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pydra/utils/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
)

Hash = NewType("Hash", bytes)
CacheKey = NewType("CacheKey", ty.Tuple[ty.Hashable, ty.Hashable])
CacheKey = NewType("CacheKey", ty.Tuple[ty.Hashable, ...])


def location_converter(path: ty.Union[Path, str, None]) -> Path:
Expand Down Expand Up @@ -472,18 +472,29 @@ def bytes_repr_fileset(
fileset: FileSet, cache: Cache
) -> Iterator[ty.Union[CacheKey, bytes]]:
fspaths = sorted(fileset.fspaths)
# Yield the cache key for the fileset, which is a tuple of the file-system paths
# and their mtime. Is used to store persistent cache of the fileset hashes
# to avoid recomputation between calls
yield CacheKey(
tuple(repr(p) for p in fspaths) # type: ignore[arg-type]
+ tuple(p.lstat().st_mtime_ns for p in fspaths)
)
yield from fileset.__bytes_repr__(cache)
cls = type(fileset)
yield f"{cls.__module__}.{cls.__name__}:".encode()
for key, chunk_iter in fileset.byte_chunks():
yield (",'" + key + "'=").encode()
yield from chunk_iter


# Need to disable the mtime cache key for mocked filesets. Used in doctests
@register_serializer(MockMixin)
def bytes_repr_mock_fileset(
mock_fileset: MockMixin, cache: Cache
) -> Iterator[ty.Union[CacheKey, bytes]]:
yield from mock_fileset.__bytes_repr__(cache)
cls = type(mock_fileset)
yield f"{cls.__module__}.{cls.__name__}:".encode()

Check warning on line 495 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L494-L495

Added lines #L494 - L495 were not covered by tests
for key, _ in mock_fileset.byte_chunks():
yield (",'" + key + "'").encode()

Check warning on line 497 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L497

Added line #L497 was not covered by tests


@register_serializer(list)
Expand Down

0 comments on commit bdf6abf

Please sign in to comment.