datafusion_python/
config.rs1use pyo3::prelude::*;
19use pyo3::types::*;
20
21use datafusion::config::ConfigOptions;
22
23use crate::errors::PyDataFusionResult;
24use crate::utils::py_obj_to_scalar_value;
25
26#[pyclass(name = "Config", module = "datafusion", subclass)]
27#[derive(Clone)]
28pub(crate) struct PyConfig {
29 config: ConfigOptions,
30}
31
32#[pymethods]
33impl PyConfig {
34 #[new]
35 fn py_new() -> Self {
36 Self {
37 config: ConfigOptions::new(),
38 }
39 }
40
41 #[staticmethod]
43 pub fn from_env() -> PyDataFusionResult<Self> {
44 Ok(Self {
45 config: ConfigOptions::from_env()?,
46 })
47 }
48
49 pub fn get<'py>(&mut self, key: &str, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
51 let options = self.config.to_owned();
52 for entry in options.entries() {
53 if entry.key == key {
54 return Ok(entry.value.into_pyobject(py)?);
55 }
56 }
57 Ok(None::<String>.into_pyobject(py)?)
58 }
59
60 pub fn set(&mut self, key: &str, value: PyObject, py: Python) -> PyDataFusionResult<()> {
62 let scalar_value = py_obj_to_scalar_value(py, value)?;
63 self.config.set(key, scalar_value.to_string().as_str())?;
64 Ok(())
65 }
66
67 pub fn get_all(&mut self, py: Python) -> PyResult<PyObject> {
69 let dict = PyDict::new(py);
70 let options = self.config.to_owned();
71 for entry in options.entries() {
72 dict.set_item(entry.key, entry.value.clone().into_pyobject(py)?)?;
73 }
74 Ok(dict.into())
75 }
76
77 fn __repr__(&mut self, py: Python) -> PyResult<String> {
78 let dict = self.get_all(py);
79 match dict {
80 Ok(result) => Ok(format!("Config({result})")),
81 Err(err) => Ok(format!("Error: {:?}", err.to_string())),
82 }
83 }
84}