diff --git a/bindings/python/src/operator.rs b/bindings/python/src/operator.rs index 90015d41bcd7..51c3c56f806a 100644 --- a/bindings/python/src/operator.rs +++ b/bindings/python/src/operator.rs @@ -33,6 +33,14 @@ fn build_operator(scheme: &str, map: HashMap) -> PyResult, +) -> PyResult { + let op = ocore::Operator::from_uri((uri, options)).map_err(format_pyerr)?; + Ok(op) +} + fn build_blocking_operator( scheme: &str, map: HashMap, @@ -45,6 +53,18 @@ fn build_blocking_operator( Ok(op) } +fn build_blocking_operator_from_uri( + uri: &str, + options: &HashMap, +) -> PyResult { + let op = ocore::Operator::from_uri((uri, options)).map_err(format_pyerr)?; + + 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('_', "-") } @@ -112,6 +132,37 @@ impl Operator { }) } + /// Create a new blocking `Operator` from a URI. + /// + /// Parameters + /// ---------- + /// uri : str + /// The URI of the service. + /// **kwargs : dict + /// The options for the service. + /// + /// Returns + /// ------- + /// Operator + /// The new operator. + #[staticmethod] + #[pyo3(signature = (uri, *, **kwargs))] + pub fn from_uri(uri: String, kwargs: Option<&Bound>) -> PyResult { + let __map = kwargs + .map(|v| { + v.extract::>() + .expect("must be a valid hashmap") + }) + .unwrap_or_default(); + let core = build_blocking_operator_from_uri(&uri, &__map)?; + let __scheme = core.info().scheme().to_string(); + Ok(Operator { + core, + __scheme, + __map, + }) + } + /// Add a new layer to this operator. /// /// Parameters @@ -758,6 +809,37 @@ impl AsyncOperator { }) } + /// Create a new `AsyncOperator` from a URI. + /// + /// Parameters + /// ---------- + /// uri : str + /// The URI of the service. + /// **kwargs : dict + /// The options for the service. + /// + /// Returns + /// ------- + /// AsyncOperator + /// The new operator. + #[staticmethod] + #[pyo3(signature = (uri, *, **kwargs))] + pub fn from_uri(uri: String, kwargs: Option<&Bound>) -> PyResult { + let __map = kwargs + .map(|v| { + v.extract::>() + .expect("must be a valid hashmap") + }) + .unwrap_or_default(); + let core = build_operator_from_uri(&uri, &__map)?; + let __scheme = core.info().scheme().to_string(); + Ok(AsyncOperator { + core, + __scheme, + __map, + }) + } + /// Add a new layer to the operator. /// /// Parameters