m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.2 KiB

6 months ago
  1. import sys
  2. import textwrap
  3. from optparse import Values
  4. from typing import List
  5. from pip._internal.cli.base_command import Command
  6. from pip._internal.cli.status_codes import SUCCESS
  7. from pip._internal.utils.misc import get_prog
  8. BASE_COMPLETION = """
  9. # pip {shell} completion start{script}# pip {shell} completion end
  10. """
  11. COMPLETION_SCRIPTS = {
  12. "bash": """
  13. _pip_completion()
  14. {{
  15. COMPREPLY=( $( COMP_WORDS="${{COMP_WORDS[*]}}" \\
  16. COMP_CWORD=$COMP_CWORD \\
  17. PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )
  18. }}
  19. complete -o default -F _pip_completion {prog}
  20. """,
  21. "zsh": """
  22. #compdef -P pip[0-9.]#
  23. __pip() {{
  24. compadd $( COMP_WORDS="$words[*]" \\
  25. COMP_CWORD=$((CURRENT-1)) \\
  26. PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )
  27. }}
  28. if [[ $zsh_eval_context[-1] == loadautofunc ]]; then
  29. # autoload from fpath, call function directly
  30. __pip "$@"
  31. else
  32. # eval/source/. command, register function for later
  33. compdef __pip -P 'pip[0-9.]#'
  34. fi
  35. """,
  36. "fish": """
  37. function __fish_complete_pip
  38. set -lx COMP_WORDS (commandline -o) ""
  39. set -lx COMP_CWORD ( \\
  40. math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
  41. )
  42. set -lx PIP_AUTO_COMPLETE 1
  43. string split \\ -- (eval $COMP_WORDS[1])
  44. end
  45. complete -fa "(__fish_complete_pip)" -c {prog}
  46. """,
  47. "powershell": """
  48. if ((Test-Path Function:\\TabExpansion) -and -not `
  49. (Test-Path Function:\\_pip_completeBackup)) {{
  50. Rename-Item Function:\\TabExpansion _pip_completeBackup
  51. }}
  52. function TabExpansion($line, $lastWord) {{
  53. $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
  54. if ($lastBlock.StartsWith("{prog} ")) {{
  55. $Env:COMP_WORDS=$lastBlock
  56. $Env:COMP_CWORD=$lastBlock.Split().Length - 1
  57. $Env:PIP_AUTO_COMPLETE=1
  58. (& {prog}).Split()
  59. Remove-Item Env:COMP_WORDS
  60. Remove-Item Env:COMP_CWORD
  61. Remove-Item Env:PIP_AUTO_COMPLETE
  62. }}
  63. elseif (Test-Path Function:\\_pip_completeBackup) {{
  64. # Fall back on existing tab expansion
  65. _pip_completeBackup $line $lastWord
  66. }}
  67. }}
  68. """,
  69. }
  70. class CompletionCommand(Command):
  71. """A helper command to be used for command completion."""
  72. ignore_require_venv = True
  73. def add_options(self) -> None:
  74. self.cmd_opts.add_option(
  75. "--bash",
  76. "-b",
  77. action="store_const",
  78. const="bash",
  79. dest="shell",
  80. help="Emit completion code for bash",
  81. )
  82. self.cmd_opts.add_option(
  83. "--zsh",
  84. "-z",
  85. action="store_const",
  86. const="zsh",
  87. dest="shell",
  88. help="Emit completion code for zsh",
  89. )
  90. self.cmd_opts.add_option(
  91. "--fish",
  92. "-f",
  93. action="store_const",
  94. const="fish",
  95. dest="shell",
  96. help="Emit completion code for fish",
  97. )
  98. self.cmd_opts.add_option(
  99. "--powershell",
  100. "-p",
  101. action="store_const",
  102. const="powershell",
  103. dest="shell",
  104. help="Emit completion code for powershell",
  105. )
  106. self.parser.insert_option_group(0, self.cmd_opts)
  107. def run(self, options: Values, args: List[str]) -> int:
  108. """Prints the completion code of the given shell"""
  109. shells = COMPLETION_SCRIPTS.keys()
  110. shell_options = ["--" + shell for shell in sorted(shells)]
  111. if options.shell in shells:
  112. script = textwrap.dedent(
  113. COMPLETION_SCRIPTS.get(options.shell, "").format(prog=get_prog())
  114. )
  115. print(BASE_COMPLETION.format(script=script, shell=options.shell))
  116. return SUCCESS
  117. else:
  118. sys.stderr.write(
  119. "ERROR: You must pass {}\n".format(" or ".join(shell_options))
  120. )
  121. return SUCCESS