comparison env/lib/python3.9/site-packages/argcomplete/shell_integration.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 #!/usr/bin/env python
2
3 try:
4 from shlex import quote
5 except ImportError:
6 from pipes import quote
7
8 bashcode = r'''
9 # Run something, muting output or redirecting it to the debug stream
10 # depending on the value of _ARC_DEBUG.
11 # If ARGCOMPLETE_USE_TEMPFILES is set, use tempfiles for IPC.
12 __python_argcomplete_run() {
13 if [[ -z "${ARGCOMPLETE_USE_TEMPFILES-}" ]]; then
14 __python_argcomplete_run_inner "$@"
15 return
16 fi
17 local tmpfile="$(mktemp)"
18 _ARGCOMPLETE_STDOUT_FILENAME="$tmpfile" __python_argcomplete_run_inner "$@"
19 local code=$?
20 cat "$tmpfile"
21 rm "$tmpfile"
22 return $code
23 }
24
25 __python_argcomplete_run_inner() {
26 if [[ -z "${_ARC_DEBUG-}" ]]; then
27 "$@" 8>&1 9>&2 1>/dev/null 2>&1
28 else
29 "$@" 8>&1 9>&2 1>&9 2>&1
30 fi
31 }
32
33 _python_argcomplete%(function_suffix)s() {
34 local IFS=$'\013'
35 local SUPPRESS_SPACE=0
36 if compopt +o nospace 2> /dev/null; then
37 SUPPRESS_SPACE=1
38 fi
39 COMPREPLY=( $(IFS="$IFS" \
40 COMP_LINE="$COMP_LINE" \
41 COMP_POINT="$COMP_POINT" \
42 COMP_TYPE="$COMP_TYPE" \
43 _ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" \
44 _ARGCOMPLETE=1 \
45 _ARGCOMPLETE_SUPPRESS_SPACE=$SUPPRESS_SPACE \
46 __python_argcomplete_run "%(argcomplete_script)s") )
47 if [[ $? != 0 ]]; then
48 unset COMPREPLY
49 elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then
50 compopt -o nospace
51 fi
52 }
53 complete %(complete_opts)s -F _python_argcomplete%(function_suffix)s %(executables)s
54 '''
55
56 tcshcode = '''\
57 complete "%(executable)s" 'p@*@`python-argcomplete-tcsh "%(argcomplete_script)s"`@' ;
58 '''
59
60 fishcode = r'''
61 function __fish_%(executable)s_complete
62 set -x _ARGCOMPLETE 1
63 set -x _ARGCOMPLETE_DFS \t
64 set -x _ARGCOMPLETE_IFS \n
65 set -x _ARGCOMPLETE_SUPPRESS_SPACE 1
66 set -x _ARGCOMPLETE_SHELL fish
67 set -x COMP_LINE (commandline -p)
68 set -x COMP_POINT (string length (commandline -cp))
69 set -x COMP_TYPE
70 if set -q _ARC_DEBUG
71 %(argcomplete_script)s 8>&1 9>&2 1>/dev/null 2>&1
72 else
73 %(argcomplete_script)s 8>&1 9>&2 1>&9 2>&1
74 end
75 end
76 complete -c %(executable)s -f -a '(__fish_%(executable)s_complete)'
77 '''
78
79 shell_codes = {'bash': bashcode, 'tcsh': tcshcode, 'fish': fishcode}
80
81
82 def shellcode(executables, use_defaults=True, shell='bash', complete_arguments=None, argcomplete_script=None):
83 '''
84 Provide the shell code required to register a python executable for use with the argcomplete module.
85
86 :param list(str) executables: Executables to be completed (when invoked exactly with this name)
87 :param bool use_defaults: Whether to fallback to readline's default completion when no matches are generated.
88 :param str shell: Name of the shell to output code for (bash or tcsh)
89 :param complete_arguments: Arguments to call complete with
90 :type complete_arguments: list(str) or None
91 :param argcomplete_script: Script to call complete with, if not the executable to complete.
92 If supplied, will be used to complete *all* passed executables.
93 :type argcomplete_script: str or None
94 '''
95
96 if complete_arguments is None:
97 complete_options = '-o nospace -o default -o bashdefault' if use_defaults else '-o nospace -o bashdefault'
98 else:
99 complete_options = " ".join(complete_arguments)
100
101 if shell == 'bash':
102 quoted_executables = [quote(i) for i in executables]
103 executables_list = " ".join(quoted_executables)
104 script = argcomplete_script
105 if script:
106 function_suffix = '_' + script
107 else:
108 script = '$1'
109 function_suffix = ''
110 code = bashcode % dict(complete_opts=complete_options, executables=executables_list,
111 argcomplete_script=script, function_suffix=function_suffix)
112 else:
113 code = ""
114 for executable in executables:
115 script = argcomplete_script
116 # If no script was specified, default to the executable being completed.
117 if not script:
118 script = executable
119 code += shell_codes.get(shell, '') % dict(executable=executable, argcomplete_script=script)
120
121 return code