Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement _get_file() #37

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions ipfsspec/async_ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import weakref
from functools import lru_cache
from pathlib import Path
from contextlib import asynccontextmanager
import warnings

import asyncio
Expand All @@ -12,6 +13,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 @@ -118,6 +121,17 @@ async def cat(self, path, session):
self._raise_not_found_for_status(res, path)
return await res.read()

@asynccontextmanager
async def iter_chunked(self, path, session, chunk_size):
res = await self.get(path, session)
async with res:
self._raise_not_found_for_status(res, path)
try:
size = int(res.headers["content-length"])
except (ValueError, KeyError):
size = None
yield size, res.content.iter_chunked(chunk_size)

async def ls(self, path, session, detail=False):
res = await self.get(path, session, headers={"Accept": "application/vnd.ipld.raw"}, params={"format": "raw"})
self._raise_not_found_for_status(res, path)
Expand Down Expand Up @@ -293,6 +307,28 @@ 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
):
logger.debug(rpath)
rpath = self._strip_protocol(rpath)
session = await self.set_session()

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

try:
async with self.gateway.iter_chunked(rpath, session, chunk_size) as (size, chunks):
callback.set_size(size)
async for chunk in chunks:
outfile.write(chunk)
callback.relative_update(len(chunk))
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
Loading