Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/argcomplete/shell_integration.py: 18%

38 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:31 +0000

1# Copyright 2012-2023, Andrey Kislyuk and argcomplete contributors. Licensed under the terms of the 

2# `Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>`_. Distribution of the LICENSE and NOTICE 

3# files with source copies of this package and derivative works is **REQUIRED** as specified by the Apache License. 

4# See https://github.com/kislyuk/argcomplete for more info. 

5 

6from shlex import quote 

7 

8bashcode = 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 if [[ -n "${ZSH_VERSION-}" ]]; then 

36 local completions 

37 completions=($(IFS="$IFS" \ 

38 COMP_LINE="$BUFFER" \ 

39 COMP_POINT="$CURSOR" \ 

40 _ARGCOMPLETE=1 \ 

41 _ARGCOMPLETE_SHELL="zsh" \ 

42 _ARGCOMPLETE_SUPPRESS_SPACE=1 \ 

43 __python_argcomplete_run "${words[1]}") ) 

44 _describe "${words[1]}" completions -o nosort 

45 else 

46 local SUPPRESS_SPACE=0 

47 if compopt +o nospace 2> /dev/null; then 

48 SUPPRESS_SPACE=1 

49 fi 

50 COMPREPLY=($(IFS="$IFS" \ 

51 COMP_LINE="$COMP_LINE" \ 

52 COMP_POINT="$COMP_POINT" \ 

53 COMP_TYPE="$COMP_TYPE" \ 

54 _ARGCOMPLETE_COMP_WORDBREAKS="$COMP_WORDBREAKS" \ 

55 _ARGCOMPLETE=1 \ 

56 _ARGCOMPLETE_SHELL="bash" \ 

57 _ARGCOMPLETE_SUPPRESS_SPACE=$SUPPRESS_SPACE \ 

58 __python_argcomplete_run "%(argcomplete_script)s")) 

59 if [[ $? != 0 ]]; then 

60 unset COMPREPLY 

61 elif [[ $SUPPRESS_SPACE == 1 ]] && [[ "${COMPREPLY-}" =~ [=/:]$ ]]; then 

62 compopt -o nospace 

63 fi 

64 fi 

65} 

66if [[ -z "${ZSH_VERSION-}" ]]; then 

67 complete %(complete_opts)s -F _python_argcomplete%(function_suffix)s %(executables)s 

68else 

69 compdef _python_argcomplete%(function_suffix)s %(executables)s 

70fi 

71""" 

72 

73tcshcode = """\ 

74complete "%(executable)s" 'p@*@`python-argcomplete-tcsh "%(argcomplete_script)s"`@' ; 

75""" 

76 

77fishcode = r""" 

78function __fish_%(function_name)s_complete 

79 set -x _ARGCOMPLETE 1 

80 set -x _ARGCOMPLETE_DFS \t 

81 set -x _ARGCOMPLETE_IFS \n 

82 set -x _ARGCOMPLETE_SUPPRESS_SPACE 1 

83 set -x _ARGCOMPLETE_SHELL fish 

84 set -x COMP_LINE (commandline -p) 

85 set -x COMP_POINT (string length (commandline -cp)) 

86 set -x COMP_TYPE 

87 if set -q _ARC_DEBUG 

88 %(argcomplete_script)s 8>&1 9>&2 1>&9 2>&1 

89 else 

90 %(argcomplete_script)s 8>&1 9>&2 1>/dev/null 2>&1 

91 end 

92end 

93complete %(completion_arg)s %(executable)s -f -a '(__fish_%(function_name)s_complete)' 

94""" 

95 

96powershell_code = r""" 

