Skip to content

Commit ce10332

Browse files
authored
Merge pull request #743 from nipype/mock-bytes-repr
Special handling of bytes_repr for mock filesets
2 parents 873f8a0 + dd6fa8a commit ce10332

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

pydra/utils/hash.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121
from filelock import SoftFileLock
2222
import attrs.exceptions
23-
from fileformats.core import FileSet
23+
from fileformats.core.fileset import FileSet, MockMixin
2424
from . import user_cache_dir, add_exc_note
2525

2626
logger = logging.getLogger("pydra")
@@ -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,11 +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
493+
494+
495+
# Need to disable the mtime cache key for mocked filesets. Used in doctests
496+
@register_serializer(MockMixin)
497+
def bytes_repr_mock_fileset(
498+
mock_fileset: MockMixin, cache: Cache
499+
) -> Iterator[ty.Union[CacheKey, bytes]]:
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()
486504

487505

488506
@register_serializer(list)

0 commit comments

Comments
 (0)