Skip to content

Commit efc865c

Browse files
authored
Merge pull request #212 from dnlbauer/stream_crate
Stream RO-Crate Zip
2 parents e22ff83 + 04128fa commit efc865c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+448
-80
lines changed

CITATION.cff

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
cff-version: 1.1.0
22
message: "Cite as"
3-
authors:
3+
author:
4+
- family-names: Bauer
5+
given-names: Daniel
6+
orcid: https://orcid.org/0000-0001-9447-460X
47
- family-names: Chadwick
58
given-names: Eli
69
orcid: https://orcid.org/0000-0002-0035-6475

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ Options:
451451
* Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
452452
* Copyright 2024 Data Centre, SciLifeLab, SE
453453
* Copyright 2024 National Institute of Informatics (NII), JP
454+
* Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
454455

455456
Licensed under the
456457
Apache License, version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>,

examples/fastapi/main.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2019-2024 The University of Manchester, UK
2+
# Copyright 2020-2024 Vlaams Instituut voor Biotechnologie (VIB), BE
3+
# Copyright 2020-2024 Barcelona Supercomputing Center (BSC), ES
4+
# Copyright 2020-2024 Center for Advanced Studies, Research and Development in Sardinia (CRS4), IT
5+
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
6+
# Copyright 2024 Data Centre, SciLifeLab, SE
7+
# Copyright 2024 National Institute of Informatics (NII), JP
8+
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
9+
#
10+
# Licensed under the Apache License, Version 2.0 (the "License");
11+
# you may not use this file except in compliance with the License.
12+
# You may obtain a copy of the License at
13+
#
14+
# http://www.apache.org/licenses/LICENSE-2.0
15+
#
16+
# Unless required by applicable law or agreed to in writing, software
17+
# distributed under the License is distributed on an "AS IS" BASIS,
18+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
# See the License for the specific language governing permissions and
20+
# limitations under the License.
21+
22+
"""
23+
Streaming RO-Crates from a web server
24+
25+
This example demonstrates how to create an RO-Crate on-the-fly
26+
and stream the result to the client.
27+
By using `stream_zip`, the RO-Crate is not written to disk and remote
28+
data is only fetched on the fly.
29+
30+
To run: `fastapi dev main.py`, then visit http://localhost:8000/crate
31+
"""
32+
33+
from fastapi import FastAPI
34+
from fastapi.responses import StreamingResponse
35+
from rocrate.rocrate import ROCrate
36+
from io import StringIO
37+
38+
app = FastAPI()
39+
40+
41+
@app.get("/crate")
42+
async def get():
43+
crate = ROCrate()
44+
45+
# Add a remote file
46+
crate.add_file(
47+
"https://raw.githubusercontent.com/ResearchObject/ro-crate-py/refs/heads/master/test/test-data/sample_file.txt",
48+
fetch_remote=True
49+
)
50+
51+
# Add a file containing a string to the crate
52+
crate.add_file(
53+
source=StringIO("Hello, World!"),
54+
dest_path="test-data/hello.txt"
55+
)
56+
57+
# Stream crate to client as a zip file
58+
return StreamingResponse(
59+
crate.stream_zip(),
60+
media_type="application/rocrate+zip",
61+
headers={
62+
"Content-Disposition": "attachment; filename=crate.zip",
63+
}
64+
)

examples/fastapi/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
../../
2+
fastapi
3+
fastapi-cli

examples/read_test_metadata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
66
# Copyright 2024 Data Centre, SciLifeLab, SE
77
# Copyright 2024 National Institute of Informatics (NII), JP
8+
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
89
#
910
# Licensed under the Apache License, Version 2.0 (the "License");
1011
# you may not use this file except in compliance with the License.

rocrate/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
88
# Copyright 2024 Data Centre, SciLifeLab, SE
99
# Copyright 2024 National Institute of Informatics (NII), JP
10+
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
1011
#
1112
# Licensed under the Apache License, Version 2.0 (the "License");
1213
# you may not use this file except in compliance with the License.
@@ -30,6 +31,7 @@
3031
"""
3132

3233
__author__ = ", ".join((
34+
'Daniel Bauer',
3335
'Eli Chadwick',
3436
'Paul De Geest',
3537
'Bert Droesbeke',
@@ -52,6 +54,7 @@
5254
Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
5355
Copyright 2024 Data Centre, SciLifeLab, SE
5456
Copyright 2024 National Institute of Informatics (NII), JP
57+
Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
5558
"""
5659
__license__ = ("Apache License, version 2.0 "
5760
"<https://www.apache.org/licenses/LICENSE-2.0>")

rocrate/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
66
# Copyright 2024 Data Centre, SciLifeLab, SE
77
# Copyright 2024 National Institute of Informatics (NII), JP
8+
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
89
#
910
# Licensed under the Apache License, Version 2.0 (the "License");
1011
# you may not use this file except in compliance with the License.

rocrate/memory_buffer.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2019-2024 The University of Manchester, UK
2+
# Copyright 2020-2024 Vlaams Instituut voor Biotechnologie (VIB), BE
3+
# Copyright 2020-2024 Barcelona Supercomputing Center (BSC), ES
4+
# Copyright 2020-2024 Center for Advanced Studies, Research and Development in Sardinia (CRS4), IT
5+
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
6+
# Copyright 2024 Data Centre, SciLifeLab, SE
7+
# Copyright 2024 National Institute of Informatics (NII), JP
8+
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
9+
#
10+
# Licensed under the Apache License, Version 2.0 (the "License");
11+
# you may not use this file except in compliance with the License.
12+
# You may obtain a copy of the License at
13+
#
14+
# http://www.apache.org/licenses/LICENSE-2.0
15+
#
16+
# Unless required by applicable law or agreed to in writing, software
17+
# distributed under the License is distributed on an "AS IS" BASIS,
18+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
# See the License for the specific language governing permissions and
20+
# limitations under the License.
21+
22+
from io import RawIOBase
23+
24+
25+
class MemoryBuffer(RawIOBase):
26+
"""
27+
A buffer class that supports reading and writing binary data.
28+
The buffer automatically resets upon reading to make sure all data is read only once.
29+
"""
30+
31+
def __init__(self):
32+
self._buffer = b''
33+
34+
def write(self, data):
35+
if self.closed:
36+
raise ValueError('write to closed file')
37+
self._buffer += data
38+
return len(data)
39+
40+
def read(self, size=-1):
41+
if self.closed:
42+
raise ValueError('read from closed file')
43+
if size < 0:
44+
data = self._buffer
45+
self._buffer = b''
46+
else:
47+
data = self._buffer[:size]
48+
self._buffer = self._buffer[size:]
49+
return data
50+
51+
def __len__(self):
52+
return len(self._buffer)

rocrate/metadata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
66
# Copyright 2024 Data Centre, SciLifeLab, SE
77
# Copyright 2024 National Institute of Informatics (NII), JP
8+
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
89
#
910
# Licensed under the Apache License, Version 2.0 (the "License");
1011
# you may not use this file except in compliance with the License.

rocrate/model/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright 2022-2024 École Polytechnique Fédérale de Lausanne, CH
66
# Copyright 2024 Data Centre, SciLifeLab, SE
77
# Copyright 2024 National Institute of Informatics (NII), JP
8+
# Copyright 2025 Senckenberg Society for Nature Research (SGN), DE
89
#
910
# Licensed under the Apache License, Version 2.0 (the "License");
1011
# you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)