Skip to content

Commit

Permalink
asynlocal: make open_async compatible with fsspec (#25)
Browse files Browse the repository at this point in the history
* asynlocal: make open_async compatible with fsspec

Closes #24.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
skshetry and pre-commit-ci[bot] authored Jan 13, 2024
1 parent ef8f5ce commit 49b7d08
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
9 changes: 3 additions & 6 deletions src/morefs/asyn_local.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import shutil
import sys
from asyncio import get_running_loop, iscoroutinefunction
from contextlib import asynccontextmanager
from functools import partial, wraps
from typing import Awaitable, Callable, TypeVar

Expand Down Expand Up @@ -83,18 +82,16 @@ async def _get_file(self, src, dst, **kwargs): # pylint: disable=arguments-rena
src = self._strip_protocol(src)
return await self._get_file_async(src, dst)

async with self.open_async(src, "rb") as fsrc:
fsrc = await self.open_async(src, "rb")
async with fsrc:
while True:
buf = await fsrc.read(length=shutil.COPY_BUFSIZE)
if not buf:
break
await dst.write(buf)

@asynccontextmanager
async def open_async(self, path, mode="rb", **kwargs):
path = self._strip_protocol(path)
if self.auto_mkdir and "w" in mode:
await self._makedirs(self._parent(path), exist_ok=True)

async with aiofile.async_open(path, mode, **kwargs) as f:
yield f
return await aiofile.async_open(path, mode, **kwargs)
2 changes: 1 addition & 1 deletion src/morefs/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import fsspec


class OverlayFileSystem(fsspec.AbstractFileSystem):
class OverlayFileSystem(fsspec.AbstractFileSystem): # pylint: disable=abstract-method
cachable = False

def __init__(self, *fses: fsspec.AbstractFileSystem, **kwargs):
Expand Down
15 changes: 10 additions & 5 deletions tests/test_asyn_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ def test_sync_methods(tmp_path, localfs, fs):

@pytest.mark.asyncio
async def test_open_async(tmp_path, fs):
async with fs.open_async(tmp_path / "file", mode="wb") as f:
f = await fs.open_async(tmp_path / "file", mode="wb")
async with f:
pass
assert await fs._exists(tmp_path / "file")

async with fs.open_async(tmp_path / "file", mode="wb") as f:
f = await fs.open_async(tmp_path / "file", mode="wb")
async with f:
assert await f.write(b"contents")

async with fs.open_async(tmp_path / "file") as f:
f = await fs.open_async(tmp_path / "file")
async with f:
assert await f.read() == b"contents"


Expand All @@ -96,7 +99,8 @@ async def test_get_file(tmp_path, fs):

assert await fs._isfile(tmp_path / "bar")

async with fs.open_async(tmp_path / "file1", mode="wb") as f:
f = await fs.open_async(tmp_path / "file1", mode="wb")
async with f:
await fs._get_file(tmp_path / "foo", f)
assert await fs._cat_file(tmp_path / "file1") == b"foo"

Expand All @@ -112,7 +116,8 @@ async def test_get_file(tmp_path, fs):
@pytest.mark.asyncio
async def test_auto_mkdir_on_open_async(tmp_path):
fs = AsyncLocalFileSystem(auto_mkdir=True)
async with fs.open_async(tmp_path / "dir" / "file", mode="wb") as f:
f = await fs.open_async(tmp_path / "dir" / "file", mode="wb")
async with f:
await f.write(b"contents")

assert await fs._isdir(tmp_path / "dir")
Expand Down

0 comments on commit 49b7d08

Please sign in to comment.