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

feat(bindings/python): Support exists API for python binding #5450

Closed
wants to merge 2 commits into from
Closed
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
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
11 changes: 11 additions & 0 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ impl Operator {
self.core.rename(source, target).map_err(format_pyerr)
}

pub fn exists(&self, path: &str) -> PyResult<bool> {
self.core.exists(path).map_err(format_pyerr)
}

/// Remove all file
pub fn remove_all(&self, path: &str) -> PyResult<()> {
self.core.remove_all(path).map_err(format_pyerr)
Expand Down Expand Up @@ -398,6 +402,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)
Loading