perf(python): Lazy eval of python version command (#2158)

* perf(python): evaluate version lazily

* fix(python): update format string

* fix: use suggested format strings

Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>

Co-authored-by: Moritz Vetter <mv@3yourmind.com>
Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
This commit is contained in:
Moritz Vetter 2021-01-20 18:42:55 +01:00 committed by GitHub
parent 8a80835f72
commit e437a463ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 34 deletions

View File

@ -2050,7 +2050,7 @@ The module will be shown if any of the following conditions are met:
| Option | Default | Description | | Option | Default | Description |
| -------------------- | ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------- | | -------------------- | ----------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| `format` | `'via [${symbol}${pyenv_prefix}${version}( \($virtualenv\))]($style) '` | The format for the module. | | `format` | `'via [${symbol}${pyenv_prefix}(${version} )(\($virtualenv\))]($style)'` | The format for the module. |
| `symbol` | `"🐍 "` | A format string representing the symbol of Python | | `symbol` | `"🐍 "` | A format string representing the symbol of Python |
| `style` | `"yellow bold"` | The style for the module. | | `style` | `"yellow bold"` | The style for the module. |
| `pyenv_version_name` | `false` | Use pyenv to get Python version | | `pyenv_version_name` | `false` | Use pyenv to get Python version |

View File

@ -21,7 +21,7 @@ impl<'a> RootModuleConfig<'a> for PythonConfig<'a> {
pyenv_prefix: "pyenv ", pyenv_prefix: "pyenv ",
python_binary: VecOr(vec!["python", "python3", "python2"]), python_binary: VecOr(vec!["python", "python3", "python2"]),
scan_for_pyfiles: true, scan_for_pyfiles: true,
format: "via [${symbol}${pyenv_prefix}${version}( \\($virtualenv\\))]($style) ", format: "via [${symbol}${pyenv_prefix}(${version} )(\\($virtualenv\\))]($style)",
style: "yellow bold", style: "yellow bold",
symbol: "🐍 ", symbol: "🐍 ",
disabled: false, disabled: false,

View File

@ -40,19 +40,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
if !is_py_project && !is_venv { if !is_py_project && !is_venv {
return None; return None;
}
let python_version = if config.pyenv_version_name {
utils::exec_cmd("pyenv", &["version-name"])?.stdout
} else {
let version = config
.python_binary
.0
.iter()
.find_map(|binary| get_python_version(binary))?;
format_python_version(&version)
}; };
let virtual_env = get_python_virtual_env(context);
let pyenv_prefix = if config.pyenv_version_name { let pyenv_prefix = if config.pyenv_version_name {
config.pyenv_prefix config.pyenv_prefix
} else { } else {
@ -70,9 +59,15 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None, _ => None,
}) })
.map(|variable| match variable { .map(|variable| match variable {
"version" => Some(Ok(python_version.trim())), "version" => {
"virtualenv" => virtual_env.as_ref().map(|e| Ok(e.trim())), let version = get_python_version(&config)?;
"pyenv_prefix" => Some(Ok(pyenv_prefix)), Some(Ok(version.trim().to_string()))
}
"virtualenv" => {
let virtual_env = get_python_virtual_env(context);
virtual_env.as_ref().map(|e| Ok(e.trim().to_string()))
}
"pyenv_prefix" => Some(Ok(pyenv_prefix.to_string())),
_ => None, _ => None,
}) })
.parse(None) .parse(None)
@ -89,8 +84,12 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) Some(module)
} }
fn get_python_version(python_binary: &str) -> Option<String> { fn get_python_version(config: &PythonConfig) -> Option<String> {
match utils::exec_cmd(python_binary, &["--version"]) { if config.pyenv_version_name {
return Some(utils::exec_cmd("pyenv", &["version-name"])?.stdout);
};
let version = config.python_binary.0.iter().find_map(|binary| {
match utils::exec_cmd(binary, &["--version"]) {
Some(output) => { Some(output) => {
if output.stdout.is_empty() { if output.stdout.is_empty() {
Some(output.stderr) Some(output.stderr)
@ -100,6 +99,8 @@ fn get_python_version(python_binary: &str) -> Option<String> {
} }
None => None, None => None,
} }
})?;
Some(format_python_version(&version))
} }
fn format_python_version(python_stdout: &str) -> String { fn format_python_version(python_stdout: &str) -> String {