MCPcopy Create free account
hub / github.com/MegEngine/MegFlow / path_to_module

Function path_to_module

flow-rs/src/loader/python/mod.rs:233–250  ·  view source on GitHub ↗
(root: &Path, path: &Path)

Source from the content-addressed store, hash-verified

231);
232
233fn path_to_module(root: &Path, path: &Path) -> Result<String> {
234 let abs_root = std::fs::canonicalize(root)?;
235 let abs_path = std::fs::canonicalize(path)?;
236
237 if abs_path.starts_with(&abs_root) && abs_path != abs_root {
238 let relative = abs_path.strip_prefix(&abs_root)?;
239 let relative = relative.to_str().unwrap().to_owned();
240 if relative.ends_with(".py") {
241 Ok(relative[..relative.len() - 3].replace("/", "."))
242 } else if relative.ends_with('/') {
243 Ok(relative[..relative.len() - 1].replace("/", "."))
244 } else {
245 Ok(relative.replace("/", "."))
246 }
247 } else {
248 Err(anyhow::anyhow!("module not found"))
249 }
250}
251
252#[cfg(test)]
253mod test {

Callers 2

load_from_fileMethod · 0.85
test_path_to_moduleFunction · 0.85

Calls 1

lenMethod · 0.45

Tested by 1

test_path_to_moduleFunction · 0.68