Skip to content

Commit dd6fa8a

Browse files
tcloseeffigies
authored andcommitted
implement bytes_repr in pydra instead of in fileformats
1 parent 88e4e0b commit dd6fa8a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

pydra/utils/hash.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
)
5656

5757
Hash = NewType("Hash", bytes)
58-
CacheKey = NewType("CacheKey", ty.Tuple[ty.Hashable, ty.Hashable])
58+
CacheKey = NewType("CacheKey", ty.Tuple[ty.Hashable, ...])
5959

6060

6161
def location_converter(path: ty.Union[Path, str, None]) -> Path:
@@ -478,18 +478,29 @@ def bytes_repr_fileset(
478478
fileset: FileSet, cache: Cache
479479
) -> Iterator[ty.Union[CacheKey, bytes]]:
480480
fspaths = sorted(fileset.fspaths)
481+
# Yield the cache key for the fileset, which is a tuple of the file-system paths
482+
# and their mtime. Is used to store persistent cache of the fileset hashes
483+
# to avoid recomputation between calls
481484
yield CacheKey(
482485
tuple(repr(p) for p in fspaths) # type: ignore[arg-type]
483486
+ tuple(p.lstat().st_mtime_ns for p in fspaths)
484487
)
485-
yield from fileset.__bytes_repr__(cache)
488+
cls = type(fileset)
489+
yield f"{cls.__module__}.{cls.__name__}:".encode()
490+
for key, chunk_iter in fileset.byte_chunks():
491+
yield (",'" + key + "'=").encode()
492+
yield from chunk_iter
486493

487494

495+
# Need to disable the mtime cache key for mocked filesets. Used in doctests
488496
@register_serializer(MockMixin)
489497
def bytes_repr_mock_fileset(
490498
mock_fileset: MockMixin, cache: Cache
491499
) -> Iterator[ty.Union[CacheKey, bytes]]:
492-
yield from mock_fileset.__bytes_repr__(cache)
500+
cls = type(mock_fileset)
501+
yield f"{cls.__module__}.{cls.__name__}:".encode()
502+
for key, _ in mock_fileset.byte_chunks():
503+
yield (",'" + key + "'").encode()
493504

494505

495506
@register_serializer(list)

0 commit comments

Comments
 (0)