Skip to content

Commit 295cd9a

Browse files
committed
allow failure when init ontoenv in Python. more flexibility
1 parent 15ac599 commit 295cd9a

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

cli/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ fn main() -> Result<()> {
138138
env.save_to_directory()?;
139139
}
140140
Commands::Version => {
141-
println!("ontoenv {} @ {}", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"));
141+
println!(
142+
"ontoenv {} @ {}",
143+
env!("CARGO_PKG_VERSION"),
144+
env!("GIT_HASH")
145+
);
142146
}
143147
Commands::Status => {
144148
// load env from .ontoenv/ontoenv.json

lib/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ where
2525
patterns.map_err(serde::de::Error::custom)
2626
}
2727

28-
#[derive(Serialize, Deserialize, Debug, Clone)]
28+
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
2929
pub struct Config {
3030
pub root: PathBuf,
3131
#[serde(default)]
@@ -165,7 +165,7 @@ impl Config {
165165
let file = std::fs::File::open(file)?;
166166
let reader = BufReader::new(file);
167167
let mut config: Config = serde_json::from_reader(reader)?;
168-
168+
169169
if config.search_directories.is_empty() {
170170
config.search_directories = vec![config.root.clone()];
171171
}

lib/src/lib.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,36 @@ impl OntoEnv {
110110
// create the config.root/.ontoenv directory so it exists before the store
111111
// is created
112112
let ontoenv_dir = config.root.join(".ontoenv");
113+
let config_path = ontoenv_dir.join("ontoenv.json");
113114

114-
// if recreate is False, raise an error if the directory already exists
115-
if ontoenv_dir.exists() && !recreate {
116-
return Err(anyhow::anyhow!(
117-
"OntoEnv directory already exists: {:?}. Run 'refresh' or use the --recreate flag to recreate the environment.",
115+
// test if the config in the ontoenv_dir is different from the current config.
116+
// If it is, replace the config with the current config and turn 'recreate' on
117+
if ontoenv_dir.exists() && config_path.exists() {
118+
println!(
119+
"OntoEnv directory exists: {:?}. Checking config",
118120
ontoenv_dir
119-
));
121+
);
122+
let file = std::fs::File::open(&config_path)?;
123+
let reader = BufReader::new(file);
124+
let env: OntoEnv = serde_json::from_reader(reader)?;
125+
// print old and new config
126+
println!("Old config: {:?}", env.config);
127+
println!("New config: {:?}", config);
128+
if env.config != config {
129+
info!("OntoEnv configuration has changed. Recreating environment.");
130+
fs::remove_dir_all(&ontoenv_dir)?;
131+
return Self::new(config, true);
132+
}
120133
}
121134

135+
// if recreate is False, raise an error if the directory already exists
136+
//if ontoenv_dir.exists() && !recreate {
137+
// return Err(anyhow::anyhow!(
138+
// "OntoEnv directory already exists: {:?}. Run 'refresh' or use the --recreate flag to recreate the environment.",
139+
// ontoenv_dir
140+
// ));
141+
//}
142+
122143
std::fs::create_dir_all(&ontoenv_dir)?;
123144

124145
// create the store in the root/.ontoenv/store.db directory

python/src/lib.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(once_cell_try)]
12
use ::ontoenv as ontoenvrs;
23
use ::ontoenv::consts::{ONTOLOGY, TYPE};
34
use ::ontoenv::ontology::OntologyLocation;
@@ -124,7 +125,7 @@ struct Config {
124125
#[pymethods]
125126
impl Config {
126127
#[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![]))]
128129
fn new(
129130
search_directories: Option<Vec<String>>,
130131
require_ontology_names: bool,
@@ -185,23 +186,34 @@ impl OntoEnv {
185186
env_logger::init();
186187
});
187188

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) {
192199
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)));
198201
}
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+
})?;
205217

206218
{
207219
let mut env = env.lock().unwrap();

0 commit comments

Comments
 (0)