Skip to content

Commit

Permalink
Implement Python classmethod Config.from_dict()
Browse files Browse the repository at this point in the history
The method takes two arguments: the inventory base path, and a Python
dict with config options. Internally, the method converts the provided
config options into `(&str, serde_yaml::Value)` pairs which are passed
to `Config.set_option()`.
  • Loading branch information
simu committed Feb 23, 2024
1 parent f4141b9 commit b580745
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use anyhow::{anyhow, Result};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyType};
use regex::RegexSet;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashSet;
Expand Down Expand Up @@ -264,6 +266,35 @@ impl Config {
fn __repr__(&self) -> String {
format!("{self:#?}")
}

/// Creates a Config object based on the provided `inventory_path` and the config options
/// passed in the `config` Python dict.
///
/// Returns a `Config` object or raises a `ValueError`.
#[classmethod]
fn from_dict(_cls: &PyType, inventory_path: &str, config: &PyDict) -> PyResult<Self> {
let mut cfg = Config::new(Some(inventory_path), None, None, None).map_err(|e| {
PyValueError::new_err(format!(
"Failed to initialize reclass-rs config object: {e}"
))
})?;

// `set_option()` expects `cfg_path` to be the path to the reclass config file. Since we're
// not actually reading from the file here, we need to push an arbitrary path segment so
// that `set_option()` will configure the `nodes_path` and `classes_path` fields correctly.
let mut cfg_path = PathBuf::from(inventory_path);
cfg_path.push("dummy");

for (k, v) in config {
let kstr = k.extract::<&str>()?;
let val: crate::types::Value = TryInto::try_into(v)?;
cfg.set_option(&cfg_path, kstr, &val.into()).map_err(|e| {
PyValueError::new_err(format!("Error while setting option {kstr}: {e}"))
})?;
}

Ok(cfg)
}
}

#[cfg(test)]
Expand Down

0 comments on commit b580745

Please sign in to comment.