use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::wrap_pyfunction;
use std::path::PathBuf;
#[pyfunction]
#[pyo3(signature=(**kwds))]
fn update_datafiles(kwds: Option<&Bound<'_, PyDict>>) -> PyResult<()> {
let overwrite_files = match kwds {
None => false,
Some(u) => match u.get_item("overwrite")? {
Some(v) => v.extract::<bool>()?,
None => false,
},
};
let datadir = match kwds {
None => None,
Some(u) => match u.get_item("dir")? {
Some(v) => Some(PathBuf::from(v.extract::<String>()?)),
None => None,
},
};
match crate::utils::update_datafiles(datadir, overwrite_files) {
Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(e.to_string())),
Ok(_) => Ok(()),
}
}
#[pyfunction]
fn datadir() -> PyResult<PyObject> {
pyo3::Python::with_gil(|py| -> PyResult<PyObject> {
match crate::utils::datadir() {
Ok(v) => Ok(v.to_str().unwrap().to_object(py)),
Err(_) => Ok(pyo3::types::PyNone::get_bound(py).into_py(py)),
}
})
}
#[pyfunction]
fn set_datadir(datadir: String) -> PyResult<()> {
let d = PathBuf::from(datadir);
match crate::utils::set_datadir(&d) {
Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(e.to_string())),
Ok(_) => Ok(()),
}
}
#[pyfunction]
fn datafiles_exist() -> PyResult<bool> {
Ok(crate::utils::data_found())
}
#[pyfunction]
fn githash() -> PyResult<String> {
Ok(String::from(crate::utils::githash()))
}
#[pyfunction]
fn version() -> PyResult<String> {
Ok(String::from(crate::utils::gittag()))
}
#[pyfunction]
fn dylib_path() -> PyResult<PyObject> {
pyo3::Python::with_gil(|py| -> PyResult<PyObject> {
match process_path::get_dylib_path() {
Some(v) => Ok(v.to_str().unwrap().to_object(py)),
None => Ok(pyo3::types::PyNone::get_bound(py).into_py(py)),
}
})
}
#[pyfunction]
fn build_date() -> PyResult<String> {
Ok(String::from(crate::utils::build_date()))
}
#[pymodule]
pub fn utils(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(datadir, m)?).unwrap();
m.add_function(wrap_pyfunction!(set_datadir, m)?).unwrap();
m.add_function(wrap_pyfunction!(datafiles_exist, m)?)
.unwrap();
m.add_function(wrap_pyfunction!(dylib_path, m)?).unwrap();
m.add_function(wrap_pyfunction!(update_datafiles, m)?)
.unwrap();
m.add_function(wrap_pyfunction!(githash, m)?).unwrap();
m.add_function(wrap_pyfunction!(version, m)?).unwrap();
m.add_function(wrap_pyfunction!(build_date, m)?).unwrap();
Ok(())
}