Skip to content

Commit 3b02ee2

Browse files
committed
removed __bytes_repr__ implementation from fileset and mock, pydra can call byte_chunks directly
1 parent feeadef commit 3b02ee2

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

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

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

16391630
@classproperty
16401631
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 FileFormatsError
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)