mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-19 01:50:01 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -1,20 +1,24 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
from optparse import Values
|
||||
from typing import Any, List, Optional
|
||||
from typing import Any
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
|
||||
from pip._internal.cli.base_command import Command
|
||||
from pip._internal.cli.status_codes import ERROR, SUCCESS
|
||||
from pip._internal.configuration import (
|
||||
Configuration,
|
||||
Kind,
|
||||
get_configuration_files,
|
||||
kinds,
|
||||
)
|
||||
from pip._internal.cli.status_codes import ERROR
|
||||
from pip._internal.cli.status_codes import SUCCESS
|
||||
from pip._internal.configuration import Configuration
|
||||
from pip._internal.configuration import get_configuration_files
|
||||
from pip._internal.configuration import Kind
|
||||
from pip._internal.configuration import kinds
|
||||
from pip._internal.exceptions import PipError
|
||||
from pip._internal.utils.logging import indent_log
|
||||
from pip._internal.utils.misc import get_prog, write_output
|
||||
from pip._internal.utils.misc import get_prog
|
||||
from pip._internal.utils.misc import write_output
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -51,57 +55,57 @@ class ConfigurationCommand(Command):
|
|||
|
||||
def add_options(self) -> None:
|
||||
self.cmd_opts.add_option(
|
||||
"--editor",
|
||||
dest="editor",
|
||||
action="store",
|
||||
'--editor',
|
||||
dest='editor',
|
||||
action='store',
|
||||
default=None,
|
||||
help=(
|
||||
"Editor to use to edit the file. Uses VISUAL or EDITOR "
|
||||
"environment variables if not provided."
|
||||
'Editor to use to edit the file. Uses VISUAL or EDITOR '
|
||||
'environment variables if not provided.'
|
||||
),
|
||||
)
|
||||
|
||||
self.cmd_opts.add_option(
|
||||
"--global",
|
||||
dest="global_file",
|
||||
action="store_true",
|
||||
'--global',
|
||||
dest='global_file',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help="Use the system-wide configuration file only",
|
||||
help='Use the system-wide configuration file only',
|
||||
)
|
||||
|
||||
self.cmd_opts.add_option(
|
||||
"--user",
|
||||
dest="user_file",
|
||||
action="store_true",
|
||||
'--user',
|
||||
dest='user_file',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help="Use the user configuration file only",
|
||||
help='Use the user configuration file only',
|
||||
)
|
||||
|
||||
self.cmd_opts.add_option(
|
||||
"--site",
|
||||
dest="site_file",
|
||||
action="store_true",
|
||||
'--site',
|
||||
dest='site_file',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help="Use the current environment configuration file only",
|
||||
help='Use the current environment configuration file only',
|
||||
)
|
||||
|
||||
self.parser.insert_option_group(0, self.cmd_opts)
|
||||
|
||||
def run(self, options: Values, args: List[str]) -> int:
|
||||
def run(self, options: Values, args: list[str]) -> int:
|
||||
handlers = {
|
||||
"list": self.list_values,
|
||||
"edit": self.open_in_editor,
|
||||
"get": self.get_name,
|
||||
"set": self.set_name_value,
|
||||
"unset": self.unset_name,
|
||||
"debug": self.list_config_values,
|
||||
'list': self.list_values,
|
||||
'edit': self.open_in_editor,
|
||||
'get': self.get_name,
|
||||
'set': self.set_name_value,
|
||||
'unset': self.unset_name,
|
||||
'debug': self.list_config_values,
|
||||
}
|
||||
|
||||
# Determine action
|
||||
if not args or args[0] not in handlers:
|
||||
logger.error(
|
||||
"Need an action (%s) to perform.",
|
||||
", ".join(sorted(handlers)),
|
||||
'Need an action (%s) to perform.',
|
||||
', '.join(sorted(handlers)),
|
||||
)
|
||||
return ERROR
|
||||
|
||||
|
|
@ -111,7 +115,7 @@ class ConfigurationCommand(Command):
|
|||
# Depends on whether the command is modifying.
|
||||
try:
|
||||
load_only = self._determine_file(
|
||||
options, need_value=(action in ["get", "set", "unset", "edit"])
|
||||
options, need_value=(action in ['get', 'set', 'unset', 'edit']),
|
||||
)
|
||||
except PipError as e:
|
||||
logger.error(e.args[0])
|
||||
|
|
@ -119,7 +123,7 @@ class ConfigurationCommand(Command):
|
|||
|
||||
# Load a new configuration
|
||||
self.configuration = Configuration(
|
||||
isolated=options.isolated_mode, load_only=load_only
|
||||
isolated=options.isolated_mode, load_only=load_only,
|
||||
)
|
||||
self.configuration.load()
|
||||
|
||||
|
|
@ -132,7 +136,7 @@ class ConfigurationCommand(Command):
|
|||
|
||||
return SUCCESS
|
||||
|
||||
def _determine_file(self, options: Values, need_value: bool) -> Optional[Kind]:
|
||||
def _determine_file(self, options: Values, need_value: bool) -> Kind | None:
|
||||
file_options = [
|
||||
key
|
||||
for key, value in (
|
||||
|
|
@ -158,47 +162,47 @@ class ConfigurationCommand(Command):
|
|||
return file_options[0]
|
||||
|
||||
raise PipError(
|
||||
"Need exactly one file to operate upon "
|
||||
"(--user, --site, --global) to perform."
|
||||
'Need exactly one file to operate upon '
|
||||
'(--user, --site, --global) to perform.',
|
||||
)
|
||||
|
||||
def list_values(self, options: Values, args: List[str]) -> None:
|
||||
self._get_n_args(args, "list", n=0)
|
||||
def list_values(self, options: Values, args: list[str]) -> None:
|
||||
self._get_n_args(args, 'list', n=0)
|
||||
|
||||
for key, value in sorted(self.configuration.items()):
|
||||
write_output("%s=%r", key, value)
|
||||
write_output('%s=%r', key, value)
|
||||
|
||||
def get_name(self, options: Values, args: List[str]) -> None:
|
||||
key = self._get_n_args(args, "get [name]", n=1)
|
||||
def get_name(self, options: Values, args: list[str]) -> None:
|
||||
key = self._get_n_args(args, 'get [name]', n=1)
|
||||
value = self.configuration.get_value(key)
|
||||
|
||||
write_output("%s", value)
|
||||
write_output('%s', value)
|
||||
|
||||
def set_name_value(self, options: Values, args: List[str]) -> None:
|
||||
key, value = self._get_n_args(args, "set [name] [value]", n=2)
|
||||
def set_name_value(self, options: Values, args: list[str]) -> None:
|
||||
key, value = self._get_n_args(args, 'set [name] [value]', n=2)
|
||||
self.configuration.set_value(key, value)
|
||||
|
||||
self._save_configuration()
|
||||
|
||||
def unset_name(self, options: Values, args: List[str]) -> None:
|
||||
key = self._get_n_args(args, "unset [name]", n=1)
|
||||
def unset_name(self, options: Values, args: list[str]) -> None:
|
||||
key = self._get_n_args(args, 'unset [name]', n=1)
|
||||
self.configuration.unset_value(key)
|
||||
|
||||
self._save_configuration()
|
||||
|
||||
def list_config_values(self, options: Values, args: List[str]) -> None:
|
||||
def list_config_values(self, options: Values, args: list[str]) -> None:
|
||||
"""List config key-value pairs across different config files"""
|
||||
self._get_n_args(args, "debug", n=0)
|
||||
self._get_n_args(args, 'debug', n=0)
|
||||
|
||||
self.print_env_var_values()
|
||||
# Iterate over config files and print if they exist, and the
|
||||
# key-value pairs present in them if they do
|
||||
for variant, files in sorted(self.configuration.iter_config_files()):
|
||||
write_output("%s:", variant)
|
||||
write_output('%s:', variant)
|
||||
for fname in files:
|
||||
with indent_log():
|
||||
file_exists = os.path.exists(fname)
|
||||
write_output("%s, exists: %r", fname, file_exists)
|
||||
write_output('%s, exists: %r', fname, file_exists)
|
||||
if file_exists:
|
||||
self.print_config_file_values(variant)
|
||||
|
||||
|
|
@ -206,35 +210,35 @@ class ConfigurationCommand(Command):
|
|||
"""Get key-value pairs from the file of a variant"""
|
||||
for name, value in self.configuration.get_values_in_config(variant).items():
|
||||
with indent_log():
|
||||
write_output("%s: %s", name, value)
|
||||
write_output('%s: %s', name, value)
|
||||
|
||||
def print_env_var_values(self) -> None:
|
||||
"""Get key-values pairs present as environment variables"""
|
||||
write_output("%s:", "env_var")
|
||||
write_output('%s:', 'env_var')
|
||||
with indent_log():
|
||||
for key, value in sorted(self.configuration.get_environ_vars()):
|
||||
env_var = f"PIP_{key.upper()}"
|
||||
write_output("%s=%r", env_var, value)
|
||||
env_var = f'PIP_{key.upper()}'
|
||||
write_output('%s=%r', env_var, value)
|
||||
|
||||
def open_in_editor(self, options: Values, args: List[str]) -> None:
|
||||
def open_in_editor(self, options: Values, args: list[str]) -> None:
|
||||
editor = self._determine_editor(options)
|
||||
|
||||
fname = self.configuration.get_file_to_edit()
|
||||
if fname is None:
|
||||
raise PipError("Could not determine appropriate file.")
|
||||
raise PipError('Could not determine appropriate file.')
|
||||
|
||||
try:
|
||||
subprocess.check_call([editor, fname])
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise PipError(
|
||||
"Editor Subprocess exited with exit code {}".format(e.returncode)
|
||||
f'Editor Subprocess exited with exit code {e.returncode}',
|
||||
)
|
||||
|
||||
def _get_n_args(self, args: List[str], example: str, n: int) -> Any:
|
||||
def _get_n_args(self, args: list[str], example: str, n: int) -> Any:
|
||||
"""Helper to make sure the command got the right number of arguments"""
|
||||
if len(args) != n:
|
||||
msg = (
|
||||
"Got unexpected number of arguments, expected {}. "
|
||||
'Got unexpected number of arguments, expected {}. '
|
||||
'(example: "{} config {}")'
|
||||
).format(n, get_prog(), example)
|
||||
raise PipError(msg)
|
||||
|
|
@ -251,16 +255,16 @@ class ConfigurationCommand(Command):
|
|||
self.configuration.save()
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Unable to save configuration. Please report this as a bug."
|
||||
'Unable to save configuration. Please report this as a bug.',
|
||||
)
|
||||
raise PipError("Internal Error.")
|
||||
raise PipError('Internal Error.')
|
||||
|
||||
def _determine_editor(self, options: Values) -> str:
|
||||
if options.editor is not None:
|
||||
return options.editor
|
||||
elif "VISUAL" in os.environ:
|
||||
return os.environ["VISUAL"]
|
||||
elif "EDITOR" in os.environ:
|
||||
return os.environ["EDITOR"]
|
||||
elif 'VISUAL' in os.environ:
|
||||
return os.environ['VISUAL']
|
||||
elif 'EDITOR' in os.environ:
|
||||
return os.environ['EDITOR']
|
||||
else:
|
||||
raise PipError("Could not determine editor to use.")
|
||||
raise PipError('Could not determine editor to use.')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue