Skip to content

Commit 894cd2a

Browse files
committed
removed __bytes_repr__ implementation from fileset and mock, pydra can call byte_chunks directly
1 parent a713d29 commit 894cd2a

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

fileformats/core/fileset.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -887,30 +887,6 @@ def hash_files(
887887
file_hashes[str(path)] = crypto_obj.hexdigest()
888888
return file_hashes
889889

890-
def __bytes_repr__(
891-
self, cache: ty.Dict[ty.Any, str] # pylint: disable=unused-argument
892-
) -> ty.Iterable[bytes]:
893-
"""Provided for compatibility with Pydra's hashing function, return the contents
894-
of all the files in the file-set in chunks
895-
896-
Parameters
897-
----------
898-
cache : dict[Any, str]
899-
an object passed around by Pydra's hashing function to store cached versions
900-
of previously hashed objects, to allow recursive structures
901-
902-
Yields
903-
------
904-
bytes
905-
a chunk of bytes of length FILE_CHUNK_LEN_DEFAULT from the contents of all
906-
files in the file-set.
907-
"""
908-
cls = type(self)
909-
yield f"{cls.__module__}.{cls.__name__}:".encode()
910-
for key, chunk_iter in self.byte_chunks():
911-
yield (",'" + key + "'=").encode()
912-
yield from chunk_iter
913-
914890
@classmethod
915891
def referenced_types(cls) -> ty.Set[ty.Type[Classifier]]:
916892
"""Returns a flattened list of nested types referenced within the fileset type
@@ -1701,8 +1677,23 @@ def type_name(cls) -> str:
17011677
assert class_name.endswith("Mock")
17021678
return class_name[: -len("Mock")]
17031679

1704-
def __bytes_repr__(self, cache: ty.Dict[str, ty.Any]) -> ty.Iterable[bytes]:
1705-
yield from (str(fspath).encode() for fspath in self.fspaths)
1680+
def byte_chunks(
1681+
self,
1682+
mtime: bool = False,
1683+
chunk_len=FILE_CHUNK_LEN_DEFAULT,
1684+
relative_to: ty.Optional[os.PathLike] = None,
1685+
ignore_hidden_files: bool = False,
1686+
ignore_hidden_dirs: bool = False,
1687+
):
1688+
if relative_to is None:
1689+
relative_to = os.path.commonpath(self.fspaths)
1690+
else:
1691+
relative_to = str(relative_to)
1692+
for key, fspath in sorted(
1693+
((str(p)[len(relative_to) :], p) for p in self.fspaths),
1694+
key=itemgetter(0),
1695+
):
1696+
yield (key, iter([key.encode()])) # empty iterator as files don't exist
17061697

17071698
@classproperty
17081699
def namespace(cls) -> str:

fileformats/core/tests/test_utils.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import shutil
55
import time
66
import pytest
7-
from fileformats.core import FileSet
8-
from fileformats.generic import File, Directory, FsObject
7+
from fileformats.core import FileSet, MockMixin
8+
from fileformats.generic import File, Directory, FsObject, SetOf
9+
from fileformats.text import TextFile
910
from fileformats.core.mixin import WithSeparateHeader
1011
from fileformats.core.exceptions import UnsatisfiableCopyModeError
1112
from fileformats.core.utils import mtime_cached_property
@@ -54,6 +55,11 @@ def fsobject(luigi_file, bowser_dir, request):
5455
assert False
5556

5657

58+
@pytest.fixture
59+
def mock_fileset():
60+
return SetOf[TextFile].mock("/path/to/a/mock", "/path/to/another/mock")
61+
62+
5763
@pytest.fixture
5864
def dest_dir(work_dir):
5965
dest_dir = work_dir / "new-dir"
@@ -407,3 +413,10 @@ def test_mtime_cached_property_force_clear(tmp_path: Path):
407413
file.flag = 1
408414
MtimeTestFile.cached_prop.clear(file)
409415
assert file.cached_prop == 1
416+
417+
418+
def test_hash_mock_files(mock_fileset: MockMixin, work_dir: Path, dest_dir: Path):
419+
file_hashes = mock_fileset.hash_files(relative_to="")
420+
assert sorted(Path(p) for p in file_hashes) == sorted(
421+
p for p in mock_fileset.fspaths
422+
)

0 commit comments

Comments
 (0)