From 62481f13295ad6b96648e608d4d93029864c8eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 21 Aug 2022 22:54:52 +0200 Subject: roezio: migrate poezio/xdg.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/xdg.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/xdg.rs (limited to 'src/xdg.rs') diff --git a/src/xdg.rs b/src/xdg.rs new file mode 100644 index 00000000..d5c9d41d --- /dev/null +++ b/src/xdg.rs @@ -0,0 +1,69 @@ +// Copyright (C) 2022 Maxime “pep” Buquet +// +// This program is free software: you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the +// Free Software Foundation, either version 3 of the License, or (at your +// option) any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use std::cell::LazyCell; + +use directories::ProjectDirs; +use pyo3::{ + marker::Python, + prelude::{pyclass, pymethods, PyObject, PyResult}, + IntoPy, +}; + +/// Project qualifier +pub const QUALIFIER: &'static str = "io"; +/// Project organization +pub const ORGANIZATION: &'static str = "poez"; +/// Project appname +pub const APPNAME: &'static str = "Poezio"; + +/// Project directories +pub const PROJECT: LazyCell = LazyCell::new(|| { + ProjectDirs::from(QUALIFIER, ORGANIZATION, APPNAME).expect("HOME dir should be available.") +}); + +#[pyclass(name = "XDG")] +pub struct PyProject(ProjectDirs); + +fn get_path(py: Python<'_>) -> PyResult { + // TODO: Stop importing pathlib all the time + let pathlib = py.import("pathlib")?; + let path = pathlib.getattr("Path")?; + Ok(path.into_py(py)) +} + +impl PyProject { + pub fn new(dirs: ProjectDirs) -> Self { + PyProject(dirs) + } +} + +#[pymethods] +impl PyProject { + #[getter] + pub fn cache_dir(&self, py: Python<'_>) -> PyResult { + Ok(get_path(py)?.call1(py, (self.0.cache_dir(),))?) + } + + #[getter] + pub fn config_dir(&self, py: Python<'_>) -> PyResult { + Ok(get_path(py)?.call1(py, (self.0.config_dir(),))?) + } + + #[getter] + pub fn data_dir(&self, py: Python<'_>) -> PyResult { + Ok(get_path(py)?.call1(py, (self.0.data_dir(),))?) + } +} -- cgit v1.2.3