chitralverma commented on code in PR #7869:
URL: https://github.com/apache/opendal/pull/7869#discussion_r3533882348
##########
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:
simplified this.
--
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]