Skip to content

Commit

Permalink
feat(bindings/python): Support exists API for python binding
Browse files Browse the repository at this point in the history
Co-authored-by: Zheao Li <me@manjusaka.me>
Signed-off-by: Manjusaka <me@manjusaka.me>
Signed-off-by: Yashika soni <soniyashika164@gmail.com>
  • Loading branch information
Yashika-code and Zheaoli committed Dec 24, 2024
1 parent 260fcd4 commit c6e9a9f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bindings/python/python/opendal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Operator:
def copy(self, source: str, target: str) -> None: ...
def rename(self, source: str, target: str) -> None: ...
def remove_all(self, path: str) -> None: ...
def exists(self, path: str) -> bool: ...
def to_async_operator(self) -> AsyncOperator: ...

@final
Expand Down Expand Up @@ -81,6 +82,7 @@ class AsyncOperator:
async def copy(self, source: str, target: str) -> None: ...
async def rename(self, source: str, target: str) -> None: ...
async def remove_all(self, path: str) -> None: ...
async def exists(self, path: str) -> bool: ...
def to_operator(self) -> Operator: ...

@final
Expand Down
7 changes: 7 additions & 0 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ impl AsyncOperator {
})
}

pub fn exists<'p>(&'p self, py: Python<'p>, path: String) -> PyResult<Bound<PyAny>> {
let this = self.core.clone();
future_into_py(
py,
async move { this.exists(&path).await.map_err(format_pyerr) },
)
}
/// Create a dir at given path.
///
/// # Notes
Expand Down
29 changes: 29 additions & 0 deletions bindings/python/tests/test_exists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
from random import randint
from uuid import uuid4

import pytest


@pytest.mark.need_capability("write", "delete", "stat")
def test_sync_exists(service_name, operator, async_operator):
size = randint(1, 1024)
filename = f"test_file_{str(uuid4())}.txt"
content = os.urandom(size)
size = len(content)
operator.write(filename, content)
assert operator.exists(filename)
assert not operator.exists(filename + "not_exists")


@pytest.mark.asyncio
@pytest.mark.need_capability("write", "delete", "stat")
async def test_async_exists(service_name, operator, async_operator):
size = randint(1, 1024)
filename = f"test_file_{str(uuid4())}.txt"
content = os.urandom(size)
size = len(content)
await async_operator.write(filename, content)
assert await async_operator.exists(filename)
assert not await async_operator.exists(filename + "not_exists")
await async_operator.delete(filename)

0 comments on commit c6e9a9f

Please sign in to comment.