Skip to content

Commit b08b0f0

Browse files
committed
removed __bytes_repr__ implementation from fileset and mock, pydra can call byte_chunks directly
1 parent 9f45ec4 commit b08b0f0

File tree

2 files changed

+61
-26
lines changed

2 files changed

+61
-26
lines changed

fileformats/core/fileset.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -949,30 +949,6 @@ def hash_files(
949949
file_hashes[str(path)] = crypto_obj.hexdigest()
950950
return file_hashes
951951

952-
def __bytes_repr__(
953-
self, cache: ty.Dict[ty.Any, str] # pylint: disable=unused-argument
954-
) -> ty.Iterable[bytes]:
955-
"""Provided for compatibility with Pydra's hashing function, return the contents
956-
of all the files in the file-set in chunks
957-
958-
Parameters
959-
----------
960-
cache : dict[Any, str]
961-
an object passed around by Pydra's hashing function to store cached versions
962-
of previously hashed objects, to allow recursive structures
963-
964-
Yields
965-
------
966-
bytes
967-
a chunk of bytes of length FILE_CHUNK_LEN_DEFAULT from the contents of all
968-
files in the file-set.
969-
"""
970-
cls = type(self)
971-
yield f"{cls.__module__}.{cls.__name__}:".encode()
972-
for key, chunk_iter in self.byte_chunks():
973-
yield (",'" + key + "'=").encode()
974-
yield from chunk_iter
975-
976952
@classmethod
977953
def referenced_types(cls) -> ty.Set[ty.Type[Classifier]]:
978954
"""Returns a flattened list of nested types referenced within the fileset type

fileformats/core/tests/test_utils.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
import time
66
import typing as ty
77
import pytest
8-
from fileformats.core import FileSet, validated_property
9-
from fileformats.generic import File, BinaryFile, Directory, FsObject
8+
from fileformats.generic import File, BinaryFile, Directory, FsObject, SetOf
9+
from fileformats.core import (
10+
FileSet,
11+
MockMixin,
12+
validated_property,
13+
mtime_cached_property,
14+
)
15+
from fileformats.text import TextFile
1016
from fileformats.core.mixin import WithSeparateHeader
1117
from fileformats.core.exceptions import UnsatisfiableCopyModeError
1218
from conftest import write_test_file
@@ -54,6 +60,11 @@ def fsobject(luigi_file, bowser_dir, request):
5460
assert False
5561

5662

63+
@pytest.fixture
64+
def mock_fileset():
65+
return SetOf[TextFile].mock("/path/to/a/mock", "/path/to/another/mock")
66+
67+
5768
@pytest.fixture
5869
def dest_dir(work_dir):
5970
dest_dir = work_dir / "new-dir"
@@ -523,3 +534,51 @@ def test_hash_files(fsobject: FsObject, work_dir: Path, dest_dir: Path):
523534
)
524535
cpy = fsobject.copy(dest_dir)
525536
assert cpy.hash_files() == fsobject.hash_files()
537+
538+
539+
class MtimeTestFile(File):
540+
541+
flag: int
542+
543+
@mtime_cached_property
544+
def cached_prop(self):
545+
return self.flag
546+
547+
548+
def test_mtime_cached_property(tmp_path: Path):
549+
fspath = tmp_path / "file_1.txt"
550+
fspath.write_text("hello")
551+
552+
file = MtimeTestFile(fspath)
553+
554+
file.flag = 0
555+
assert file.cached_prop == 0
556+
# Need a long delay to ensure the mtime changes on Ubuntu and particularly on Windows
557+
# On MacOS, the mtime resolution is much higher so not usually an issue. Use
558+
# explicitly cache clearing if needed
559+
time.sleep(2)
560+
file.flag = 1
561+
assert file.cached_prop == 0
562+
time.sleep(2)
563+
fspath.write_text("world")
564+
assert file.cached_prop == 1
565+
566+
567+
def test_mtime_cached_property_force_clear(tmp_path: Path):
568+
fspath = tmp_path / "file_1.txt"
569+
fspath.write_text("hello")
570+
571+
file = MtimeTestFile(fspath)
572+
573+
file.flag = 0
574+
assert file.cached_prop == 0
575+
file.flag = 1
576+
MtimeTestFile.cached_prop.clear(file)
577+
assert file.cached_prop == 1
578+
579+
580+
def test_hash_mock_files(mock_fileset: MockMixin, work_dir: Path, dest_dir: Path):
581+
file_hashes = mock_fileset.hash_files(relative_to="")
582+
assert sorted(Path(p) for p in file_hashes) == sorted(
583+
p for p in mock_fileset.fspaths
584+
)

0 commit comments

Comments
 (0)