1
0
Fork 0

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`.
pull/1941/head
Indrajit Raychaudhuri 4 years ago committed by Indrajit Raychaudhuri
parent 9037fda36d
commit d686da3c03

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

Loading…
Cancel
Save