Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
pip._internal.cli.autocompletion Namespace Reference

Functions

None autocomplete ()
 
Optional[str] get_path_completion_type (List[str] cwords, int cword, Iterable[Any] opts)
 
Iterable[str] auto_complete_paths (str current, str completion_type)
 

Detailed Description

Logic that powers autocompletion installed by ``pip completion``.

Function Documentation

◆ auto_complete_paths()

Iterable[str] auto_complete_paths ( str  current,
str  completion_type 
)
If ``completion_type`` is ``file`` or ``path``, list all regular files
and directories starting with ``current``; otherwise only list directories
starting with ``current``.

:param current: The word to be completed
:param completion_type: path completion type(``file``, ``path`` or ``dir``)
:return: A generator of regular files and/or directories

Definition at line 143 of file autocompletion.py.

143def auto_complete_paths(current: str, completion_type: str) -> Iterable[str]:
144 """If ``completion_type`` is ``file`` or ``path``, list all regular files
145 and directories starting with ``current``; otherwise only list directories
146 starting with ``current``.
147
148 :param current: The word to be completed
149 :param completion_type: path completion type(``file``, ``path`` or ``dir``)
150 :return: A generator of regular files and/or directories
151 """
152 directory, filename = os.path.split(current)
153 current_path = os.path.abspath(directory)
154 # Don't complete paths if they can't be accessed
155 if not os.access(current_path, os.R_OK):
156 return
157 filename = os.path.normcase(filename)
158 # list all files that start with ``filename``
159 file_list = (
160 x for x in os.listdir(current_path) if os.path.normcase(x).startswith(filename)
161 )
162 for f in file_list:
163 opt = os.path.join(current_path, f)
164 comp_file = os.path.normcase(os.path.join(directory, f))
165 # complete regular files when there is not ``<dir>`` after option
166 # complete directories when there is ``<file>``, ``<path>`` or
167 # ``<dir>``after option
168 if completion_type != "dir" and os.path.isfile(opt):
169 yield comp_file
170 elif os.path.isdir(opt):
171 yield os.path.join(comp_file, "")
for i

References i.

Referenced by pip._internal.cli.autocompletion.autocomplete().

Here is the caller graph for this function:

◆ autocomplete()

None autocomplete ( )
Entry Point for completion of main and subcommand options.

Definition at line 15 of file autocompletion.py.

15def autocomplete() -> None:
16 """Entry Point for completion of main and subcommand options."""
17 # Don't complete if user hasn't sourced bash_completion file.
18 if "PIP_AUTO_COMPLETE" not in os.environ:
19 return
20 cwords = os.environ["COMP_WORDS"].split()[1:]
21 cword = int(os.environ["COMP_CWORD"])
22 try:
23 current = cwords[cword - 1]
24 except IndexError:
25 current = ""
26
27 parser = create_main_parser()
28 subcommands = list(commands_dict)
29 options = []
30
31 # subcommand
32 subcommand_name: Optional[str] = None
33 for word in cwords:
34 if word in subcommands:
35 subcommand_name = word
36 break
37 # subcommand options
38 if subcommand_name is not None:
39 # special case: 'help' subcommand has no options
40 if subcommand_name == "help":
41 sys.exit(1)
42 # special case: list locally installed dists for show and uninstall
43 should_list_installed = not current.startswith("-") and subcommand_name in [
44 "show",
45 "uninstall",
46 ]
47 if should_list_installed:
48 env = get_default_environment()
49 lc = current.lower()
50 installed = [
52 for dist in env.iter_installed_distributions(local_only=True)
54 and dist.canonical_name not in cwords[1:]
55 ]
56 # if there are no dists installed, fall back to option completion
57 if installed:
58 for dist in installed:
59 print(dist)
60 sys.exit(1)
61
62 should_list_installables = (
63 not current.startswith("-") and subcommand_name == "install"
64 )
65 if should_list_installables:
66 for path in auto_complete_paths(current, "path"):
67 print(path)
68 sys.exit(1)
69
70 subcommand = create_command(subcommand_name)
71
74 for opt_str in opt._long_opts + opt._short_opts:
75 options.append((opt_str, opt.nargs))
76
77 # filter out previously specified options from available options
78 prev_opts = [x.split("=")[0] for x in cwords[1 : cword - 1]]
79 options = [(x, v) for (x, v) in options if x not in prev_opts]
80 # filter options by current input
81 options = [(k, v) for k, v in options if k.startswith(current)]
82 # get completion type given cwords and available subcommand options
83 completion_type = get_path_completion_type(
84 cwords,
85 cword,
87 )
88 # get completion files and directories if ``completion_type`` is
89 # ``<file>``, ``<dir>`` or ``<path>``
90 if completion_type:
91 paths = auto_complete_paths(current, completion_type)
92 options = [(path, 0) for path in paths]
93 for option in options:
94 opt_label = option[0]
95 # append '=' to options which require args
96 if option[1] and option[0][:2] == "--":
97 opt_label += "="
98 print(opt_label)
99 else:
100 # show main parser options only when necessary
101
102 opts = [i.option_list for i in parser.option_groups]
104 flattened_opts = chain.from_iterable(opts)
105 if current.startswith("-"):
106 for opt in flattened_opts:
108 subcommands += opt._long_opts + opt._short_opts
109 else:
110 # get completion type given cwords and all available options
111 completion_type = get_path_completion_type(cwords, cword, flattened_opts)
112 if completion_type:
113 subcommands = list(auto_complete_paths(current, completion_type))
114
115 print(" ".join([x for x in subcommands if x.startswith(current)]))
116 sys.exit(1)
117
118

References pip._internal.cli.autocompletion.auto_complete_paths(), pip._internal.cli.autocompletion.get_path_completion_type(), and i.

Here is the call graph for this function:

◆ get_path_completion_type()

Optional[str] get_path_completion_type ( List[str]  cwords,
int  cword,
Iterable[Any]   opts 
)
Get the type of path completion (``file``, ``dir``, ``path`` or None)

:param cwords: same as the environmental variable ``COMP_WORDS``
:param cword: same as the environmental variable ``COMP_CWORD``
:param opts: The available options to check
:return: path completion type (``file``, ``dir``, ``path`` or None)

Definition at line 119 of file autocompletion.py.

121) -> Optional[str]:
122 """Get the type of path completion (``file``, ``dir``, ``path`` or None)
123
124 :param cwords: same as the environmental variable ``COMP_WORDS``
125 :param cword: same as the environmental variable ``COMP_CWORD``
126 :param opts: The available options to check
127 :return: path completion type (``file``, ``dir``, ``path`` or None)
128 """
129 if cword < 2 or not cwords[cword - 2].startswith("-"):
130 return None
131 for opt in opts:
133 continue
134 for o in str(opt).split("/"):
135 if cwords[cword - 2].split("=")[0] == o:
136 if not opt.metavar or any(
137 x in ("path", "file", "dir") for x in opt.metavar.split("/")
138 ):
139 return opt.metavar
140 return None
141
142

References i.

Referenced by pip._internal.cli.autocompletion.autocomplete().

Here is the caller graph for this function: