From 10cc67e6c78ca37508ba30cba42baa68c8fff5a3 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:53:40 +0200 Subject: roezio: migrate poezio/args.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/args.rs | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/error.rs | 41 +++++++++++++++++++++++++ src/lib.rs | 13 ++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/args.rs create mode 100644 src/error.rs (limited to 'src') diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 00000000..cb25c876 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,97 @@ +// 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 crate::error::Error; +use crate::xdg::PROJECT; + +use std::cell::LazyCell; +use std::fs; +use std::io::Write; +use std::path::PathBuf; + +use clap::Parser; +use pyo3::{ + marker::Python, + prelude::{pyclass, pymethods, PyObject, PyResult}, +}; + +const VERSION: &'static str = "v0.14.0"; +const CONFIG_FILE: LazyCell = + LazyCell::new(|| PROJECT.config_dir().to_path_buf().join("poezio.cfg")); + +#[pyclass] +#[derive(Parser, Debug)] +#[clap(author, version = VERSION, about, long_about = None)] +pub(crate) struct Args { + /// Check the config file + #[pyo3(get)] + #[clap(short, long, action)] + pub(crate) check_config: bool, + + /// The file where debug will be written + #[clap(short, long, value_name = "DEBUG_FILE")] + pub(crate) debug: Option, + + /// The config file to use + #[clap(short, long = "file", value_name = "CONFIG_FILE", default_value_os_t = CONFIG_FILE.to_path_buf())] + pub(crate) filename: PathBuf, + + /// Custom version passed to Poezio + #[pyo3(get)] + #[clap(long, help = None, default_value_t = VERSION.to_string())] + pub(crate) custom_version: String, +} + +#[pymethods] +impl Args { + #[getter] + fn debug(&self, py: Python<'_>) -> PyResult { + // TODO: Stop importing pathlib all the time + let pathlib = py.import("pathlib")?; + let path: PyObject = pathlib.getattr("Path")?.extract()?; + if let Some(ref debug) = self.debug { + Ok(path.call1(py, (debug.clone(),))?) + } else { + Ok(py.None()) + } + } + + #[getter] + fn filename(&self, py: Python<'_>) -> PyResult { + // TODO: Stop importing pathlib all the time + let pathlib = py.import("pathlib")?; + let path: PyObject = pathlib.getattr("Path")?.extract()?; + Ok(path.call1(py, (self.filename.clone(),))?) + } +} + +/// Parse command line arguments and return whether it's our firstrun alongside Args +pub(crate) fn parse_args(argv: Vec) -> Result<(Args, bool), Error> { + let args = Args::parse_from(argv); + + if args.filename.exists() { + return Ok((args, false)); + }; + + let parent = args + .filename + .parent() + .ok_or(Error::UnableToCreateConfigDir)?; + fs::create_dir_all(parent).map_err(|_| Error::UnableToCreateConfigDir)?; + let default = include_bytes!("../data/default_config.cfg"); + let mut file = fs::File::create(args.filename.clone())?; + file.write_all(default)?; + Ok((args, true)) +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 00000000..c1a0cb88 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,41 @@ +// 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 Affero 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 Affero General Public License +// for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use std::error::Error as StdError; +use std::fmt; +use std::io; + +#[derive(Debug)] +pub(crate) enum Error { + IOError(io::Error), + UnableToCreateConfigDir, +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::IOError(e) => write!(f, "io error: {}", e), + Error::UnableToCreateConfigDir => write!(f, "Unable to create config dir"), + } + } +} + +impl StdError for Error {} + +impl From for Error { + fn from(err: io::Error) -> Error { + Error::IOError(err) + } +} diff --git a/src/lib.rs b/src/lib.rs index 4a99947a..96cefa51 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,11 @@ +#![feature(once_cell)] + +mod args; +mod error; mod logger; mod theming; +use crate::args::parse_args; use crate::logger::LogItem; use crate::theming::{curses_attr, parse_attrs}; @@ -8,6 +13,7 @@ use chrono::{Datelike, Timelike}; use pyo3::{ conversion::{IntoPy, ToPyObject}, create_exception, + exceptions::PyIOError, marker::Python, prelude::{pyfunction, pymodule, wrap_pyfunction, PyErr, PyModule, PyObject, PyResult}, types::{PyDateTime, PyDict}, @@ -20,6 +26,7 @@ fn libpoezio(py: Python, m: &PyModule) -> PyResult<()> { m.add("LogParseError", py.get_type::())?; m.add_function(wrap_pyfunction!(to_curses_attr, m)?)?; m.add_function(wrap_pyfunction!(parse_logs, m)?)?; + m.add_function(wrap_pyfunction!(run_cmdline_args, m)?)?; Ok(()) } @@ -86,3 +93,9 @@ fn parse_logs(py: Python, input: &str) -> PyResult { } Ok(items.into_py(py).to_object(py)) } + +#[pyfunction] +fn run_cmdline_args(py: Python, argv: Vec) -> PyResult<(PyObject, bool)> { + let (args, firstrun) = parse_args(argv).map_err(|err| PyIOError::new_err(err.to_string()))?; + Ok((args.into_py(py), firstrun)) +} -- cgit v1.2.3