Skip to content

Commit b28a073

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

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

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

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

16451636
@classproperty
16461637
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)