Skip to content
82 changes: 82 additions & 0 deletions bindings/python/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ fn build_operator(scheme: &str, map: HashMap<String, String>) -> PyResult<ocore:
Ok(op)
}

fn build_operator_from_uri(
uri: &str,
options: &HashMap<String, String>,
) -> PyResult<ocore::Operator> {
let op = ocore::Operator::from_uri((uri, options)).map_err(format_pyerr)?;
Ok(op)
}

fn build_blocking_operator(
scheme: &str,
map: HashMap<String, String>,
Expand All @@ -45,6 +53,18 @@ fn build_blocking_operator(
Ok(op)
}

fn build_blocking_operator_from_uri(
uri: &str,
options: &HashMap<String, String>,
) -> PyResult<ocore::blocking::Operator> {
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('_', "-")
}
Expand Down Expand Up @@ -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<PyDict>>) -> PyResult<Self> {
let __map = kwargs
.map(|v| {
v.extract::<HashMap<String, String>>()
.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
Expand Down Expand Up @@ -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<PyDict>>) -> PyResult<Self> {
let __map = kwargs
.map(|v| {
v.extract::<HashMap<String, String>>()
.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
Expand Down
Loading