This is an automated email from the ASF dual-hosted git repository.

messense pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new e0ccc9ef2 feat(binding/python): Add new API to convert between 
AsyncOperator and Operator (#3514)
e0ccc9ef2 is described below

commit e0ccc9ef28a49492f2b116b24fd0f81e5252ddbd
Author: Nadeshiko Manju <[email protected]>
AuthorDate: Thu Nov 9 09:44:27 2023 +0800

    feat(binding/python): Add new API to convert between AsyncOperator and 
Operator (#3514)
    
    feat(binding/python): Add new API to convert AsyncOperator to Operator
    
    Signed-off-by: Manjusaka <[email protected]>
---
 bindings/python/python/opendal/__init__.pyi |  2 ++
 bindings/python/src/operator.rs             | 15 +++++++++++----
 bindings/python/tests/conftest.py           | 18 ++++++++----------
 3 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/bindings/python/python/opendal/__init__.pyi 
b/bindings/python/python/opendal/__init__.pyi
index 2a28f523f..03f702337 100644
--- a/bindings/python/python/opendal/__init__.pyi
+++ b/bindings/python/python/opendal/__init__.pyi
@@ -43,6 +43,7 @@ class Operator:
     def copy(self, source: str, target: str): ...
     def rename(self, source: str, target: str): ...
     def remove_all(self, path: str): ...
+    def to_async_operator(self) -> AsyncOperator: ...
 
 class AsyncOperator:
     def __init__(self, scheme: str, **kwargs): ...
@@ -73,6 +74,7 @@ class AsyncOperator:
     async def copy(self, source: str, target: str): ...
     async def rename(self, source: str, target: str): ...
     async def remove_all(self, path: str): ...
+    def to_operator(self) -> Operator: ...
 
 class File:
     def read(self, size: Optional[int] = None) -> memoryview: ...
diff --git a/bindings/python/src/operator.rs b/bindings/python/src/operator.rs
index f6e8c3f86..19908bffe 100644
--- a/bindings/python/src/operator.rs
+++ b/bindings/python/src/operator.rs
@@ -29,10 +29,9 @@ use crate::*;
 fn build_operator(
     scheme: ocore::Scheme,
     map: HashMap<String, String>,
-    blocking: bool,
 ) -> PyResult<ocore::Operator> {
     let mut op = ocore::Operator::via_map(scheme, map).map_err(format_pyerr)?;
-    if blocking && !op.info().full_capability().blocking {
+    if !op.info().full_capability().blocking {
         let runtime = pyo3_asyncio::tokio::get_runtime();
         let _guard = runtime.enter();
         op = op
@@ -66,7 +65,7 @@ impl Operator {
             })
             .unwrap_or_default();
 
-        Ok(Operator(build_operator(scheme, map, true)?.blocking()))
+        Ok(Operator(build_operator(scheme, map)?.blocking()))
     }
 
     /// Add new layers upon existing operator
@@ -185,6 +184,10 @@ impl Operator {
         Ok(capability::Capability::new(self.0.info().full_capability()))
     }
 
+    pub fn to_async_operator(&self) -> PyResult<AsyncOperator> {
+        Ok(AsyncOperator(self.0.clone().into()))
+    }
+
     fn __repr__(&self) -> String {
         let info = self.0.info();
         let name = info.name();
@@ -224,7 +227,7 @@ impl AsyncOperator {
             })
             .unwrap_or_default();
 
-        Ok(AsyncOperator(build_operator(scheme, map, false)?))
+        Ok(AsyncOperator(build_operator(scheme, map)?))
     }
 
     /// Add new layers upon existing operator
@@ -456,6 +459,10 @@ impl AsyncOperator {
         Ok(capability::Capability::new(self.0.info().full_capability()))
     }
 
+    pub fn to_operator(&self) -> PyResult<Operator> {
+        Ok(Operator(self.0.clone().blocking()))
+    }
+
     fn __repr__(&self) -> String {
         let info = self.0.info();
         let name = info.name();
diff --git a/bindings/python/tests/conftest.py 
b/bindings/python/tests/conftest.py
index 970bb3ba5..3385ee3f7 100644
--- a/bindings/python/tests/conftest.py
+++ b/bindings/python/tests/conftest.py
@@ -34,7 +34,7 @@ def pytest_configure(config):
     )
 
 
[email protected]()
[email protected](scope="session")
 def service_name():
     service_name = os.environ.get("OPENDAL_TEST")
     if service_name is None:
@@ -42,7 +42,7 @@ def service_name():
     return service_name
 
 
[email protected]()
[email protected](scope="session")
 def setup_config(service_name):
     # Read arguments from envs.
     prefix = f"opendal_{service_name}_"
@@ -54,20 +54,18 @@ def setup_config(service_name):
     return config
 
 
[email protected]()
-def operator(service_name, setup_config):
-    return opendal.Operator(service_name, **setup_config).layer(
-        opendal.layers.RetryLayer()
-    )
-
-
[email protected]()
[email protected](scope="session")
 def async_operator(service_name, setup_config):
     return opendal.AsyncOperator(service_name, **setup_config).layer(
         opendal.layers.RetryLayer()
     )
 
 
[email protected](scope="session")
+def operator(async_operator):
+    return async_operator.to_operator()
+
+
 @pytest.fixture(autouse=True)
 def check_capability(request, operator, async_operator):
     if request.node.get_closest_marker("need_capability"):

Reply via email to