diff --git a/crates/pet-conda/src/lib.rs b/crates/pet-conda/src/lib.rs index cc0d0318..fefaba4d 100644 --- a/crates/pet-conda/src/lib.rs +++ b/crates/pet-conda/src/lib.rs @@ -152,7 +152,7 @@ impl Locator for Conda { fn supported_categories(&self) -> Vec { vec![PythonEnvironmentCategory::Conda] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { // Possible we do not have the prefix, but this exe is in the bin directory and its a conda env or root conda install. let mut prefix = env.prefix.clone(); if prefix.is_none() { diff --git a/crates/pet-conda/tests/ci_test.rs b/crates/pet-conda/tests/ci_test.rs index e8f9eea4..c24b73de 100644 --- a/crates/pet-conda/tests/ci_test.rs +++ b/crates/pet-conda/tests/ci_test.rs @@ -82,7 +82,7 @@ fn detect_conda_root_from_path() { let conda = Conda::from(&env); let python_env = PythonEnv::new(exe, Some(conda_dir.clone()), None); - let env = conda.from(&python_env).unwrap(); + let env = conda.try_from(&python_env).unwrap(); assert_eq!(env.manager.is_some(), true); @@ -182,7 +182,7 @@ fn detect_conda_env_from_path() { let conda = Conda::from(&env); let python_env = PythonEnv::new(exe.clone(), Some(prefix.clone()), None); - let env = conda.from(&python_env).unwrap(); + let env = conda.try_from(&python_env).unwrap(); assert_eq!(env.manager.is_some(), true); diff --git a/crates/pet-conda/tests/lib_test.rs b/crates/pet-conda/tests/lib_test.rs index cc6f62a0..5430c143 100644 --- a/crates/pet-conda/tests/lib_test.rs +++ b/crates/pet-conda/tests/lib_test.rs @@ -19,7 +19,7 @@ fn find_conda_env_without_manager() { let path = resolve_test_path(&["unix", "conda_env_without_manager", "env_python_3"]); let env = locator - .from(&PythonEnv::new( + .try_from(&PythonEnv::new( path.join("bin").join("python").into(), Some(path.clone().into()), None, @@ -70,7 +70,7 @@ fn find_conda_env_without_manager_but_detect_manager_from_history() { fs::write(history_file, history_contents).unwrap(); let env = locator - .from(&PythonEnv::new( + .try_from(&PythonEnv::new( path.join("bin").join("python").into(), Some(path.clone().into()), None, diff --git a/crates/pet-core/src/lib.rs b/crates/pet-core/src/lib.rs index 9eae65cf..3c3209ce 100644 --- a/crates/pet-core/src/lib.rs +++ b/crates/pet-core/src/lib.rs @@ -47,7 +47,7 @@ pub trait Locator: Send + Sync { /// /// Note: The returned environment could have some missing information. /// This is because the `from` will do a best effort to get the environment information without spawning Python. - fn from(&self, env: &PythonEnv) -> Option; + fn try_from(&self, env: &PythonEnv) -> Option; /// Finds all environments specific to this locator. fn find(&self, reporter: &dyn Reporter); } diff --git a/crates/pet-homebrew/src/lib.rs b/crates/pet-homebrew/src/lib.rs index 5dd5c44d..b1448127 100644 --- a/crates/pet-homebrew/src/lib.rs +++ b/crates/pet-homebrew/src/lib.rs @@ -104,7 +104,7 @@ impl Locator for Homebrew { fn supported_categories(&self) -> Vec { vec![PythonEnvironmentCategory::Homebrew] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { from(env) } diff --git a/crates/pet-linux-global-python/src/lib.rs b/crates/pet-linux-global-python/src/lib.rs index 1f5bea13..038e6bdc 100644 --- a/crates/pet-linux-global-python/src/lib.rs +++ b/crates/pet-linux-global-python/src/lib.rs @@ -62,7 +62,7 @@ impl Locator for LinuxGlobalPython { vec![PythonEnvironmentCategory::LinuxGlobal] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if std::env::consts::OS == "macos" || std::env::consts::OS == "windows" { return None; } diff --git a/crates/pet-mac-commandlinetools/src/lib.rs b/crates/pet-mac-commandlinetools/src/lib.rs index 10bdcfb2..9342514f 100644 --- a/crates/pet-mac-commandlinetools/src/lib.rs +++ b/crates/pet-mac-commandlinetools/src/lib.rs @@ -35,7 +35,7 @@ impl Locator for MacCmdLineTools { vec![PythonEnvironmentCategory::MacCommandLineTools] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if std::env::consts::OS != "macos" { return None; } @@ -216,7 +216,7 @@ impl Locator for MacCmdLineTools { } } env.symlinks = Some(symlinks); - if let Some(env) = self.from(&env) { + if let Some(env) = self.try_from(&env) { _reporter.report_environment(&env); } } diff --git a/crates/pet-mac-python-org/src/lib.rs b/crates/pet-mac-python-org/src/lib.rs index e4ad95bf..671bdaf4 100644 --- a/crates/pet-mac-python-org/src/lib.rs +++ b/crates/pet-mac-python-org/src/lib.rs @@ -34,7 +34,7 @@ impl Locator for MacPythonOrg { vec![PythonEnvironmentCategory::MacPythonOrg] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if std::env::consts::OS != "macos" { return None; } @@ -158,7 +158,8 @@ impl Locator for MacPythonOrg { let executable = prefix.join("bin").join("python3"); let version = version::from_header_files(&prefix); - if let Some(env) = self.from(&PythonEnv::new(executable, Some(prefix), version)) { + if let Some(env) = self.try_from(&PythonEnv::new(executable, Some(prefix), version)) + { reporter.report_environment(&env); } } diff --git a/crates/pet-mac-xcode/src/lib.rs b/crates/pet-mac-xcode/src/lib.rs index 1c552637..3d2d2cd0 100644 --- a/crates/pet-mac-xcode/src/lib.rs +++ b/crates/pet-mac-xcode/src/lib.rs @@ -35,7 +35,7 @@ impl Locator for MacXCode { vec![PythonEnvironmentCategory::MacCommandLineTools] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if std::env::consts::OS != "macos" { return None; } @@ -204,7 +204,7 @@ impl Locator for MacXCode { // } // } // env.symlinks = Some(symlinks); - // if let Some(env) = self.from(&env) { + // if let Some(env) = self.try_from(&env) { // _reporter.report_environment(&env); // } // } diff --git a/crates/pet-pipenv/src/lib.rs b/crates/pet-pipenv/src/lib.rs index 89f5cc9f..75ccf09d 100644 --- a/crates/pet-pipenv/src/lib.rs +++ b/crates/pet-pipenv/src/lib.rs @@ -78,7 +78,7 @@ impl Locator for PipEnv { vec![PythonEnvironmentCategory::Pipenv] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if !is_pipenv(env, &self.env_vars) { return None; } diff --git a/crates/pet-poetry/src/lib.rs b/crates/pet-poetry/src/lib.rs index a7fcb6f9..58bfaa93 100644 --- a/crates/pet-poetry/src/lib.rs +++ b/crates/pet-poetry/src/lib.rs @@ -157,7 +157,7 @@ impl Locator for Poetry { vec![PythonEnvironmentCategory::Poetry] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if !is_virtualenv(env) { return None; } diff --git a/crates/pet-pyenv/src/lib.rs b/crates/pet-pyenv/src/lib.rs index 16045a9a..95828e77 100644 --- a/crates/pet-pyenv/src/lib.rs +++ b/crates/pet-pyenv/src/lib.rs @@ -58,7 +58,7 @@ impl Locator for PyEnv { ] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if let Some(prefix) = &env.prefix { if is_conda_env(prefix) { return None; diff --git a/crates/pet-pyenv/tests/pyenv_test.rs b/crates/pet-pyenv/tests/pyenv_test.rs index 795bbe46..814b1f7c 100644 --- a/crates/pet-pyenv/tests/pyenv_test.rs +++ b/crates/pet-pyenv/tests/pyenv_test.rs @@ -436,7 +436,7 @@ fn resolve_pyenv_environment() { }; // Resolve regular Python installs in Pyenv - let result = locator.from(&PythonEnv::new( + let result = locator.try_from(&PythonEnv::new( resolve_test_path(&[home.to_str().unwrap(), ".pyenv/versions/3.9.9/bin/python"]), Some(resolve_test_path(&[ home.to_str().unwrap(), @@ -448,7 +448,7 @@ fn resolve_pyenv_environment() { assert_eq!(result.unwrap(), expected_3_9_9); // Resolve regular virtual-envs in Pyenv - let result = locator.from(&PythonEnv::new( + let result = locator.try_from(&PythonEnv::new( resolve_test_path(&[ home.to_str().unwrap(), ".pyenv/versions/my-virtual-env/bin/python", @@ -463,7 +463,7 @@ fn resolve_pyenv_environment() { assert_eq!(result.unwrap(), expected_virtual_env); // Should not resolve conda envs in pyenv - let result = locator.from(&PythonEnv::new( + let result = locator.try_from(&PythonEnv::new( resolve_test_path(&[ home.to_str().unwrap(), ".pyenv/versions/anaconda-4.0.0/bin/python", @@ -478,7 +478,7 @@ fn resolve_pyenv_environment() { assert_eq!(result.is_none(), true); // Should not resolve conda envs using Conda Locator - let result = conda.from(&PythonEnv::new( + let result = conda.try_from(&PythonEnv::new( resolve_test_path(&[ home.to_str().unwrap(), ".pyenv/versions/anaconda-4.0.0/bin/python", diff --git a/crates/pet-venv/src/lib.rs b/crates/pet-venv/src/lib.rs index 9a1441f1..01fc58ab 100644 --- a/crates/pet-venv/src/lib.rs +++ b/crates/pet-venv/src/lib.rs @@ -40,7 +40,7 @@ impl Locator for Venv { vec![PythonEnvironmentCategory::Venv] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if is_venv(env) { let mut prefix = env.prefix.clone(); if prefix.is_none() { diff --git a/crates/pet-virtualenv/src/lib.rs b/crates/pet-virtualenv/src/lib.rs index de0b18d8..a3049537 100644 --- a/crates/pet-virtualenv/src/lib.rs +++ b/crates/pet-virtualenv/src/lib.rs @@ -78,7 +78,7 @@ impl Locator for VirtualEnv { vec![PythonEnvironmentCategory::VirtualEnv] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if is_virtualenv(env) { let version = match env.version { Some(ref v) => Some(v.clone()), diff --git a/crates/pet-virtualenvwrapper/src/lib.rs b/crates/pet-virtualenvwrapper/src/lib.rs index 11cc373e..9abda765 100644 --- a/crates/pet-virtualenvwrapper/src/lib.rs +++ b/crates/pet-virtualenvwrapper/src/lib.rs @@ -36,7 +36,7 @@ impl Locator for VirtualEnvWrapper { vec![PythonEnvironmentCategory::VirtualEnvWrapper] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { if !is_virtualenvwrapper(env, &self.env_vars) { return None; } diff --git a/crates/pet-windows-registry/src/lib.rs b/crates/pet-windows-registry/src/lib.rs index 1c2fc8d5..f9f4dcb6 100644 --- a/crates/pet-windows-registry/src/lib.rs +++ b/crates/pet-windows-registry/src/lib.rs @@ -69,7 +69,7 @@ impl Locator for WindowsRegistry { vec![PythonEnvironmentCategory::WindowsRegistry] } - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { // Assume we create a virtual env from a python install, // Then the exe in the virtual env bin will be a symlink to the homebrew python install. // Hence the first part of the condition will be true, but the second part will be false. diff --git a/crates/pet-windows-store/src/lib.rs b/crates/pet-windows-store/src/lib.rs index 688e5a1e..18992ac1 100644 --- a/crates/pet-windows-store/src/lib.rs +++ b/crates/pet-windows-store/src/lib.rs @@ -69,7 +69,7 @@ impl Locator for WindowsStore { } #[cfg(windows)] - fn from(&self, env: &PythonEnv) -> Option { + fn try_from(&self, env: &PythonEnv) -> Option { use pet_virtualenv::is_virtualenv; // Assume we create a virtual env from a python install, @@ -91,7 +91,7 @@ impl Locator for WindowsStore { } #[cfg(unix)] - fn from(&self, _env: &PythonEnv) -> Option { + fn try_from(&self, _env: &PythonEnv) -> Option { None } diff --git a/crates/pet/src/locators.rs b/crates/pet/src/locators.rs index 9d902f7a..7f3bebe9 100644 --- a/crates/pet/src/locators.rs +++ b/crates/pet/src/locators.rs @@ -86,10 +86,10 @@ pub fn identify_python_environment_using_locators( fallback_category: Option, ) -> Option { let executable = env.executable.clone(); - if let Some(env) = locators - .iter() - .fold(None, |e, loc| if e.is_some() { e } else { loc.from(env) }) - { + if let Some(env) = locators.iter().fold( + None, + |e, loc| if e.is_some() { e } else { loc.try_from(env) }, + ) { return Some(env); } @@ -98,9 +98,11 @@ pub fn identify_python_environment_using_locators( // We try to get the interpreter info, hoping that the real exe returned might be identifiable. if let Some(resolved_env) = ResolvedPythonEnv::from(&executable) { let env = resolved_env.to_python_env(); - if let Some(env) = locators - .iter() - .fold(None, |e, loc| if e.is_some() { e } else { loc.from(&env) }) + if let Some(env) = + locators.iter().fold( + None, + |e, loc| if e.is_some() { e } else { loc.try_from(&env) }, + ) { trace!( "Unknown Env ({:?}) in Path resolved as {:?}", diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index e2714829..184bf1de 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -51,7 +51,7 @@ mod common; /// by spawning the Python executable /// Verification 2: /// For each enviornment, given the executable verify we can get the exact same information -/// Using the `locators.from` method (without having to find all environments). +/// Using the `locator.try_from` method (without having to find all environments). /// I.e. we should be able to get the same information using only the executable. /// Verification 3: /// Similarly for each environment use one of the known symlinks and verify we can get the same information. @@ -96,7 +96,7 @@ fn verify_validity_of_discovered_envs() { })); // Verification 2: // For each enviornment, given the executable verify we can get the exact same information - // Using the `locators.from` method (without having to find all environments). + // Using the `locator.try_from` method (without having to find all environments). // I.e. we should be able to get the same information using only the executable. // // Verification 3: @@ -295,7 +295,7 @@ fn verify_we_can_get_same_env_inf_using_from_with_exe( ) { let mut environment = environment.clone(); - // Assume we were given a path to the exe, then we use the `locator.from` method. + // Assume we were given a path to the exe, then we use the `locator.try_from` method. // We should be able to get the exct same information back given only the exe. // // Note: We will not not use the old locator objects, as we do not want any cached information.