Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
ConfigurationCommand Class Reference
Inheritance diagram for ConfigurationCommand:
Collaboration diagram for ConfigurationCommand:

Public Member Functions

None add_options (self)
 
int run (self, Values options, List[str] args)
 
None list_values (self, Values options, List[str] args)
 
None get_name (self, Values options, List[str] args)
 
None set_name_value (self, Values options, List[str] args)
 
None unset_name (self, Values options, List[str] args)
 
None list_config_values (self, Values options, List[str] args)
 
None print_config_file_values (self, Kind variant)
 
None print_env_var_values (self)
 
None open_in_editor (self, Values options, List[str] args)
 
- Public Member Functions inherited from Command
None __init__ (self, str name, str summary, bool isolated=False)
 
None handle_pip_version_check (self, Values options)
 
Tuple[Values, List[str]] parse_args (self, List[str] args)
 
int main (self, List[str] args)
 
- Public Member Functions inherited from CommandContextMixIn
Generator[None, None, Nonemain_context (self)
 
_T enter_context (self, ContextManager[_T] context_provider)
 

Data Fields

 cmd_opts
 
 list_values
 
 open_in_editor
 
 get_name
 
 set_name_value
 
 unset_name
 
 list_config_values
 
 configuration
 
- Data Fields inherited from Command
 name
 
 summary
 
 parser
 
 cmd_opts
 
 tempdir_registry
 
 verbosity
 

Static Public Attributes

bool ignore_require_venv = True
 
str usage
 
- Static Public Attributes inherited from Command
str usage = ""
 
bool ignore_require_venv = False
 

Protected Member Functions

Optional[Kind] _determine_file (self, Values options, bool need_value)
 
Any _get_n_args (self, List[str] args, str example, int n)
 
None _save_configuration (self)
 
str _determine_editor (self, Values options)
 
- Protected Member Functions inherited from Command
int _main (self, List[str] args)
 

Additional Inherited Members

- Protected Attributes inherited from CommandContextMixIn
 _in_main_context
 
 _main_context
 

Detailed Description

Manage local and global configuration.

Subcommands:

- list: List the active configuration (or from the file specified)
- edit: Edit the configuration file in an editor
- get: Get the value associated with command.option
- set: Set the command.option=value
- unset: Unset the value associated with command.option
- debug: List the configuration files and values defined under them

Configuration keys should be dot separated command and option name,
with the special prefix "global" affecting any command. For example,
"pip config set global.index-url https://example.org/" would configure
the index url for all commands, but "pip config set download.timeout 10"
would configure a 10 second timeout only for "pip download" commands.

If none of --user, --global and --site are passed, a virtual
environment configuration file is used if one is active and the file
exists. Otherwise, all modifications happen to the user file by
default.

Definition at line 22 of file configuration.py.

Member Function Documentation

◆ _determine_editor()

str _determine_editor (   self,
Values  options 
)
protected

Definition at line 274 of file configuration.py.

274 def _determine_editor(self, options: Values) -> str:
275 if options.editor is not None:
276 return options.editor
277 elif "VISUAL" in os.environ:
278 return os.environ["VISUAL"]
279 elif "EDITOR" in os.environ:
280 return os.environ["EDITOR"]
281 else:
282 raise PipError("Could not determine editor to use.")
for i

References i.

Referenced by ConfigurationCommand.open_in_editor().

Here is the caller graph for this function:

◆ _determine_file()

Optional[Kind] _determine_file (   self,
Values  options,
bool  need_value 
)
protected

Definition at line 141 of file configuration.py.

141 def _determine_file(self, options: Values, need_value: bool) -> Optional[Kind]:
142 file_options = [
143 key
144 for key, value in (
148 )
149 if value
150 ]
151
152 if not file_options:
153 if not need_value:
154 return None
155 # Default to user, unless there's a site file.
156 elif any(
157 os.path.exists(site_config_file)
158 for site_config_file in get_configuration_files()[kinds.SITE]
159 ):
160 return kinds.SITE
161 else:
162 return kinds.USER
163 elif len(file_options) == 1:
164 return file_options[0]
165
166 raise PipError(
167 "Need exactly one file to operate upon "
168 "(--user, --site, --global) to perform."
169 )
170

References i.

◆ _get_n_args()

Any _get_n_args (   self,
List[str]  args,
str  example,
int  n 
)
protected
Helper to make sure the command got the right number of arguments

Definition at line 249 of file configuration.py.

249 def _get_n_args(self, args: List[str], example: str, n: int) -> Any:
250 """Helper to make sure the command got the right number of arguments"""
251 if len(args) != n:
252 msg = (
253 "Got unexpected number of arguments, expected {}. "
254 '(example: "{} config {}")'
255 ).format(n, get_prog(), example)
256 raise PipError(msg)
257
258 if n == 1:
259 return args[0]
260 else:
261 return args
262

References i.

Referenced by ConfigurationCommand.get_name(), ConfigurationCommand.list_config_values(), ConfigurationCommand.list_values(), ConfigurationCommand.set_name_value(), and ConfigurationCommand.unset_name().

Here is the caller graph for this function:

◆ _save_configuration()

None _save_configuration (   self)
protected

Definition at line 263 of file configuration.py.

263 def _save_configuration(self) -> None:
264 # We successfully ran a modifying command. Need to save the
265 # configuration.
266 try:
267 self.configuration.save()
268 except Exception:
270 "Unable to save configuration. Please report this as a bug."
271 )
272 raise PipError("Internal Error.")
273

