@@ -15,6 +15,7 @@ use pyo3::{
1515use std:: borrow:: Borrow ;
1616use std:: collections:: { HashMap , HashSet } ;
1717use std:: path:: PathBuf ;
18+ use std:: ffi:: OsStr ;
1819use std:: sync:: { Arc , Mutex } ;
1920
2021fn anyhow_to_pyerr ( e : Error ) -> PyErr {
@@ -211,7 +212,17 @@ impl OntoEnv {
211212 temporary : bool ,
212213 no_search : bool ,
213214 ) -> PyResult < Self > {
214- let root_path = path. clone ( ) . unwrap_or_else ( || PathBuf :: from ( root) ) ;
215+ let mut root_path = path. clone ( ) . unwrap_or_else ( || PathBuf :: from ( root) ) ;
216+ // If the provided path points to a '.ontoenv' directory, treat its parent as the root
217+ if root_path
218+ . file_name ( )
219+ . map ( |n| n == OsStr :: new ( ".ontoenv" ) )
220+ . unwrap_or ( false )
221+ {
222+ if let Some ( parent) = root_path. parent ( ) {
223+ root_path = parent. to_path_buf ( ) ;
224+ }
225+ }
215226
216227 // Strict Git-like behavior:
217228 // - temporary=True: create a temporary (in-memory) env
@@ -250,15 +261,19 @@ impl OntoEnv {
250261 // Explicit create/overwrite at root_path
251262 OntoEnvRs :: init ( cfg, true ) . map_err ( anyhow_to_pyerr) ?
252263 } else {
253- // Discover upward from root_path; load if found, else error.
264+ // Discover upward from root_path; load if found. If not found and not read-only,
265+ // initialize a new environment at the requested root.
254266 match :: ontoenv:: api:: find_ontoenv_root_from ( & root_path) {
255267 Some ( found_root) => OntoEnvRs :: load_from_directory ( found_root, read_only)
256268 . map_err ( anyhow_to_pyerr) ?,
257269 None => {
258- return Err ( PyErr :: new :: < pyo3:: exceptions:: PyValueError , _ > ( format ! (
259- "OntoEnv directory not found at: \" {}\" " ,
260- root_path. join( ".ontoenv" ) . to_string_lossy( )
261- ) ) ) ;
270+ if read_only {
271+ return Err ( PyErr :: new :: < pyo3:: exceptions:: PyValueError , _ > ( format ! (
272+ "OntoEnv directory not found at: \" {}\" and read_only=True" ,
273+ root_path. join( ".ontoenv" ) . to_string_lossy( )
274+ ) ) ) ;
275+ }
276+ OntoEnvRs :: init ( cfg, false ) . map_err ( anyhow_to_pyerr) ?
262277 }
263278 }
264279 } ;
0 commit comments