Skip to content

Commit

Permalink
Feat/delete (#31)
Browse files Browse the repository at this point in the history
* Support deleting files of local filesystem

* Support deleting files from Azure

* Support deleting from Filesystem class
  • Loading branch information
4c0n authored Oct 24, 2024
1 parent 0dc5f44 commit 0ab71d6
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
14 changes: 14 additions & 0 deletions plugfs/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ async def read(self) -> bytes:
async def get_iterator(self) -> AsyncIterator[bytes]:
return await self._adapter.get_iterator(self._path)

async def delete(self) -> None:
await self._adapter.delete(self._path)


@final
class AzureStorageBlobsAdapter(Adapter):
Expand Down Expand Up @@ -112,6 +115,17 @@ async def makedirs(self, path: str) -> None:
"""Azure storage does not really have directories, so we don't need to do anything here.
The path will just be part of the blob name."""

async def delete(self, path: str) -> None:
blob_client = self._client.get_blob_client(path)

async with blob_client:
try:
await blob_client.delete_blob()
except ResourceNotFoundError as error:
raise NotFoundException(
f"Failed to delete file '{path}', file does not exist!"
) from error

async def _write(self, path: str, data: bytes | AsyncIterator[bytes]) -> AzureFile:
blob_client = self._client.get_blob_client(path)

Expand Down
11 changes: 10 additions & 1 deletion plugfs/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ async def read(self) -> bytes: ...
@abstractmethod
async def get_iterator(self) -> AsyncIterator[bytes]: ...

@abstractmethod
async def delete(self) -> None: ...


class NotFoundException(Exception): ...

Expand Down Expand Up @@ -59,6 +62,9 @@ async def write_iterator(
@abstractmethod
async def makedirs(self, path: str) -> None: ...

@abstractmethod
async def delete(self, path: str) -> None: ...


@final
class Filesystem:
Expand All @@ -80,4 +86,7 @@ async def write_iterator(self, path: str, iterator: AsyncIterator[bytes]) -> Fil
return await self._adapter.write_iterator(path, iterator)

async def makedirs(self, path: str) -> None:
return await self._adapter.makedirs(path)
await self._adapter.makedirs(path)

async def delete(self, path: str) -> None:
await self._adapter.delete(path)
13 changes: 12 additions & 1 deletion plugfs/local.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import AsyncIterator, final

import aiofiles
from aiofiles.os import listdir, makedirs
from aiofiles.os import listdir, makedirs, remove
from aiofiles.ospath import exists, getsize, isdir, isfile

from plugfs.filesystem import (
Expand Down Expand Up @@ -35,6 +35,9 @@ async def get_iterator(self) -> AsyncIterator[bytes]:
async def write(self, data: bytes) -> None:
await self._adapter.write(self._path, data)

async def delete(self) -> None:
await self._adapter.delete(self._path)


@final
class LocalAdapter(Adapter):
Expand Down Expand Up @@ -109,3 +112,11 @@ async def write_iterator(

async def makedirs(self, path: str) -> None:
await makedirs(path)

async def delete(self, path: str) -> None:
try:
await remove(path)
except FileNotFoundError as error:
raise NotFoundException(
f"Failed to delete file '{path}', file does not exist!"
) from error
26 changes: 26 additions & 0 deletions tests/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,29 @@ async def test_write_overwrite_iterator_existing(
async def _iterator(self) -> AsyncIterator[bytes]:
for chunk in [b"Hello ", b"world", b"!"]:
yield chunk

@pytest.mark.anyio
async def test_delete_non_existing(
self, azure_storage_blobs_adapter: AzureStorageBlobsAdapter
) -> None:
file_path = "/this/path/does/not/exist"

with pytest.raises(NotFoundException) as exception_info:
await azure_storage_blobs_adapter.delete(file_path)

assert (
str(exception_info.value)
== f"Failed to delete file '{file_path}', file does not exist!"
)

@pytest.mark.anyio
async def test_delete(
self, azure_storage_blobs_adapter: AzureStorageBlobsAdapter
) -> None:
file_path = "/1mb.bin"
await azure_storage_blobs_adapter.delete(file_path)

with pytest.raises(NotFoundException) as exception_info:
await azure_storage_blobs_adapter.get_file(file_path)

assert str(exception_info.value) == f"Failed to find file '{file_path}'!"
25 changes: 25 additions & 0 deletions tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,28 @@ async def test_makedirs(self) -> None:
assert path.exists(dir_path)

os.rmdir(dir_path)

@pytest.mark.anyio
async def test_delete_non_existing_file(self) -> None:
adapter = LocalAdapter()
file_path = "/this/path/does/not/exist"

with pytest.raises(NotFoundException) as exception_info:
await adapter.delete(file_path)

assert (
str(exception_info.value)
== f"Failed to delete file '{file_path}', file does not exist!"
)

@pytest.mark.anyio
async def test_delete(self) -> None:
adapter = LocalAdapter()
file_path = path.join("/tmp", str(uuid4()))

with open(file_path, "wb") as file:
file.write(b"Hello world!")

await adapter.delete(file_path)

assert not path.exists(file_path)

0 comments on commit 0ab71d6

Please sign in to comment.