References ConfigurationCommand.configuration, and i.

Referenced by ConfigurationCommand.set_name_value(), and ConfigurationCommand.unset_name().

Here is the caller graph for this function:

◆ add_options()

None add_options (   self)

Reimplemented from Command.

Definition at line 58 of file configuration.py.

58 def add_options(self) -> None:
59 self.cmd_opts.add_option(
60 "--editor",
61 dest="editor",
62 action="store",
63 default=None,
64 help=(
65 "Editor to use to edit the file. Uses VISUAL or EDITOR "
66 "environment variables if not provided."
67 ),
68 )
69
70 self.cmd_opts.add_option(
71 "--global",
72 dest="global_file",
73 action="store_true",
74 default=False,
75 help="Use the system-wide configuration file only",
76 )
77
78 self.cmd_opts.add_option(
79 "--user",
80 dest="user_file",
81 action="store_true",
82 default=False,
83 help="Use the user configuration file only",
84 )
85
86 self.cmd_opts.add_option(
87 "--site",
88 dest="site_file",
89 action="store_true",
90 default=False,
91 help="Use the current environment configuration file only",
92 )
93
94 self.parser.insert_option_group(0, self.cmd_opts)
95

References Command.cmd_opts, CacheCommand.cmd_opts, CompletionCommand.cmd_opts, ConfigurationCommand.cmd_opts, DebugCommand.cmd_opts, DownloadCommand.cmd_opts, FreezeCommand.cmd_opts, HashCommand.cmd_opts, IndexCommand.cmd_opts, InspectCommand.cmd_opts, InstallCommand.cmd_opts, ListCommand.cmd_opts, SearchCommand.cmd_opts, ShowCommand.cmd_opts, UninstallCommand.cmd_opts, WheelCommand.cmd_opts, and i.

◆ get_name()

None get_name (   self,
Values  options,
List[str]  args 
)

Definition at line 177 of file configuration.py.

177 def get_name(self, options: Values, args: List[str]) -> None:
178 key = self._get_n_args(args, "get [name]", n=1)
179 value = self.configuration.get_value(key)
180
181 write_output("%s", value)
182

References ConfigurationCommand._get_n_args(), and ConfigurationCommand.configuration.

Here is the call graph for this function:

◆ list_config_values()

None list_config_values (   self,
Values  options,
List[str]  args 
)
List config key-value pairs across different config files

Definition at line 195 of file configuration.py.

195 def list_config_values(self, options: Values, args: List[str]) -> None:
196 """List config key-value pairs across different config files"""
197 self._get_n_args(args, "debug", n=0)
198
199 self.print_env_var_values()
200 # Iterate over config files and print if they exist, and the
201 # key-value pairs present in them if they do
202 for variant, files in sorted(self.configuration.iter_config_files()):
203 write_output("%s:", variant)
204 for fname in files:
205 with indent_log():
206 file_exists = os.path.exists(fname)
207 write_output("%s, exists: %r", fname, file_exists)
208 if file_exists:
209 self.print_config_file_values(variant)
210

References ConfigurationCommand._get_n_args(), ConfigurationCommand.configuration, i, ConfigurationCommand.print_config_file_values(), and ConfigurationCommand.print_env_var_values().

Here is the call graph for this function:

◆ list_values()

None list_values (   self,
Values  options,
List[str]  args 
)

Definition at line 171 of file configuration.py.

171 def list_values(self, options: Values, args: List[str]) -> None:
172 self._get_n_args(args, "list", n=0)
173
174 for key, value in sorted(self.configuration.items()):
175 write_output("%s=%r", key, value)
176

References ConfigurationCommand._get_n_args(), and ConfigurationCommand.configuration.

Here is the call graph for this function:

◆ open_in_editor()

None open_in_editor (   self,
Values  options,
List[str]  args 
)

Definition at line 225 of file configuration.py.

225 def open_in_editor(self, options: Values, args: List[str]) -> None:
226 editor = self._determine_editor(options)
227
228 fname = self.configuration.get_file_to_edit()
229 if fname is None:
230 raise PipError("Could not determine appropriate file.")
231 elif '"' in fname:
232 # This shouldn't happen, unless we see a username like that.
233 # If that happens, we'd appreciate a pull request fixing this.
234 raise PipError(
235 f'Can not open an editor for a file name containing "\n{fname}'
236 )
237
238 try:
239 subprocess.check_call(f'{editor} "{fname}"', shell=True)
240 except FileNotFoundError as e:
241 if not e.filename:
242 e.filename = editor
243 raise
245 raise PipError(
246 "Editor Subprocess exited with exit code {}".format(e.returncode)
247 )
248

