Browse Source

python: Reformat and apply visual cleanup

Changes:
- Fallback to `virtualenvwrapper` without `pyenv` wrapper if `python` is
  available in path, regardless of whether `brew` is available.
- Look up for availability of `python2` and `python3`, not just `python`.
- Check for existence of path before adding to global `$path` array.
- Adhere to more idiomatic Zsh operation and minimize redundant syntaxes.
- Prefer looking up `$functions` for fully initialized `pyenv`.
master
Indrajit Raychaudhuri 4 years ago committed by Indrajit Raychaudhuri
parent
commit
d82805e12e
  1. 73
      modules/python/init.zsh

73
modules/python/init.zsh

@ -7,12 +7,12 @@
# Patrick Bos <egpbos@gmail.com> # Patrick Bos <egpbos@gmail.com>
# #
# Load dependencies # Load dependencies.
pmodload 'helper' pmodload 'helper'
# Load manually installed pyenv into the path # Load manually installed pyenv into the path
if [[ -s "${PYENV_ROOT:=$HOME/.pyenv}/bin/pyenv" ]]; then if [[ -s "${PYENV_ROOT:=$HOME/.pyenv}/bin/pyenv" ]]; then
path=("${PYENV_ROOT}/bin" $path) path=("$PYENV_ROOT/bin" $path)
eval "$(pyenv init - --no-rehash zsh)" eval "$(pyenv init - --no-rehash zsh)"
# Load pyenv into the current python session # Load pyenv into the current python session
@ -24,52 +24,48 @@ elif (( $+commands[pyenv] )); then
# path can be overridden using PYTHONUSERBASE. # path can be overridden using PYTHONUSERBASE.
else else
if [[ -n "$PYTHONUSERBASE" ]]; then if [[ -n "$PYTHONUSERBASE" ]]; then
path=($PYTHONUSERBASE/bin $path) path=($PYTHONUSERBASE/bin(N) $path)
elif is-darwin; then elif is-darwin; then
path=($HOME/Library/Python/*/bin(N) $path) path=($HOME/Library/Python/*/bin(N) $path)
else else
# This is subject to change. # This is subject to change.
path=($HOME/.local/bin $path) path=($HOME/.local/bin(N) $path)
fi fi
fi fi
# Return if requirements are not found. # Return if requirements are not found.
if (( ! $+commands[python] && ! $+commands[pyenv] )); then if (( ! $#commands[(i)python[23]#] && ! $+functions[pyenv] )); then
return 1 return 1
fi fi
function _python-workon-cwd { function _python-workon-cwd {
# Check if this is a Git repo # Check if this is a Git repo.
local GIT_REPO_ROOT="" local GIT_REPO_ROOT="$(git rev-parse --show-toplevel 2> /dev/null)"
local GIT_TOPLEVEL="$(git rev-parse --show-toplevel 2> /dev/null)" # Get absolute path, resolving symlinks.
if [[ $? == 0 ]]; then local PROJECT_ROOT="$PWD:A"
GIT_REPO_ROOT="$GIT_TOPLEVEL"
fi
# Get absolute path, resolving symlinks
local PROJECT_ROOT="${PWD:A}"
while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \ while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
&& ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do && ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
PROJECT_ROOT="${PROJECT_ROOT:h}" PROJECT_ROOT="$PROJECT_ROOT:h"
done done
if [[ "$PROJECT_ROOT" == "/" ]]; then if [[ $PROJECT_ROOT == "/" ]]; then
PROJECT_ROOT="." PROJECT_ROOT="."
fi fi
# Check for virtualenv name override # Check for virtualenv name override.
local ENV_NAME="" local ENV_NAME=""
if [[ -f "$PROJECT_ROOT/.venv" ]]; then if [[ -f "$PROJECT_ROOT/.venv" ]]; then
ENV_NAME="$(cat "$PROJECT_ROOT/.venv")" ENV_NAME="$(<$PROJECT_ROOT/.venv)"
elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]]; then elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]]; then
ENV_NAME="$PROJECT_ROOT/.venv" ENV_NAME="$PROJECT_ROOT/.venv"
elif [[ "$PROJECT_ROOT" != "." ]]; then elif [[ $PROJECT_ROOT != "." ]]; then
ENV_NAME="${PROJECT_ROOT:t}" ENV_NAME="$PROJECT_ROOT:t"
fi fi
if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
# We've just left the repo, deactivate the environment # We've just left the repo, deactivate the environment.
# Note: this only happens if the virtualenv was activated automatically # Note: this only happens if the virtualenv was activated automatically.
deactivate && unset CD_VIRTUAL_ENV deactivate && unset CD_VIRTUAL_ENV
fi fi
if [[ "$ENV_NAME" != "" ]]; then if [[ $ENV_NAME != "" ]]; then
# Activate the environment only if it is not already active # Activate the environment only if it is not already active.
if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
if [[ -n "$WORKON_HOME" && -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then if [[ -n "$WORKON_HOME" && -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME" workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
@ -80,17 +76,17 @@ function _python-workon-cwd {
fi fi
} }
# Load auto workon cwd hook # Load auto workon cwd hook.
if zstyle -t ':prezto:module:python:virtualenv' auto-switch 'yes'; then if zstyle -t ':prezto:module:python:virtualenv' auto-switch; then
# Auto workon when changing directory # Auto workon when changing directory.
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook
add-zsh-hook chpwd _python-workon-cwd add-zsh-hook chpwd _python-workon-cwd
fi fi
# Load virtualenvwrapper into the shell session, if pre-requisites are met # Load virtualenvwrapper into the shell session, if pre-requisites are met
# and unless explicitly requested not to # and unless explicitly requested not to
if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) && \ if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) \
zstyle -T ':prezto:module:python:virtualenv' initialize ; then && zstyle -T ':prezto:module:python:virtualenv' initialize ; then
# Set the directory where virtual environments are stored. # Set the directory where virtual environments are stored.
export WORKON_HOME="${WORKON_HOME:-$HOME/.virtualenvs}" export WORKON_HOME="${WORKON_HOME:-$HOME/.virtualenvs}"
@ -117,19 +113,10 @@ if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) && \
pyenv "$pyenv_plugins[(R)virtualenvwrapper(_lazy|)]" pyenv "$pyenv_plugins[(R)virtualenvwrapper(_lazy|)]"
fi fi
else else
# Fallback to 'virtualenvwrapper' without 'pyenv' wrapper if available # Fallback to 'virtualenvwrapper' without 'pyenv' wrapper if 'python' is
# in '$path' or in an alternative location on a Debian based system. # available in '$path'.
# if (( ! $+VIRTUALENVWRAPPER_PYTHON )) && (( $#commands[(i)python[23]#] )); then
# If homebrew is installed and the python location wasn't overridden via VIRTUALENVWRAPPER_PYTHON=$commands[(i)python[23]#]
# environment variable we fall back to python3 then python2 in that order.
# This is needed to fix an issue with virtualenvwrapper as homebrew no
# longer shadows the system python.
if [[ -z "$VIRTUALENVWRAPPER_PYTHON" ]] && (( $+commands[brew] )); then
if (( $+commands[python3] )); then
export VIRTUALENVWRAPPER_PYTHON=$commands[python3]
elif (( $+commands[python2] )); then
export VIRTUALENVWRAPPER_PYTHON=$commands[python2]
fi
fi fi
virtenv_sources=( virtenv_sources=(
@ -137,7 +124,7 @@ if (( $+VIRTUALENVWRAPPER_VIRTUALENV || $+commands[virtualenv] )) && \
/usr/share/virtualenvwrapper/virtualenvwrapper(_lazy|).sh(OnN) /usr/share/virtualenvwrapper/virtualenvwrapper(_lazy|).sh(OnN)
) )
if (( $#virtenv_sources )); then if (( $#virtenv_sources )); then
source "${virtenv_sources[1]}" source "$virtenv_sources[1]"
fi fi
unset virtenv_sources unset virtenv_sources
@ -177,7 +164,7 @@ if [[ -n "$pip_command" ]]; then
fi fi
unset pip_command unset pip_command
# Load conda into the shell session, if requested # Load conda into the shell session, if requested.
zstyle -T ':prezto:module:python' conda-init zstyle -T ':prezto:module:python' conda-init
if (( $? && $+commands[conda] )); then if (( $? && $+commands[conda] )); then
if (( $(conda ..changeps1) )); then if (( $(conda ..changeps1) )); then

Loading…
Cancel
Save