Skip to content

Commit 9f152d9

Browse files
committed
adding Reset and no_search to support input of ontology list file
1 parent ecb1856 commit 9f152d9

File tree

6 files changed

+70
-31
lines changed

6 files changed

+70
-31
lines changed

cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ env_logger.workspace = true
2222
oxigraph.workspace = true
2323
chrono.workspace = true
2424
walkdir.workspace = true
25+
serde_json.workspace = true

cli/src/main.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use anyhow::Result;
22
use clap::{Parser, Subcommand};
3+
use ontoenv::config::{Config, EnvironmentConfig};
34
use ontoenv::ontology::{GraphIdentifier, OntologyLocation};
45
use ontoenv::util::write_dataset_to_file;
5-
use ontoenv::{config::Config, OntoEnv, ontology_config::{OntologyConfig, OntologyLocation}};
6+
use ontoenv::OntoEnv;
67
use oxigraph::model::{NamedNode, NamedNodeRef};
8+
use serde_json;
79
use std::env::current_dir;
10+
use std::fs::File;
811
use std::path::PathBuf;
912

1013
#[derive(Debug, Parser)]
@@ -46,14 +49,15 @@ enum Commands {
4649
/// Glob patterns for which files to exclude, defaults to []
4750
#[clap(long, short, num_args = 1..)]
4851
excludes: Vec<String>,
52+
/// Recreate the environment if it already exists
4953
#[clap(long, short, default_value = "false")]
5054
recreate: bool,
51-
},
52-
/// Fetch all ontologies and their dependencies from a configuration file
53-
Fetch {
54-
/// Path to the environment JSON configuration file
55-
#[clap(long, short)]
56-
config_file: PathBuf,
55+
/// A JSON file containing a list of ontologies to add to the environment
56+
#[clap(long = "list", short = 'l')]
57+
ontology_list_file: Option<String>,
58+
/// Do not search for ontologies in the search directories
59+
#[clap(long = "no-search", short = 'n', action)]
60+
no_search: bool,
5761
},
5862
/// Prints the version of the ontoenv binary
5963
Version,
@@ -106,6 +110,8 @@ enum Commands {
106110
},
107111
/// Run the doctor to check the environment for issues
108112
Doctor,
113+
/// Reset the ontology environment by removing the .ontoenv directory
114+
Reset,
109115
}
110116