References ConfigurationCommand._determine_editor(), ConfigurationCommand.configuration, and i.

Here is the call graph for this function:

◆ print_config_file_values()

None print_config_file_values (   self,
Kind  variant 
)
Get key-value pairs from the file of a variant

Definition at line 211 of file configuration.py.

211 def print_config_file_values(self, variant: Kind) -> None:
212 """Get key-value pairs from the file of a variant"""
213 for name, value in self.configuration.get_values_in_config(variant).items():
214 with indent_log():
215 write_output("%s: %s", name, value)
216

References ConfigurationCommand.configuration.

Referenced by ConfigurationCommand.list_config_values().

Here is the caller graph for this function:

◆ print_env_var_values()

None print_env_var_values (   self)
Get key-values pairs present as environment variables

Definition at line 217 of file configuration.py.

217 def print_env_var_values(self) -> None:
218 """Get key-values pairs present as environment variables"""
219 write_output("%s:", "env_var")
220 with indent_log():
221 for key, value in sorted(self.configuration.get_environ_vars()):
222 env_var = f"PIP_{key.upper()}"
223 write_output("%s=%r", env_var, value)
224

References ConfigurationCommand.configuration.

Referenced by ConfigurationCommand.list_config_values().

Here is the caller graph for this function:

◆ run()

int run (   self,
Values  options,
List[str]  args 
)

Reimplemented from Command.

Definition at line 96 of file configuration.py.

96 def run(self, options: Values, args: List[str]) -> int:
97 handlers = {
98 "list": self.list_values,
99 "edit": self.open_in_editor,
100 "get": self.get_name,
101 "set": self.set_name_value,
102 "unset": self.unset_name,
103 "debug": self.list_config_values,
104 }
105
106 # Determine action
107 if not args or args[0] not in handlers:
109 "Need an action (%s) to perform.",
110 ", ".join(sorted(handlers)),
111 )
112 return ERROR
113
114 action = args[0]
115
116 # Determine which configuration files are to be loaded
117 # Depends on whether the command is modifying.
118 try:
119 load_only = self._determine_file(
120 options, need_value=(action in ["get", "set", "unset", "edit"])
121 )
122 except PipError as e:
124 return ERROR
125
126 # Load a new configuration
127 self.configuration = Configuration(
128 isolated=options.isolated_mode, load_only=load_only
129 )
130 self.configuration.load()
131
132 # Error handling happens here, not in the action-handlers.
133 try:
134 handlers[action](options, args[1:])
135 except PipError as e:
137 return ERROR
138
139 return SUCCESS
140

◆ set_name_value()

None set_name_value (   self,
Values  options,
List[str]  args 
)

Definition at line 183 of file configuration.py.

183 def set_name_value(self, options: Values, args: List[str]) -> None:
184 key, value = self._get_n_args(args, "set [name] [value]", n=2)
185 self.configuration.set_value(key, value)
186
187 self._save_configuration()
188

References ConfigurationCommand._get_n_args(), ConfigurationCommand._save_configuration(), and ConfigurationCommand.configuration.

Here is the call graph for this function:

◆ unset_name()

None unset_name (   self,
Values  options,
List[str]  args 
)

Definition at line 189 of file configuration.py.

189 def unset_name(self, options: Values, args: List[str]) -> None:
190 key = self._get_n_args(args, "unset [name]", n=1)
191 self.configuration.unset_value(key)
192
193 self._save_configuration()
194

References ConfigurationCommand._get_n_args(), ConfigurationCommand._save_configuration(), and ConfigurationCommand.configuration.

Here is the call graph for this function:

Field Documentation

◆ cmd_opts

◆ configuration

◆ get_name

get_name

Definition at line 100 of file configuration.py.

◆ ignore_require_venv

bool ignore_require_venv = True
static

Definition at line 47 of file configuration.py.

◆ list_config_values

list_config_values

Definition at line 103 of file configuration.py.

◆ list_values

list_values

Definition at line 98 of file configuration.py.

◆ open_in_editor

open_in_editor

Definition at line 99 of file configuration.py.

◆ set_name_value

set_name_value

Definition at line 101 of file configuration.py.

◆ unset_name

unset_name

Definition at line 102 of file configuration.py.

◆ usage

str usage
static
Initial value:
= """
%prog [<file-option>] list
%prog [<file-option>] [--editor <editor-path>] edit
%prog [<file-option>] get command.option
%prog [<file-option>] set command.option value
%prog [<file-option>] unset command.option
%prog [<file-option>] debug
"""

Definition at line 48 of file configuration.py.


The documentation for this class was generated from the following file: