|
| 1 | +#![feature(once_cell_try)] |
1 | 2 | use ::ontoenv as ontoenvrs; |
2 | 3 | use ::ontoenv::consts::{ONTOLOGY, TYPE}; |
3 | 4 | use ::ontoenv::ontology::OntologyLocation; |
@@ -124,7 +125,7 @@ struct Config { |
124 | 125 | #[pymethods] |
125 | 126 | impl Config { |
126 | 127 | #[new] |
127 | | - #[pyo3(signature = (search_directories, require_ontology_names=false, strict=false, offline=false, resolution_policy="default".to_owned(), root=".".to_owned(), includes=vec![], excludes=vec![]))] |
| 128 | + #[pyo3(signature = (search_directories=None, require_ontology_names=false, strict=false, offline=false, resolution_policy="default".to_owned(), root=".".to_owned(), includes=vec![], excludes=vec![]))] |
128 | 129 | fn new( |
129 | 130 | search_directories: Option<Vec<String>>, |
130 | 131 | require_ontology_names: bool, |
@@ -185,23 +186,34 @@ impl OntoEnv { |
185 | 186 | env_logger::init(); |
186 | 187 | }); |
187 | 188 |
|
188 | | - let env = ONTOENV_SINGLETON.get_or_init(|| { |
189 | | - let inner = if let Some(p) = path { |
190 | | - let config_path = p.join(".ontoenv").join("ontoenv.json"); |
191 | | - if let Ok(env) = ontoenvrs::OntoEnv::from_file(&config_path, read_only) { |
| 189 | + let config_path = path |
| 190 | + .as_ref() |
| 191 | + .map(|p| p.join(".ontoenv").join("ontoenv.json")); |
| 192 | + println!("Config path: {:?}", config_path); |
| 193 | + |
| 194 | + let env = ONTOENV_SINGLETON.get_or_try_init(|| { |
| 195 | + // if no Config provided, but there is a path, load the OntoEnv from file |
| 196 | + // otherwise, create a new OntoEnv |
| 197 | + if config.is_none() && config_path.is_some() && config_path.as_ref().unwrap().exists(){ |
| 198 | + if let Ok(env) = ontoenvrs::OntoEnv::from_file(&config_path.unwrap(), read_only) { |
192 | 199 | println!("Loaded OntoEnv from file"); |
193 | | - env |
194 | | - } else { |
195 | | - println!("Creating new OntoEnv"); |
196 | | - ontoenvrs::OntoEnv::new(config.unwrap().borrow().cfg.clone(), recreate) |
197 | | - .expect("Failed to create OntoEnv") |
| 200 | + return Ok(Arc::new(Mutex::new(env))); |
198 | 201 | } |
199 | | - } else { |
200 | | - ontoenvrs::OntoEnv::new(config.unwrap().borrow().cfg.clone(), recreate) |
201 | | - .expect("Failed to create OntoEnv") |
202 | | - }; |
203 | | - Arc::new(Mutex::new(inner)) |
204 | | - }); |
| 202 | + } |
| 203 | + |
| 204 | + // if config is provided, create a new OntoEnv with the provided config |
| 205 | + if let Some(c) = config { |
| 206 | + println!("Creating new OntoEnv with provided config"); |
| 207 | + let inner = ontoenvrs::OntoEnv::new(c.cfg.clone(), recreate) |
| 208 | + .map_err(anyhow_to_pyerr)?; |
| 209 | + return Ok(Arc::new(Mutex::new(inner))); |
| 210 | + } |
| 211 | + |
| 212 | + Err(PyErr::new::<pyo3::exceptions::PyValueError, _>( |
| 213 | + "Either a Config or a path must be provided. If path provided, there must be a valid OntoEnv directory at the path", |
| 214 | + )) |
| 215 | + |
| 216 | + })?; |
205 | 217 |
|
206 | 218 | { |
207 | 219 | let mut env = env.lock().unwrap(); |
|
0 commit comments