From dd911f12561dc3b20dd6e4a5e93d2670c4ae75da Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 12 Aug 2021 12:14:31 -0700 Subject: [PATCH 1/3] plugins/python: code style improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use shell functionality to avoid invoking external binaries, and quote some stuff. Alsö, use $EDITOR and related variables in order to fall through if some aren't defined. --- plugins/available/python.plugin.bash | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/plugins/available/python.plugin.bash b/plugins/available/python.plugin.bash index 77fa7dd6..4add174f 100644 --- a/plugins/available/python.plugin.bash +++ b/plugins/available/python.plugin.bash @@ -1,11 +1,12 @@ cite about-plugin about-plugin 'alias "shttp" to SimpleHTTPServer' -if [[ "$OSTYPE" == 'linux'* ]] -then - alias shttp='python2 -m SimpleHTTPServer' +if _command_exists python2; then + alias shttp='python2 -m SimpleHTTPServer' +elif _command_exists python + alias shttp='python -m SimpleHTTPServer' else - alias shttp='python -m SimpleHTTPServer' + return 1 fi function pyedit() { @@ -14,18 +15,18 @@ function pyedit() { example '$ pyedit requests' group 'python' - xpyc=`python -c "import os, sys; f = open(os.devnull, 'w'); sys.stderr = f; module = __import__('$1'); sys.stdout.write(module.__file__)"` + xpyc="$(python -c "import os, sys; f = open(os.devnull, 'w'); sys.stderr = f; module = __import__('$1'); sys.stdout.write(module.__file__)")" if [[ "$xpyc" == "" ]]; then echo "Python module $1 not found" return -1 - elif [[ $xpyc == *__init__.py* ]]; then - xpydir=`dirname $xpyc`; + elif [[ "$xpyc" == *__init__.py* ]]; then + xpydir="${xpyc%/*}"; echo "$EDITOR $xpydir"; - $EDITOR "$xpydir"; + ${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}} "$xpydir"; else echo "$EDITOR ${xpyc%.*}.py"; - $EDITOR "${xpyc%.*}.py"; + ${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}} "${xpyc%.*}.py"; fi } From 29216c0fd4f34795cf2b3ca6c44042bf87e3fd50 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 1 Jan 2022 22:59:32 -0800 Subject: [PATCH 2/3] plugin/python: `shfmt` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit My apologies to future `git blame` hunters ♥ --- clean_files.txt | 1 + plugins/available/python.plugin.bash | 37 ++++++++++++++-------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 8c8b3fed..4f17223c 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -106,6 +106,7 @@ plugins/available/nodenv.plugin.bash plugins/available/percol.plugin.bash plugins/available/plenv.plugin.bash plugins/available/pyenv.plugin.bash +plugins/available/python.plugin.bash plugins/available/rbenv.plugin.bash plugins/available/ruby.plugin.bash plugins/available/textmate.plugin.bash diff --git a/plugins/available/python.plugin.bash b/plugins/available/python.plugin.bash index 4add174f..d9581e5c 100644 --- a/plugins/available/python.plugin.bash +++ b/plugins/available/python.plugin.bash @@ -1,32 +1,31 @@ -cite about-plugin +# shellcheck shell=bash about-plugin 'alias "shttp" to SimpleHTTPServer' if _command_exists python2; then alias shttp='python2 -m SimpleHTTPServer' -elif _command_exists python +elif _command_exists python; then alias shttp='python -m SimpleHTTPServer' else return 1 fi function pyedit() { - about 'opens python module in your EDITOR' - param '1: python module to open' - example '$ pyedit requests' - group 'python' + about 'opens python module in your EDITOR' + param '1: python module to open' + example '$ pyedit requests' + group 'python' - xpyc="$(python -c "import os, sys; f = open(os.devnull, 'w'); sys.stderr = f; module = __import__('$1'); sys.stdout.write(module.__file__)")" + xpyc="$(python -c "import os, sys; f = open(os.devnull, 'w'); sys.stderr = f; module = __import__('$1'); sys.stdout.write(module.__file__)")" - if [[ "$xpyc" == "" ]]; then - echo "Python module $1 not found" - return -1 - - elif [[ "$xpyc" == *__init__.py* ]]; then - xpydir="${xpyc%/*}"; - echo "$EDITOR $xpydir"; - ${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}} "$xpydir"; - else - echo "$EDITOR ${xpyc%.*}.py"; - ${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}} "${xpyc%.*}.py"; - fi + if [[ "$xpyc" == "" ]]; then + echo "Python module $1 not found" + return 1 + elif [[ "$xpyc" == *__init__.py* ]]; then + xpydir="${xpyc%/*}" + echo "$EDITOR $xpydir" + ${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}} "$xpydir" + else + echo "$EDITOR ${xpyc%.*}.py" + ${VISUAL:-${EDITOR:-${ALTERNATE_EDITOR:-nano}}} "${xpyc%.*}.py" + fi } From 139baedf5d578719867a5cef252dd6e1edeffda7 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Mon, 3 Jan 2022 17:47:33 -0800 Subject: [PATCH 3/3] plugin/python: Pyton 2 is dead; Long Live Python 3! --- plugins/available/python.plugin.bash | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/available/python.plugin.bash b/plugins/available/python.plugin.bash index d9581e5c..bd644e8b 100644 --- a/plugins/available/python.plugin.bash +++ b/plugins/available/python.plugin.bash @@ -1,11 +1,12 @@ # shellcheck shell=bash about-plugin 'alias "shttp" to SimpleHTTPServer' -if _command_exists python2; then - alias shttp='python2 -m SimpleHTTPServer' +if _command_exists python3; then + alias shttp='python3 -m http.server' elif _command_exists python; then - alias shttp='python -m SimpleHTTPServer' + alias shttp='python -m http.server' else + _log_warning "Unable to load 'plugin/python' due to being unable to find a working 'python'" return 1 fi