2023-09-20 16:51:27 +02:00
|
|
|
# Automatic poetry environment activation/deactivation
|
|
|
|
_togglePoetryShell() {
|
|
|
|
# deactivate environment if pyproject.toml doesn't exist and not in a subdir
|
|
|
|
if [[ ! -f "$PWD/pyproject.toml" ]] ; then
|
|
|
|
if [[ "$poetry_active" == 1 ]]; then
|
|
|
|
if [[ "$PWD" != "$poetry_dir"* ]]; then
|
|
|
|
export poetry_active=0
|
|
|
|
deactivate
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# activate the environment if pyproject.toml exists
|
|
|
|
if [[ "$poetry_active" != 1 ]]; then
|
|
|
|
if [[ -f "$PWD/pyproject.toml" ]]; then
|
2024-03-26 09:54:04 +01:00
|
|
|
if grep -q 'tool.poetry' "$PWD/pyproject.toml" && venv_dir=$(poetry env info --path); then
|
2023-09-20 16:51:27 +02:00
|
|
|
export poetry_active=1
|
|
|
|
export poetry_dir="$PWD"
|
2024-03-26 09:54:04 +01:00
|
|
|
source "${venv_dir}/bin/activate"
|
2023-09-20 16:51:27 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
autoload -U add-zsh-hook
|
|
|
|
add-zsh-hook chpwd _togglePoetryShell
|
|
|
|
_togglePoetryShell
|