From 933c856a6bfb809dba112811c37e286034498c85 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 16 Sep 2014 14:30:15 +0200 Subject: [PATCH] Add pydoc-serve to show local python documentation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With aliases pydoc2-serve pydoc3-serve. Will try multiple possible locations, since not all distributions use the same naming scheme. Depends on the doc-plugin method in serve, but the user doesn’t have to know how exactly his systems python doc folder is named. --- plugins/python/python.plugin.zsh | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/plugins/python/python.plugin.zsh b/plugins/python/python.plugin.zsh index a10c06fd3..5d4da8df2 100644 --- a/plugins/python/python.plugin.zsh +++ b/plugins/python/python.plugin.zsh @@ -9,6 +9,47 @@ function pyclean() { find ${ZSH_PYCLEAN_PLACES} -type d -name "__pycache__" -delete } +function pydoc-serve() { +# Serves offline python documentation +# Args: +# python_doc_version: 2 or 3 (will fall back to other if necessary) + which serve-doc >/dev/null + if [ $? -ne 0 ]; then + echo "This function (pydoc-serve) depends on the oh-my-zsh serve plugin." >&2 + return + fi + + local version=$1 + + # python2 prints --version to stderr, python3 to stdout + local iv="$(python --version 2>&1 | cut -d' ' -f2 | tr '.' ' ')" + [ $version -ne ${iv[1]} ] \ + && iv="$(python$version --version 2>&1 | cut -d' ' -f2 | tr '.' ' ')" + # Try to make this local … bash/zsh are a bad joke. + installed_version=(`echo $iv`) + local v1=${installed_version[1]} + local v2=${installed_version[2]} + local v3=${installed_version[3]} + + local done=-1 + for suffix in "" "docs"; do + if [ $done -ne 0 ]; then + serve-doc "python$v1.$v2.$v3" || \ + serve-doc "python$v1.$v2" || \ + serve-doc "python$v1" || \ + serve-doc "python" + done=$? + fi + done +} + +function pydoc2-serve() { + pydoc-serve 2 +} +function pydoc3-serve() { + pydoc-serve 3 +} + # Grep among .py files alias pygrep='grep --include="*.py"'