111117
fn main() -> Result<()> {
@@ -127,8 +133,11 @@ fn main() -> Result<()> {
127133
includes,
128134
excludes,
129135
recreate,
136+
ontology_list_file,
137+
no_search,
130138
} => {
131139
// if search_directories is empty, use the current directory
140+
println!("no search? {}", no_search);
132141
let config = Config::new(
133142
current_dir()?,
134143
search_directories,
@@ -138,8 +147,19 @@ fn main() -> Result<()> {
138147
strict,
139148
offline,
140149
policy,
150+
no_search,
141151
)?;
142152
let mut env = OntoEnv::new(config, recreate)?;
153+
154+
// if an ontology config file is provided, load it and add the ontologies
155+
if let Some(file) = ontology_list_file {
156+
let file = File::open(file)?;
157+
let config: EnvironmentConfig = serde_json::from_reader(file)?;
158+
for ont in config.ontologies {
159+
env.add(ont.location)?;
160+
}
161+
}
162+
143163
env.update()?;
144164
env.save_to_directory()?;
145165
}
@@ -276,27 +296,12 @@ fn main() -> Result<()> {
276296
let env = OntoEnv::from_file(&path, true)?;
277297
env.doctor();
278298
}
279-
Commands::Fetch { config_file } => {
280-
// Load the configuration from the specified JSON file
281-
let config: EnvironmentConfig = serde_json::from_reader(std::fs::File::open(&config_file)?)?;
282-
283-
// Create a new OntoEnv with the default configuration
284-
let mut env = OntoEnv::new(Config::default(), false)?;
285-
286-
// Iterate over each ontology in the configuration and add it to the environment
287-
for ontology in config.ontologies {
288-
let location = match ontology.location {
289-
OntologyLocation::File { file } => OntologyLocation::File(file),
290-
OntologyLocation::Uri { uri } => OntologyLocation::Url(uri),
291-
};
292-
env.add(location)?;
299+
Commands::Reset => {
300+
// remove .ontoenv directory
301+
let path = current_dir()?.join(".ontoenv");
302+
if path.exists() {
303+
std::fs::remove_dir_all(path)?;
293304
}
294-
295-
// Update the environment to fetch dependencies
296-
env.update()?;
297-
298-
// Save the updated environment
299-
env.save_to_directory()?;
300305
}
301306
}
302307

lib/src/config.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::ontology::OntologyLocation;
12
use crate::policy::{DefaultPolicy, ResolutionPolicy};
23
use anyhow::Result;
34
use glob::{Pattern, PatternError};
@@ -25,6 +26,18 @@ where
2526
patterns.map_err(serde::de::Error::custom)
2627
}
2728

29+
#[derive(Serialize, Deserialize, Debug)]
30+
pub struct EnvironmentConfig {
31+
pub ontologies: Vec<OntologyConfig>,
32+
}
33+
34+
#[derive(Serialize, Deserialize, Debug)]
35+
pub struct OntologyConfig {
36+
#[serde(flatten)]
37+
pub location: OntologyLocation,
38+
pub version: Option<String>,
39+
}
40+
2841
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
2942
pub struct Config {
3043
pub root: PathBuf,
@@ -63,6 +76,7 @@ impl Config {
6376
strict: bool,
6477
offline: bool,
6578
resolution_policy: String,
79+
no_search: bool,
6680
) -> Result<Self>
6781
where
6882
I: IntoIterator,
@@ -72,9 +86,16 @@ impl Config {
7286
K: IntoIterator<Item = PathBuf>,
7387
{
7488
// if search directories are empty, add the root. Otherwise, use the provided search directories
89+
// if no_search is true, then do not default to the root directory
7590
let search_directories = search_directories
7691
.map(|dirs| dirs.into_iter().collect())
77-
.unwrap_or_else(|| vec![root.clone()]);
92+
.unwrap_or_else(|| {
93+
if no_search {
94+
vec![]
95+
} else {
96+
vec![root.clone()]
97+
}
98+
});
7899

79100
let mut config = Config {
80101
root,
@@ -135,6 +156,7 @@ impl Config {
135156
strict,
136157
offline,
137158
DefaultPolicy.policy_name().to_string(),
159+
false,
138160
)
139161
}
140162

lib/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ extern crate derive_builder;
22

33
pub mod config;
44
pub mod consts;
5-
pub mod environment_config;
65
pub mod doctor;
76
pub mod errors;
87
pub mod ontology;
@@ -71,7 +70,9 @@ pub struct EnvironmentStatus {
7170
// impl Display pretty print for EnvironmentStatus
7271
impl std::fmt::Display for EnvironmentStatus {
7372
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
74-
let exists = if self.exists { "Yes" } else { "No" };
73+
if !self.exists {
74+
return write!(f, "No environment found");
75+
}
7576
// convert last_updated to local timestamp, or display N/A if
7677
// it is None
7778
let last_updated = match self.last_updated {
@@ -202,7 +203,6 @@ impl OntoEnv {
202203

203204
/// Calculates and returns the environment status
204205
pub fn status(&self) -> Result<EnvironmentStatus> {
205-
let store = self.store()?;
206206
// get time modified of the self.store_path() directory
207207
let last_updated: DateTime<Utc> = std::fs::metadata(self.store_path()?)?.modified()?.into();
208208
// get the size of the .ontoenv directory on disk
@@ -277,6 +277,14 @@ impl OntoEnv {
277277

278278
/// Load an OntoEnv from the given path
279279
pub fn from_file(path: &Path, read_only: bool) -> Result<Self> {
280+
// if path does not exist, return an error
281+
if !path.exists() {
282+
return Err(anyhow::anyhow!(
283+
"OntoEnv environment not found at: {:?}",
284+
path
285+
));
286+
}
287+
280288
let file = std::fs::File::open(path)?;
281289
let reader = BufReader::new(file);
282290
let env: OntoEnv = serde_json::from_reader(reader)?;

lib/src/ontology.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ impl GraphIdentifier {
9494

9595
#[derive(Serialize, Deserialize, Hash, Clone, Eq, PartialEq, Debug)]
9696
pub enum OntologyLocation {
97+
#[serde(rename = "file")]
9798
File(PathBuf),
99+
#[serde(rename = "url")]
98100
Url(String),
99101
}
100102

python/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ impl Config {
158158
strict,
159159
offline,
160160
resolution_policy.to_string(),
161+
false,
161162
)
162163
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?,
163164
})

0 commit comments

Comments
 (0)