Parent directory

python.plugin.zsh

3810 bytes
  1# set python command if 'py' not installed
  2builtin which py > /dev/null || alias py='python3'
  3
  4# Find python file
  5alias pyfind='find . -name "*.py"'
  6
  7# Remove python compiled byte-code and mypy/pytest cache in either the current
  8# directory or in a list of specified directories (including sub directories).
  9function pyclean() {
 10  find "${@:-.}" -type f -name "*.py[co]" -delete
 11  find "${@:-.}" -type d -name "__pycache__" -delete
 12  find "${@:-.}" -depth -type d -name ".mypy_cache" -exec rm -r "{}" +
 13  find "${@:-.}" -depth -type d -name ".pytest_cache" -exec rm -r "{}" +
 14}
 15
 16# Add the user installed site-packages paths to PYTHONPATH, only if the
 17#   directory exists. Also preserve the current PYTHONPATH value.
 18# Feel free to autorun this when .zshrc loads.
 19function pyuserpaths() {
 20  setopt localoptions extendedglob
 21
 22  # Check for a non-standard install directory.
 23  local user_base="${PYTHONUSERBASE:-"${HOME}/.local"}"
 24
 25  local python version site_pkgs
 26  for python in python2 python3; do
 27    # Check if command exists
 28    (( ${+commands[$python]} )) || continue
 29
 30    # Get minor release version.
 31    # The patch version is variable length, truncate it.
 32    version=${(M)${"$($python -V 2>&1)":7}#[^.]##.[^.]##}
 33
 34    # Add version specific path, if:
 35    # - it exists in the filesystem
 36    # - it isn't in $PYTHONPATH already.
 37    site_pkgs="${user_base}/lib/python${version}/site-packages"
 38    [[ -d "$site_pkgs" && ! "$PYTHONPATH" =~ (^|:)"$site_pkgs"(:|$) ]] || continue
 39    export PYTHONPATH="${site_pkgs}${PYTHONPATH+":${PYTHONPATH}"}"
 40  done
 41}
 42
 43# Grep among .py files
 44alias pygrep='grep -nr --include="*.py"'
 45
 46# Share local directory as a HTTP server
 47alias pyserver="python3 -m http.server"
 48
 49
 50## venv settings
 51: ${PYTHON_VENV_NAME:=venv}
 52
 53# Array of possible virtual environment names to look for, in order
 54# -U for removing duplicates
 55typeset -gaU PYTHON_VENV_NAMES
 56[[ -n "$PYTHON_VENV_NAMES" ]] || PYTHON_VENV_NAMES=($PYTHON_VENV_NAME venv .venv)
 57
 58# Activate a the python virtual environment specified.
 59# If none specified, use the first existing in $PYTHON_VENV_NAMES.
 60function vrun() {
 61  if [[ -z "$1" ]]; then
 62    local name
 63    for name in $PYTHON_VENV_NAMES; do
 64      local venvpath="${name:P}"
 65      if [[ -d "$venvpath" ]]; then
 66        vrun "$name"
 67        return $?
 68      fi
 69    done
 70    echo >&2 "Error: no virtual environment found in current directory"
 71  fi
 72
 73  local name="${1:-$PYTHON_VENV_NAME}"
 74  local venvpath="${name:P}"
 75
 76  if [[ ! -d "$venvpath" ]]; then
 77    echo >&2 "Error: no such venv in current directory: $name"
 78    return 1
 79  fi
 80
 81  if [[ ! -f "${venvpath}/bin/activate" ]]; then
 82    echo >&2 "Error: '${name}' is not a proper virtual environment"
 83    return 1
 84  fi
 85
 86  . "${venvpath}/bin/activate" || return $?
 87  echo "Activated virtual environment ${name}"
 88}
 89
 90# Create a new virtual environment using the specified name.
 91# If none specified, use $PYTHON_VENV_NAME
 92function mkv() {
 93  local name="${1:-$PYTHON_VENV_NAME}"
 94  local venvpath="${name:P}"
 95
 96  python3 -m venv "${name}" || return
 97  echo >&2 "Created venv in '${venvpath}'"
 98  vrun "${name}"
 99}
100
101if [[ "$PYTHON_AUTO_VRUN" == "true" ]]; then
102  # Automatically activate venv when changing dir
103  function auto_vrun() {
104    # deactivate if we're on a different dir than VIRTUAL_ENV states
105    # we don't deactivate subdirectories!
106    if (( $+functions[deactivate] )) && [[ $PWD != ${VIRTUAL_ENV:h}* ]]; then
107      deactivate > /dev/null 2>&1
108    fi
109
110    if [[ $PWD != ${VIRTUAL_ENV:h} ]]; then
111      local file
112      for file in "${^PYTHON_VENV_NAMES[@]}"/bin/activate(N.); do
113        # make sure we're not in a venv already
114        (( $+functions[deactivate] )) && deactivate > /dev/null 2>&1
115        source $file > /dev/null 2>&1
116        break
117      done
118    fi
119  }
120  add-zsh-hook chpwd auto_vrun
121  auto_vrun
122fi