Skip to content

Commit f719dc6

Browse files
committed
crate-with-subcrate creation: support zipped output
1 parent 786b2b1 commit f719dc6

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

rocrate/rocrate.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,10 @@ def _stream_zip(self, chunk_size=8192, out_path=None):
652652
while chunk := buffer.read(chunk_size):
653653
yield chunk
654654

655+
def _all_streams(self, chunk_size=8192):
656+
for writeable_entity in self.data_entities + self.default_entities:
657+
yield from writeable_entity.stream(chunk_size=chunk_size)
658+
655659
def add_workflow(
656660
self, source=None, dest_path=None, fetch_remote=False, validate_url=False, properties=None,
657661
main=False, lang="cwl", lang_version=None, gen_cwl=False, cls=ComputationalWorkflow,
@@ -924,6 +928,12 @@ def write(self, base_path):
924928
if self.crate.mode == Mode.CREATE:
925929
self.get_crate().write(base_path / unquote(self.id))
926930

931+
def stream(self, chunk_size=8192):
932+
yield from super().stream(chunk_size=chunk_size)
933+
if self.crate.mode == Mode.CREATE:
934+
for path, chunk in self.get_crate()._all_streams(chunk_size=chunk_size):
935+
yield os.path.join(unquote(self.id), path), chunk
936+
927937

928938
def make_workflow_rocrate(workflow_path, wf_type, include_files=[],
929939
fetch_remote=False, cwl=None, diagram=None):

test/test_write.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def test_write_subcrate(test_data_dir, tmpdir, to_zip):
634634
crate = ROCrate(test_data_dir / "crate_with_subcrates", load_subcrates=True)
635635
out_path = tmpdir / "ro_crate_out"
636636
if to_zip:
637-
zip_path = tmpdir / 'ro_crate_out.crate.zip'
637+
zip_path = tmpdir / 'ro_crate_out.zip'
638638
crate.write_zip(zip_path)
639639
with zipfile.ZipFile(zip_path, "r") as zf:
640640
zf.extractall(out_path)
@@ -651,7 +651,8 @@ def test_write_subcrate(test_data_dir, tmpdir, to_zip):
651651
assert (out_path / "subcrate" / "subsubcrate" / "ro-crate-metadata.json").is_file()
652652

653653

654-
def test_subcrates_creation(test_data_dir, tmpdir):
654+
@pytest.mark.parametrize("to_zip", [False, True])
655+
def test_subcrates_creation(test_data_dir, tmpdir, to_zip):
655656
crate = ROCrate()
656657
crate.add_file(test_data_dir / "sample_file.txt")
657658
subcrate = crate.add_subcrate(dest_path="subcrate/")
@@ -671,7 +672,14 @@ def test_subcrates_creation(test_data_dir, tmpdir):
671672
assert subcrate_crate.get("subsubcrate/setup.cfg") is subsubf
672673

673674
out_path = tmpdir / "ro_crate_out"
674-
crate.write(out_path)
675+
if to_zip:
676+
zip_path = tmpdir / 'ro_crate_out.zip'
677+
crate.write_zip(zip_path)
678+
with zipfile.ZipFile(zip_path, "r") as zf:
679+
zf.extractall(out_path)
680+
else:
681+
crate.write(out_path)
682+
675683
assert (out_path / "ro-crate-metadata.json").is_file()
676684
assert (out_path / "sample_file.txt").is_file()
677685
assert (out_path / "subcrate" / "ro-crate-metadata.json").is_file()

0 commit comments

Comments
 (0)