78c91ccbf99c77bd4d9cdb74279a40776721f66d

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

style(python): fix code style and some hidden bugs

Diff

 1diff --git a/plugins/python/python.plugin.zsh b/plugins/python/python.plugin.zsh
 2index 6c7fc6cbba63c197c8af4d5f0c4b8aa76011fc8f..7a73ab92a7a47bfd7843067aeae0417bbef96c71 100644
 3--- a/plugins/python/python.plugin.zsh
 4+++ b/plugins/python/python.plugin.zsh
 5@@ -7,40 +7,37 @@ alias pyfind='find . -name "*.py"'
 6 # Remove python compiled byte-code and mypy/pytest cache in either the current
 7 # directory or in a list of specified directories (including sub directories).
 8 function pyclean() {
 9-    ZSH_PYCLEAN_PLACES=${*:-'.'}
10-    find ${ZSH_PYCLEAN_PLACES} -type f -name "*.py[co]" -delete
11-    find ${ZSH_PYCLEAN_PLACES} -type d -name "__pycache__" -delete
12-    find ${ZSH_PYCLEAN_PLACES} -depth -type d -name ".mypy_cache" -exec rm -r "{}" +
13-    find ${ZSH_PYCLEAN_PLACES} -depth -type d -name ".pytest_cache" -exec rm -r "{}" +
14+  find "${@:-.}" -type f -name "*.py[co]" -delete
15+  find "${@:-.}" -type d -name "__pycache__" -delete
16+  find "${@:-.}" -depth -type d -name ".mypy_cache" -exec rm -r "{}" +
17+  find "${@:-.}" -depth -type d -name ".pytest_cache" -exec rm -r "{}" +
18 }
19 
20 # Add the user installed site-packages paths to PYTHONPATH, only if the
21 #   directory exists. Also preserve the current PYTHONPATH value.
22 # Feel free to autorun this when .zshrc loads.
23 function pyuserpaths() {
24-    local targets=("python2" "python3")  # bins
25-    
26-    # Get existing interpreters.
27-    local interps=()
28-    for target in $targets; do
29-        [ `command -v $target` ] && interps+=($target)
30-    done
31-
32-    # Check for a non-standard install directory.
33-    local user_base="${HOME}/.local"
34-    [ $PYTHONUSERBASE ] && user_base=$PYTHONUSERBASE
35-
36-    # Add version specific paths, if:
37-    #   it exists in the filesystem;
38-    #   it isn't in PYTHONPATH already.
39-    for interp in $interps; do
40-        # Get minor release version.
41-        local ver=`$interp -V 2>&1`
42-        ver=`echo ${ver:7} | cut -d '.' -f 1,2`  # The patch version is variable length, truncate it.
43-        
44-        local site_pkgs="${user_base}/lib/python${ver}/site-packages"
45-        [[ -d $site_pkgs && ! $PYTHONPATH =~ $site_pkgs ]] && export PYTHONPATH=${site_pkgs}:$PYTHONPATH
46-    done
47+  setopt localoptions extendedglob
48+
49+  # Check for a non-standard install directory.
50+  local user_base="${PYTHONUSERBASE:-"${HOME}/.local"}"
51+
52+  local python version site_pkgs
53+  for python in python2 python3; do
54+    # Check if command exists
55+    (( ${+commands[$python]} )) || continue
56+
57+    # Get minor release version.
58+    # The patch version is variable length, truncate it.
59+    version=${(M)${"$($python -V 2>&1)":7}#[^.]##.[^.]##}
60+
61+    # Add version specific path, if:
62+    # - it exists in the filesystem
63+    # - it isn't in $PYTHONPATH already.
64+    site_pkgs="${user_base}/lib/python${version}/site-packages"
65+    [[ -d "$site_pkgs" && ! "$PYTHONPATH" =~ (^|:)"$site_pkgs"(:|$) ]] || continue
66+    export PYTHONPATH="${site_pkgs}${PYTHONPATH+":${PYTHONPATH}"}"
67+  done
68 }
69 
70 # Grep among .py files