From dd911f12561dc3b20dd6e4a5e93d2670c4ae75da Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 12 Aug 2021 12:14:31 -0700 Subject: [PATCH] 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 }