erickguan commented on code in PR #7869:
URL: https://github.com/apache/opendal/pull/7869#discussion_r3531753631
##########
bindings/python/python/opendal/operator.pyi:
##########
@@ -161,6 +161,35 @@ class AsyncOperator:
coroutine
An awaitable that returns True if the path exists, False otherwise.
"""
+ @classmethod
+ def from_uri(cls, /, uri: str, **kwargs) -> AsyncOperator:
+ """
+ Create a new `AsyncOperator` from a URI string.
+
+ The URI encodes the scheme and configuration in a single string, e.g.
+ ``memory://`` or ``s3://bucket/path?region=us-east-1``. The scheme must
+ belong to a service enabled in this build.
+
+ Parameters
+ ----------
+ uri : str
+ The URI of the service.
+ **kwargs : dict
+ Extra options that override or supplement values encoded in the
URI.
+
+ Returns
+ -------
+ AsyncOperator
+ The new async operator.
+
+ Examples
+ --------
+ >>> import opendal
+ >>> op = opendal.AsyncOperator.from_uri("memory://")
+ >>> op = opendal.AsyncOperator.from_uri(
+ ... "s3://bucket/path", region="us-east-1"
Review Comment:
This looks confusing.
For URI, users could perform
from urllib.parse import urlencode
##########
bindings/python/python/opendal/operator.pyi:
##########
@@ -746,6 +775,35 @@ class Operator:
bool
True if the path exists, False otherwise.
"""
+ @classmethod
+ def from_uri(cls, /, uri: str, **kwargs) -> Operator:
Review Comment:
I feel kwargs are a bit useless here.
##########
bindings/python/src/operator.rs:
##########
@@ -45,10 +46,91 @@ fn build_blocking_operator(
Ok(op)
}
+fn build_operator_from_uri(uri: &str, map: HashMap<String, String>) ->
PyResult<ocore::Operator> {
+ let op = ocore::Operator::from_uri((uri, map)).map_err(format_pyerr)?;
+ Ok(op)
+}
+
+fn build_blocking_operator_from_uri(
+ uri: &str,
+ map: HashMap<String, String>,
+) -> PyResult<ocore::blocking::Operator> {
+ let op = build_operator_from_uri(uri, map)?;
+
+ let runtime = pyo3_async_runtimes::tokio::get_runtime();
+ let _guard = runtime.enter();
+ let op = ocore::blocking::Operator::new(op).map_err(format_pyerr)?;
+ Ok(op)
+}
+
fn normalize_scheme(raw: &str) -> String {
raw.trim().to_ascii_lowercase().replace('_', "-")
}
+/// Extract service options from `**kwargs`, propagating a Python exception if
a
+/// value is not a `str -> str` mapping instead of panicking.
+fn extract_kwargs(kwargs: Option<&Bound<PyDict>>) -> PyResult<HashMap<String,
String>> {
+ match kwargs {
+ Some(v) => v.extract::<HashMap<String, String>>(),
+ None => Ok(HashMap::new()),
+ }
+}
+
+/// Rebuild a blocking [`Operator`] while unpickling.
+///
+/// `from_uri` operators store a full URI in `__scheme` and must be rebuilt via
+/// `from_uri`; scheme operators are rebuilt via `via_iter`. Routing both
through
+/// this reconstructor keeps a single pickle entry point and avoids the scheme
+/// normalization in `__new__` that would corrupt a stored URI.
+#[pyfunction]
+pub fn _reconstruct_operator(
+ from_uri: bool,
+ scheme: &str,
+ map: HashMap<String, String>,
+) -> PyResult<Operator> {
+ if from_uri {
+ Ok(Operator {
+ core: build_blocking_operator_from_uri(scheme, map.clone())?,
+ __scheme: scheme.to_string(),
+ __map: map,
+ __from_uri: true,
+ })
+ } else {
+ Ok(Operator {
+ core: build_blocking_operator(scheme, map.clone())?,
+ __scheme: scheme.to_string(),
+ __map: map,
+ __from_uri: false,
+ })
+ }
+}
+
+/// Rebuild an [`AsyncOperator`] while unpickling.
+///
+/// See [`_reconstruct_operator`] for why a dedicated reconstructor is used.
+#[pyfunction]
+pub fn _reconstruct_async_operator(
+ from_uri: bool,
+ scheme: &str,
+ map: HashMap<String, String>,
+) -> PyResult<AsyncOperator> {
+ if from_uri {
+ Ok(AsyncOperator {
+ core: build_operator_from_uri(scheme, map.clone())?,
+ __scheme: scheme.to_string(),
+ __map: map,
+ __from_uri: true,
Review Comment:
Since we have OperatorUri, can we extract scheme? Then we don't need a new
field.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]