diff --git a/src/config.rs b/src/config.rs index 91c69b4..c52697d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -131,6 +131,7 @@ impl Config { cfg_path: &std::path::Path, k: &str, v: &serde_yaml::Value, + verbose: bool, ) -> Result<()> { let vstr = serde_yaml::to_string(v)?; let vstr = vstr.trim(); @@ -192,7 +193,11 @@ impl Config { } } _ => { - eprintln!("reclass-config.yml entry '{k}={vstr}' not implemented yet, ignoring..."); + if verbose { + eprintln!( + "reclass-config.yml entry '{k}={vstr}' not implemented yet, ignoring..." + ); + } } }; @@ -203,8 +208,9 @@ impl Config { /// /// This method assumes that you've created a Config object with a suitable `inventory_path`. /// - /// The method will print diagnostic messages for config options which aren't implemented yet. - pub fn load_from_file(&mut self, config_file: &str) -> Result<()> { + /// If `verbose` is true, the method will print diagnostic messages for config options which + /// aren't implemented yet. + pub fn load_from_file(&mut self, config_file: &str, verbose: bool) -> Result<()> { let mut cfg_path = PathBuf::from(&self.inventory_path); cfg_path.push(config_file); @@ -216,7 +222,7 @@ impl Config { { let kstr = serde_yaml::to_string(k)?; let kstr = kstr.trim(); - self.set_option(&cfg_path, kstr, v)?; + self.set_option(&cfg_path, kstr, v, verbose)?; } self.compile_ignore_class_notfound_patterns()?; Ok(()) @@ -268,11 +274,18 @@ impl Config { } /// Creates a Config object based on the provided `inventory_path` and the config options - /// passed in the `config` Python dict. + /// passed in the `config` Python dict. If `verbose` is set to `true`, reclass-rs will print + /// diagnostic messages for unknown config options. /// /// Returns a `Config` object or raises a `ValueError`. #[classmethod] - fn from_dict(_cls: &PyType, inventory_path: &str, config: &PyDict) -> PyResult { + #[pyo3(signature = (inventory_path, config, verbose=false))] + fn from_dict( + _cls: &PyType, + inventory_path: &str, + config: &PyDict, + verbose: bool, + ) -> PyResult { 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}" @@ -288,9 +301,10 @@ impl Config { 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}")) - })?; + cfg.set_option(&cfg_path, kstr, &val.into(), verbose) + .map_err(|e| { + PyValueError::new_err(format!("Error while setting option {kstr}: {e}")) + })?; } Ok(cfg) diff --git a/src/inventory.rs b/src/inventory.rs index 294f686..96b9551 100644 --- a/src/inventory.rs +++ b/src/inventory.rs @@ -101,7 +101,7 @@ mod inventory_tests { #[test] fn test_render() { let mut c = crate::Config::new(Some("./tests/inventory"), None, None, None).unwrap(); - c.load_from_file("reclass-config.yml").unwrap(); + c.load_from_file("reclass-config.yml", false).unwrap(); let r = Reclass::new_from_config(c).unwrap(); let inv = Inventory::render(&r).unwrap(); @@ -263,7 +263,8 @@ mod inventory_tests { None, ) .unwrap(); - c.load_from_file("reclass-config-compat.yml").unwrap(); + c.load_from_file("reclass-config-compat.yml", false) + .unwrap(); let r = Reclass::new_from_config(c).unwrap(); let inv = Inventory::render(&r).unwrap(); @@ -312,7 +313,7 @@ mod inventory_tests { None, ) .unwrap(); - c.load_from_file("reclass-config.yml").unwrap(); + c.load_from_file("reclass-config.yml", false).unwrap(); let r = Reclass::new_from_config(c).unwrap(); let inv = Inventory::render(&r).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 891d275..7519297 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -293,10 +293,16 @@ impl Reclass { /// /// Returns a `Reclass` instance or raises a `ValueError` #[classmethod] - fn from_config_file(cls: &PyType, inventory_path: &str, config_file: &str) -> PyResult { + #[pyo3(signature = (inventory_path, config_file, verbose=false))] + fn from_config_file( + cls: &PyType, + inventory_path: &str, + config_file: &str, + verbose: bool, + ) -> PyResult { let mut c = Config::new(Some(inventory_path), None, None, None) .map_err(|e| PyValueError::new_err(format!("{e}")))?; - c.load_from_file(config_file) + c.load_from_file(config_file, verbose) .map_err(|e| PyValueError::new_err(format!("{e}")))?; Self::from_config(cls, c) } @@ -447,7 +453,7 @@ mod tests { None, ) .unwrap(); - c.load_from_file("reclass-config.yml").unwrap(); + c.load_from_file("reclass-config.yml", true).unwrap(); let r = Reclass::new_from_config(c).unwrap(); assert_eq!(r.nodes.len(), 8); let mut nodes = r.nodes.keys().collect::>(); diff --git a/src/node/node_render_tests_ignore_class_notfound_regexp.rs b/src/node/node_render_tests_ignore_class_notfound_regexp.rs index 90b617b..76bcf0a 100644 --- a/src/node/node_render_tests_ignore_class_notfound_regexp.rs +++ b/src/node/node_render_tests_ignore_class_notfound_regexp.rs @@ -10,7 +10,7 @@ fn test_render_n1() { None, ) .unwrap(); - c.load_from_file("reclass-config.yml").unwrap(); + c.load_from_file("reclass-config.yml", false).unwrap(); let r = Reclass::new_from_config(c).unwrap(); let n1 = r.render_node("n1").unwrap(); @@ -33,7 +33,7 @@ fn test_render_n2() { None, ) .unwrap(); - c.load_from_file("reclass-config.yml").unwrap(); + c.load_from_file("reclass-config.yml", false).unwrap(); let r = Reclass::new_from_config(c).unwrap(); let n2 = r.render_node("n2");