97Register-ArgumentCompleter -Native -CommandName %(executable)s -ScriptBlock { 

98 param($commandName, $wordToComplete, $cursorPosition) 

99 $completion_file = New-TemporaryFile 

100 $env:ARGCOMPLETE_USE_TEMPFILES = 1 

101 $env:_ARGCOMPLETE_STDOUT_FILENAME = $completion_file 

102 $env:COMP_LINE = $wordToComplete 

103 $env:COMP_POINT = $cursorPosition 

104 $env:_ARGCOMPLETE = 1 

105 $env:_ARGCOMPLETE_SUPPRESS_SPACE = 0 

106 $env:_ARGCOMPLETE_IFS = "`n" 

107 $env:_ARGCOMPLETE_SHELL = "powershell" 

108 %(argcomplete_script)s 2>&1 | Out-Null 

109 

110 Get-Content $completion_file | ForEach-Object { 

111 [System.Management.Automation.CompletionResult]::new($_, $_, "ParameterValue", $_) 

112 } 

113 Remove-Item $completion_file, Env:\_ARGCOMPLETE_STDOUT_FILENAME, Env:\ARGCOMPLETE_USE_TEMPFILES, Env:\COMP_LINE, Env:\COMP_POINT, Env:\_ARGCOMPLETE, Env:\_ARGCOMPLETE_SUPPRESS_SPACE, Env:\_ARGCOMPLETE_IFS, Env:\_ARGCOMPLETE_SHELL 

114} 

115""" # noqa: E501 

116 

117shell_codes = {"bash": bashcode, "tcsh": tcshcode, "fish": fishcode, "powershell": powershell_code} 

118 

119 

120def shellcode(executables, use_defaults=True, shell="bash", complete_arguments=None, argcomplete_script=None): 

121 """ 

122 Provide the shell code required to register a python executable for use with the argcomplete module. 

123 

124 :param list(str) executables: Executables to be completed (when invoked exactly with this name) 

125 :param bool use_defaults: Whether to fallback to readline's default completion when no matches are generated 

126 (affects bash only) 

127 :param str shell: Name of the shell to output code for 

128 :param complete_arguments: Arguments to call complete with (affects bash only) 

129 :type complete_arguments: list(str) or None 

130 :param argcomplete_script: Script to call complete with, if not the executable to complete. 

131 If supplied, will be used to complete *all* passed executables. 

132 :type argcomplete_script: str or None 

133 """ 

134 

135 if complete_arguments is None: 

136 complete_options = "-o nospace -o default -o bashdefault" if use_defaults else "-o nospace -o bashdefault" 

137 else: 

138 complete_options = " ".join(complete_arguments) 

139 

140 if shell == "bash" or shell == "zsh": 

141 quoted_executables = [quote(i) for i in executables] 

142 executables_list = " ".join(quoted_executables) 

143 script = argcomplete_script 

144 if script: 

145 function_suffix = "_" + script 

146 else: 

147 script = "$1" 

148 function_suffix = "" 

149 code = bashcode % dict( 

150 complete_opts=complete_options, 

151 executables=executables_list, 

152 argcomplete_script=script, 

153 function_suffix=function_suffix, 

154 ) 

155 elif shell == "fish": 

156 code = "" 

157 for executable in executables: 

158 script = argcomplete_script or executable 

159 completion_arg = "--path" if "/" in executable else "--command" # use path for absolute paths 

160 function_name = executable.replace("/", "_") # / not allowed in function name 

161 

162 code += fishcode % dict( 

163 executable=executable, 

164 argcomplete_script=script, 

165 completion_arg=completion_arg, 

166 function_name=function_name, 

167 ) 

168 elif shell == "powershell": 

169 code = "" 

170 for executable in executables: 

171 script = argcomplete_script or executable 

172 code += powershell_code % dict(executable=executable, argcomplete_script=script) 

173 

174 else: 

175 code = "" 

176 for executable in executables: 

177 script = argcomplete_script 

178 # If no script was specified, default to the executable being completed. 

179 if not script: 

180 script = executable 

181 code += shell_codes.get(shell, "") % dict(executable=executable, argcomplete_script=script) 

182 

183 return code