Skip to content

Commit

Permalink
Merge pull request #8568 from McSinyx/conf-const
Browse files Browse the repository at this point in the history
Declare constants in configuration.py as such
  • Loading branch information
pradyunsg authored Sep 18, 2020
2 parents 67ab116 + e0f311b commit 7c1e5d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
Empty file.
55 changes: 23 additions & 32 deletions src/pip/_internal/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@
RawConfigParser = configparser.RawConfigParser # Shorthand
Kind = NewType("Kind", str)

CONFIG_BASENAME = 'pip.ini' if WINDOWS else 'pip.conf'
ENV_NAMES_IGNORED = "version", "help"

# The kinds of configurations there are.
kinds = enum(
USER="user", # User Specific
GLOBAL="global", # System Wide
SITE="site", # [Virtual] Environment Specific
ENV="env", # from PIP_CONFIG_FILE
ENV_VAR="env-var", # from Environment Variables
)
OVERRIDE_ORDER = kinds.GLOBAL, kinds.USER, kinds.SITE, kinds.ENV, kinds.ENV_VAR
VALID_LOAD_ONLY = kinds.USER, kinds.GLOBAL, kinds.SITE

logger = logging.getLogger(__name__)


Expand All @@ -60,19 +74,6 @@ def _disassemble_key(name):
return name.split(".", 1)


# The kinds of configurations there are.
kinds = enum(
USER="user", # User Specific
GLOBAL="global", # System Wide
SITE="site", # [Virtual] Environment Specific
ENV="env", # from PIP_CONFIG_FILE
ENV_VAR="env-var", # from Environment Variables
)


CONFIG_BASENAME = 'pip.ini' if WINDOWS else 'pip.conf'


def get_configuration_files():
# type: () -> Dict[Kind, List[str]]
global_config_files = [
Expand Down Expand Up @@ -114,29 +115,21 @@ def __init__(self, isolated, load_only=None):
# type: (bool, Optional[Kind]) -> None
super(Configuration, self).__init__()

_valid_load_only = [kinds.USER, kinds.GLOBAL, kinds.SITE, None]
if load_only not in _valid_load_only:
if load_only is not None and load_only not in VALID_LOAD_ONLY:
raise ConfigurationError(
"Got invalid value for load_only - should be one of {}".format(
", ".join(map(repr, _valid_load_only[:-1]))
", ".join(map(repr, VALID_LOAD_ONLY))
)
)
self.isolated = isolated
self.load_only = load_only

# The order here determines the override order.
self._override_order = [
kinds.GLOBAL, kinds.USER, kinds.SITE, kinds.ENV, kinds.ENV_VAR
]

self._ignore_env_names = ["version", "help"]

# Because we keep track of where we got the data from
self._parsers = {
variant: [] for variant in self._override_order
variant: [] for variant in OVERRIDE_ORDER
} # type: Dict[Kind, List[Tuple[str, RawConfigParser]]]
self._config = {
variant: {} for variant in self._override_order
variant: {} for variant in OVERRIDE_ORDER
} # type: Dict[Kind, Dict[str, Any]]
self._modified_parsers = [] # type: List[Tuple[str, RawConfigParser]]

Expand Down Expand Up @@ -257,7 +250,7 @@ def _dictionary(self):
# are not needed here.
retval = {}

for variant in self._override_order:
for variant in OVERRIDE_ORDER:
retval.update(self._config[variant])

return retval
Expand Down Expand Up @@ -348,12 +341,10 @@ def get_environ_vars(self):
# type: () -> Iterable[Tuple[str, str]]
"""Returns a generator with all environmental vars with prefix PIP_"""
for key, val in os.environ.items():
should_be_yielded = (
key.startswith("PIP_") and
key[4:].lower() not in self._ignore_env_names
)
if should_be_yielded:
yield key[4:].lower(), val
if key.startswith("PIP_"):
name = key[4:].lower()
if name not in ENV_NAMES_IGNORED:
yield name, val

# XXX: This is patched in the tests.
def iter_config_files(self):
Expand Down

0 comments on commit 7c1e5d3

Please sign in to comment.