Skip to content

Commit 006df24

Browse files
committed
implement _get_file()
1 parent bc376ae commit 006df24

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

ipfsspec/async_ipfs.py

+22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from fsspec.asyn import AsyncFileSystem, sync, sync_wrapper
1414
from fsspec.exceptions import FSTimeoutError
15+
from fsspec.callbacks import DEFAULT_CALLBACK
16+
from fsspec.utils import isfilelike
1517

1618
from multiformats import CID, multicodec
1719
from . import unixfsv1
@@ -293,6 +295,26 @@ async def _cat_file(self, path, start=None, end=None, **kwargs):
293295
session = await self.set_session()
294296
return (await self.gateway.cat(path, session))[start:end]
295297

298+
async def _get_file(
299+
self, rpath, lpath, chunk_size=5 * 2**20, callback=DEFAULT_CALLBACK, **kwargs
300+
):
301+
# TODO: implement chunked retrieval
302+
logger.debug(rpath)
303+
304+
if isfilelike(lpath):
305+
outfile = lpath
306+
else:
307+
outfile = open(lpath, "wb") # noqa: ASYNC101, ASYNC230
308+
309+
try:
310+
content = await self._cat_file(rpath)
311+
outfile.write(content)
312+
callback.set_size(len(content))
313+
callback.relative_update(len(content))
314+
finally:
315+
if not isfilelike(lpath):
316+
outfile.close()
317+
296318
async def _info(self, path, **kwargs):
297319
path = self._strip_protocol(path)
298320
session = await self.set_session()

test/test_async.py

+6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ async def test_cat_file(fs):
8080
assert res == REF_CONTENT[3:7]
8181

8282

83+
@pytest.mark.asyncio
84+
async def test_get_file(fs, tmp_path):
85+
await fs._get_file(TEST_ROOT + "/default", tmp_path / "default")
86+
assert open(tmp_path / "default", "rb").read() == REF_CONTENT
87+
88+
8389
@pytest.mark.asyncio
8490
async def test_exists(fs):
8591
res = await fs._exists(TEST_ROOT + "/default")

0 commit comments

Comments
 (0)