godata/
io.rs

1use pyo3::prelude::*;
2use pyo3::exceptions::PyTypeError;
3use std::path::PathBuf;
4use std::fs;
5use crate::mdb::get_dirs;
6
7pub(crate) fn store(object: &PyAny, output_function:  &PyAny, path: &str) -> PyResult<()> {
8    // Accepts a  function that stores an object to a file that is passed to it.
9    if !output_function.is_callable() {
10        return Err(PyErr::new::<PyTypeError, _>("output_function must be callable"));
11    }
12    Python::with_gil(|_| -> PyResult<()> {
13        output_function.call((object, path), None)?;
14        Ok(())
15    })?;
16    Ok(())
17}
18
19pub(crate) fn remove_if_internal(path: &PathBuf) {
20    // Remove a file or folder if it is stored in the internal storage.
21    let dirs = get_dirs();
22    let data_dir = dirs.get("data_dir").unwrap();
23    if path.starts_with(data_dir) && path.exists() {
24        if path.is_file() {
25            fs::remove_file(&path).unwrap();
26            if path.parent().unwrap().read_dir().unwrap().count() == 0 {
27                fs::remove_dir_all(path.parent().unwrap()).unwrap();
28            }
29        }
30        else {
31            fs::remove_dir_all(path).unwrap();
32        }
33    }
34
35}