Skip to content

Commit

Permalink
implement _get_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
d70-t committed Sep 23, 2024
1 parent bc376ae commit 006df24
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ipfsspec/async_ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from fsspec.asyn import AsyncFileSystem, sync, sync_wrapper
from fsspec.exceptions import FSTimeoutError
from fsspec.callbacks import DEFAULT_CALLBACK
from fsspec.utils import isfilelike

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

async def _get_file(
self, rpath, lpath, chunk_size=5 * 2**20, callback=DEFAULT_CALLBACK, **kwargs
):
# TODO: implement chunked retrieval
logger.debug(rpath)

if isfilelike(lpath):
outfile = lpath
else:
outfile = open(lpath, "wb") # noqa: ASYNC101, ASYNC230

try:
content = await self._cat_file(rpath)
outfile.write(content)
callback.set_size(len(content))
callback.relative_update(len(content))
finally:
if not isfilelike(lpath):
outfile.close()

async def _info(self, path, **kwargs):
path = self._strip_protocol(path)
session = await self.set_session()
Expand Down
6 changes: 6 additions & 0 deletions test/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ async def test_cat_file(fs):
assert res == REF_CONTENT[3:7]


@pytest.mark.asyncio
async def test_get_file(fs, tmp_path):
await fs._get_file(TEST_ROOT + "/default", tmp_path / "default")
assert open(tmp_path / "default", "rb").read() == REF_CONTENT


@pytest.mark.asyncio
async def test_exists(fs):
res = await fs._exists(TEST_ROOT + "/default")
Expand Down

0 comments on commit 006df24

Please sign in to comment.