Skip to content

Commit

Permalink
Merge pull request #2016 from gaelicWizard/plugin/python
Browse files Browse the repository at this point in the history
plugin/python: OS detection & `shfmt`
  • Loading branch information
NoahGorny committed Jan 7, 2022
2 parents 2728f20 + 139baed commit 931f27e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
1 change: 1 addition & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ plugins/available/osx.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
Expand Down
45 changes: 23 additions & 22 deletions plugins/available/python.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
cite about-plugin
# shellcheck shell=bash
about-plugin 'alias "shttp" to SimpleHTTPServer'

if [[ "$OSTYPE" == 'linux'* ]]
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 http.server'
else
alias shttp='python -m SimpleHTTPServer'
_log_warning "Unable to load 'plugin/python' due to being unable to find a working 'python'"
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=`dirname $xpyc`;
echo "$EDITOR $xpydir";
$EDITOR "$xpydir";
else
echo "$EDITOR ${xpyc%.*}.py";
$EDITOR "${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
}

0 comments on commit 931f27e

Please sign in to comment.