Mercurial > repos > guerler > hhblits
comparison lib/python3.8/site-packages/pip/_internal/cli/autocompletion.py @ 0:9e54283cc701 draft
"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
| author | guerler |
|---|---|
| date | Mon, 27 Jul 2020 03:47:31 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:9e54283cc701 |
|---|---|
| 1 """Logic that powers autocompletion installed by ``pip completion``. | |
| 2 """ | |
| 3 | |
| 4 import optparse | |
| 5 import os | |
| 6 import sys | |
| 7 from itertools import chain | |
| 8 | |
| 9 from pip._internal.cli.main_parser import create_main_parser | |
| 10 from pip._internal.commands import commands_dict, create_command | |
| 11 from pip._internal.utils.misc import get_installed_distributions | |
| 12 from pip._internal.utils.typing import MYPY_CHECK_RUNNING | |
| 13 | |
| 14 if MYPY_CHECK_RUNNING: | |
| 15 from typing import Any, Iterable, List, Optional | |
| 16 | |
| 17 | |
| 18 def autocomplete(): | |
| 19 # type: () -> None | |
| 20 """Entry Point for completion of main and subcommand options. | |
| 21 """ | |
| 22 # Don't complete if user hasn't sourced bash_completion file. | |
| 23 if 'PIP_AUTO_COMPLETE' not in os.environ: | |
| 24 return | |
| 25 cwords = os.environ['COMP_WORDS'].split()[1:] | |
| 26 cword = int(os.environ['COMP_CWORD']) | |
| 27 try: | |
| 28 current = cwords[cword - 1] | |
| 29 except IndexError: | |
| 30 current = '' | |
| 31 | |
| 32 parser = create_main_parser() | |
| 33 subcommands = list(commands_dict) | |
| 34 options = [] | |
| 35 | |
| 36 # subcommand | |
| 37 subcommand_name = None # type: Optional[str] | |
| 38 for word in cwords: | |
| 39 if word in subcommands: | |
| 40 subcommand_name = word | |
| 41 break | |
| 42 # subcommand options | |
| 43 if subcommand_name is not None: | |
| 44 # special case: 'help' subcommand has no options | |
| 45 if subcommand_name == 'help': | |
| 46 sys.exit(1) | |
| 47 # special case: list locally installed dists for show and uninstall | |
| 48 should_list_installed = ( | |
| 49 subcommand_name in ['show', 'uninstall'] and | |
| 50 not current.startswith('-') | |
| 51 ) | |
| 52 if should_list_installed: | |
| 53 installed = [] | |
| 54 lc = current.lower() | |
| 55 for dist in get_installed_distributions(local_only=True): | |
| 56 if dist.key.startswith(lc) and dist.key not in cwords[1:]: | |
| 57 installed.append(dist.key) | |
| 58 # if there are no dists installed, fall back to option completion | |
| 59 if installed: | |
| 60 for dist in installed: | |
| 61 print(dist) | |
| 62 sys.exit(1) | |
| 63 | |
| 64 subcommand = create_command(subcommand_name) | |
| 65 | |
| 66 for opt in subcommand.parser.option_list_all: | |
| 67 if opt.help != optparse.SUPPRESS_HELP: | |
| 68 for opt_str in opt._long_opts + opt._short_opts: | |
| 69 options.append((opt_str, opt.nargs)) | |
| 70 | |
| 71 # filter out previously specified options from available options | |
| 72 prev_opts = [x.split('=')[0] for x in cwords[1:cword - 1]] | |
| 73 options = [(x, v) for (x, v) in options if x not in prev_opts] | |
| 74 # filter options by current input | |
| 75 options = [(k, v) for k, v in options if k.startswith(current)] | |
| 76 # get completion type given cwords and available subcommand options | |
| 77 completion_type = get_path_completion_type( | |
| 78 cwords, cword, subcommand.parser.option_list_all, | |
| 79 ) | |
| 80 # get completion files and directories if ``completion_type`` is | |
| 81 # ``<file>``, ``<dir>`` or ``<path>`` | |
| 82 if completion_type: | |
| 83 paths = auto_complete_paths(current, completion_type) | |
| 84 options = [(path, 0) for path in paths] | |
| 85 for option in options: | |
| 86 opt_label = option[0] | |
| 87 # append '=' to options which require args | |
| 88 if option[1] and option[0][:2] == "--": | |
| 89 opt_label += '=' | |
| 90 print(opt_label) | |
| 91 else: | |
| 92 # show main parser options only when necessary | |
| 93 | |
| 94 opts = [i.option_list for i in parser.option_groups] | |
| 95 opts.append(parser.option_list) | |
| 96 flattened_opts = chain.from_iterable(opts) | |
| 97 if current.startswith('-'): | |
| 98 for opt in flattened_opts: | |
| 99 if opt.help != optparse.SUPPRESS_HELP: | |
| 100 subcommands += opt._long_opts + opt._short_opts | |
| 101 else: | |
| 102 # get completion type given cwords and all available options | |
| 103 completion_type = get_path_completion_type(cwords, cword, | |
| 104 flattened_opts) | |
| 105 if completion_type: | |
| 106 subcommands = list(auto_complete_paths(current, | |
| 107 completion_type)) | |
| 108 | |
| 109 print(' '.join([x for x in subcommands if x.startswith(current)])) | |
| 110 sys.exit(1) | |
| 111 | |
| 112 | |
| 113 def get_path_completion_type(cwords, cword, opts): | |
| 114 # type: (List[str], int, Iterable[Any]) -> Optional[str] | |
| 115 """Get the type of path completion (``file``, ``dir``, ``path`` or None) | |
| 116 | |
| 117 :param cwords: same as the environmental variable ``COMP_WORDS`` | |
| 118 :param cword: same as the environmental variable ``COMP_CWORD`` | |
| 119 :param opts: The available options to check | |
| 120 :return: path completion type (``file``, ``dir``, ``path`` or None) | |
| 121 """ | |
| 122 if cword < 2 or not cwords[cword - 2].startswith('-'): | |
| 123 return None | |
| 124 for opt in opts: | |
| 125 if opt.help == optparse.SUPPRESS_HELP: | |
| 126 continue | |
| 127 for o in str(opt).split('/'): | |
| 128 if cwords[cword - 2].split('=')[0] == o: | |
| 129 if not opt.metavar or any( | |
| 130 x in ('path', 'file', 'dir') | |
| 131 for x in opt.metavar.split('/')): | |
| 132 return opt.metavar | |
| 133 return None | |
| 134 | |
| 135 | |
| 136 def auto_complete_paths(current, completion_type): | |
| 137 # type: (str, str) -> Iterable[str] | |
| 138 """If ``completion_type`` is ``file`` or ``path``, list all regular files | |
| 139 and directories starting with ``current``; otherwise only list directories | |
| 140 starting with ``current``. | |
| 141 | |
| 142 :param current: The word to be completed | |
| 143 :param completion_type: path completion type(`file`, `path` or `dir`)i | |
| 144 :return: A generator of regular files and/or directories | |
| 145 """ | |
| 146 directory, filename = os.path.split(current) | |
| 147 current_path = os.path.abspath(directory) | |
| 148 # Don't complete paths if they can't be accessed | |
| 149 if not os.access(current_path, os.R_OK): | |
| 150 return | |
| 151 filename = os.path.normcase(filename) | |
| 152 # list all files that start with ``filename`` | |
| 153 file_list = (x for x in os.listdir(current_path) | |
| 154 if os.path.normcase(x).startswith(filename)) | |
| 155 for f in file_list: | |
| 156 opt = os.path.join(current_path, f) | |
| 157 comp_file = os.path.normcase(os.path.join(directory, f)) | |
| 158 # complete regular files when there is not ``<dir>`` after option | |
| 159 # complete directories when there is ``<file>``, ``<path>`` or | |
| 160 # ``<dir>``after option | |
| 161 if completion_type != 'dir' and os.path.isfile(opt): | |
| 162 yield comp_file | |
| 163 elif os.path.isdir(opt): | |
| 164 yield os.path.join(comp_file, '') |
