scripts: Refactor py script

Made detection and setup a little more stable, will still have its kinks since
it is rather hastily put together but should work for detecting kernels,
invoking the right python repl and letting you set it manually with `py -c`.
This commit is contained in:
Marty Oehme 2023-12-12 12:23:21 +01:00
parent 029b38f368
commit 4dadcd5f9f
Signed by: Marty
GPG Key ID: EDBF2ED917B2EF6A
1 changed files with 52 additions and 27 deletions

View File

@ -5,39 +5,64 @@ cur_dir=$(pwd)
search_dir="${molten_session_dir}${cur_dir}"
if [ "$1" == "-c" ]; then
cmd=$(echo -e "euporie-console\njupyter-console\nptipython\nipython\npython" | fzf)
fi
# set normal command first
if [ -z "$cmd" ]; then
if exist ptipython; then
cmd=ptipython
elif exist ipython; then
cmd=ipython
elif exist python; then
cmd=python
else
echo "No python found on system, please install." 1>&2
exit 1
fi
cmd_explicit=$(echo -e "euporie-console\njupyter-console\nptipython\nipython\npython" | fzf)
fi
# search for molten session for current dir
if [ -n "$(ls -A "${search_dir}" 2>/dev/null)" ]; then
res=$(find "$search_dir" -maxdepth 1 -type f -name "*.json")
if [ "$(echo "$res" | wc -l)" -gt 1 ]; then
session=$(echo "$res" | fzf --prompt "Molten session> ")
fi
session=$(find "$search_dir" -maxdepth 1 -type f -name "*.json")
if [ "$(echo "$session" | wc -l)" -gt 1 ]; then
session=$(echo "$session" | fzf --prompt "Molten session> ")
fi
fi
if [ -n "$cmd_explicit" ] && not exist "$cmd_explicit"; then
echo "Command $cmd_explicit not found."
exit 1
elif [ "$cmd_explicit" == "euporie-console" ]; then
if [ -n "$session" ]; then
euporie-console --edit-mode vi --connection-file "$session"
else
echo "Starting euporie without a session to attach to."
euporie-console --edit-mode vi
fi
exit 0
elif [ "$cmd_explicit" == "jupyter-console" ]; then
if [ -n "$session" ]; then
jupyter-console --existing "$session"
else
echo "Starting jupyter console without a session to attach to."
jupyter-console
fi
exit 0
elif [ -n "$cmd_explicit" ]; then
"$cmd_explicit"
exit 0
fi
# set normal command first
if [ -z "$cmd" ]; then
if exist ptipython; then
cmd=ptipython
elif exist ipython; then
cmd=ipython
elif exist python; then
cmd=python
else
echo "No python found on system, please install." 1>&2
exit 1
fi
fi
# attach to kernel
if [ -n "${session}" ]; then
if exist euporie-console || [ "$cmd" == "euporie-console" ]; then
euporie-console --edit-mode vi --connection-file "$session"
elif exist jupyter-console || [ "$cmd" == "jupyter-console" ]; then
jupyter-console --existing "$session"
fi
# or just run normal python
else
$cmd
if exist euporie-console; then
euporie-console --edit-mode vi --connection-file "$session"
exit 0
elif exist jupyter-console; then
jupyter-console --existing "$session"
exit 0
fi
fi
# or just run normal python
$cmd