Skip to content

Commit 6bf4d86

Browse files
committed
removed __bytes_repr__ implementation from fileset and mock, pydra can call byte_chunks directly
1 parent 1e75998 commit 6bf4d86

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
@@ -835,30 +835,6 @@ def hash_files(self, crypto=None, **kwargs) -> ty.Dict[str, bytes]:
835835
file_hashes[str(path)] = crypto_obj.hexdigest()
836836
return file_hashes
837837

838-
def __bytes_repr__(
839-
self, cache: dict # pylint: disable=unused-argument
840-
) -> ty.Iterable[bytes]:
841-
"""Provided for compatibility with Pydra's hashing function, return the contents
842-
of all the files in the file-set in chunks
843-
844-
Parameters
845-
----------
846-
cache : dict
847-
an object passed around by Pydra's hashing function to store cached versions
848-
of previously hashed objects, to allow recursive structures
849-
850-
Yields
851-
------
852-
bytes
853-
a chunk of bytes of length FILE_CHUNK_LEN_DEFAULT from the contents of all
854-
files in the file-set.
855-
"""
856-
cls = type(self)
857-
yield f"{cls.__module__}.{cls.__name__}:".encode()
858-
for key, chunk_iter in self.byte_chunks():
859-
yield (",'" + key + "'=").encode()
860-
yield from chunk_iter
861-
862838
@classmethod
863839
def referenced_types(cls) -> ty.Set[Classifier]:
864840
"""Returns a flattened list of nested types referenced within the fileset type
@@ -1634,8 +1610,23 @@ def type_name(cls):
16341610
assert cls.__name__.endswith("Mock")
16351611
return cls.__name__[: -len("Mock")]
16361612

1637-
def __bytes_repr__(self, cache):
1638-
yield from (str(fspath).encode() for fspath in self.fspaths)
1613+
def byte_chunks(
1614+
self,
1615+
mtime: bool = False,
1616+
chunk_len=FILE_CHUNK_LEN_DEFAULT,
1617+
relative_to: ty.Optional[os.PathLike] = None,
1618+
ignore_hidden_files: bool = False,
1619+
ignore_hidden_dirs: bool = False,
1620+
):
1621+
if relative_to is None:
1622+
relative_to = os.path.commonpath(self.fspaths)
1623+
else:
1624+
relative_to = str(relative_to)
1625+
for key, fspath in sorted(
1626+
((str(p)[len(relative_to) :], p) for p in self.fspaths),
1627+
key=itemgetter(0),
1628+
):
1629+
yield (key, iter([key.encode()])) # empty iterator as files don't exist
16391630

16401631
@classproperty
16411632
def namespace(cls):

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, hook
8-
from fileformats.generic import File, Directory, FsObject
7+
from fileformats.core import FileSet, MockMixin, hook
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 conftest import write_test_file
@@ -53,6 +54,11 @@ def fsobject(luigi_file, bowser_dir, request):
5354
assert False
5455

5556

57+
@pytest.fixture
58+
def mock_fileset():
59+
return SetOf[TextFile].mock("/path/to/a/mock", "/path/to/another/mock")
60+
61+
5662
@pytest.fixture
5763
def dest_dir(work_dir):
5864
dest_dir = work_dir / "new-dir"
@@ -367,3 +373,10 @@ def test_hash_files(fsobject: FsObject, work_dir: Path, dest_dir: Path):
367373
)
368374
cpy = fsobject.copy(dest_dir)
369375
assert cpy.hash_files() == fsobject.hash_files()
376+
377+
378+
def test_hash_mock_files(mock_fileset: MockMixin, work_dir: Path, dest_dir: Path):
379+
file_hashes = mock_fileset.hash_files(relative_to="")
380+
assert sorted(Path(p) for p in file_hashes) == sorted(
381+
p for p in mock_fileset.fspaths
382+
)

0 commit comments

Comments
 (0)