Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-python/src/io/sink_options.rs
7889 views
1
use std::sync::Arc;
2
3
use polars::prelude::sync_on_close::SyncOnCloseType;
4
use polars::prelude::{CloudScheme, UnifiedSinkArgs};
5
use pyo3::types::PyAnyMethods;
6
use pyo3::{Bound, FromPyObject, Py, PyAny, PyResult};
7
8
use crate::functions::parse_cloud_options;
9
use crate::prelude::Wrap;
10
11
/// Interface to `class SinkOptions` on the Python side
12
pub struct PySinkOptions<'py>(Bound<'py, pyo3::PyAny>);
13
14
impl<'py> FromPyObject<'py> for PySinkOptions<'py> {
15
fn extract_bound(ob: &Bound<'py, pyo3::PyAny>) -> pyo3::PyResult<Self> {
16
Ok(Self(ob.clone()))
17
}
18
}
19
20
impl PySinkOptions<'_> {
21
pub fn extract_unified_sink_args(
22
&self,
23
cloud_scheme: Option<CloudScheme>,
24
) -> PyResult<UnifiedSinkArgs> {
25
#[derive(FromPyObject)]
26
struct Extract {
27
mkdir: bool,
28
maintain_order: bool,
29
sync_on_close: Option<Wrap<SyncOnCloseType>>,
30
storage_options: Option<Vec<(String, String)>>,
31
credential_provider: Option<Py<PyAny>>,
32
retries: usize,
33
}
34
35
let Extract {
36
mkdir,
37
maintain_order,
38
sync_on_close,
39
storage_options,
40
credential_provider,
41
retries,
42
} = self.0.extract()?;
43
44
let cloud_options =
45
parse_cloud_options(cloud_scheme, storage_options, credential_provider, retries)?;
46
47
let sync_on_close = sync_on_close.map_or(SyncOnCloseType::default(), |x| x.0);
48
49
let unified_sink_args = UnifiedSinkArgs {
50
mkdir,
51
maintain_order,
52
sync_on_close,
53
cloud_options: cloud_options.map(Arc::new),
54
};
55
56
Ok(unified_sink_args)
57
}
58
}
59
60