diff options
author | Pradyun S. Gedam <pradyunsg@gmail.com> | 2017-05-14 12:27:45 +0530 |
---|---|---|
committer | Pradyun S. Gedam <pradyunsg@gmail.com> | 2017-05-20 12:56:13 +0530 |
commit | 3efb452a2acb6ba297639488ec91ef5a1c7de4aa (patch) | |
tree | 79518cd7c24cc428a03d5d53bd5ba3c3db79c6c5 /pip | |
parent | c9113e41db8b788985a19f1c652be7037611c327 (diff) | |
download | pip-3efb452a2acb6ba297639488ec91ef5a1c7de4aa.tar.gz |
Use an enum-like attribute to refer to configuration sources
Diffstat (limited to 'pip')
-rw-r--r-- | pip/commands/configuration.py | 12 | ||||
-rw-r--r-- | pip/configuration.py | 44 | ||||
-rw-r--r-- | pip/utils/__init__.py | 8 |
3 files changed, 40 insertions, 24 deletions
diff --git a/pip/commands/configuration.py b/pip/commands/configuration.py index a748fa7ef..b06d75586 100644 --- a/pip/commands/configuration.py +++ b/pip/commands/configuration.py @@ -3,7 +3,7 @@ import os import subprocess from pip.basecommand import Command -from pip.configuration import Configuration +from pip.configuration import Configuration, kinds from pip.exceptions import PipError from pip.locations import venv_config_file from pip.status_codes import SUCCESS, ERROR @@ -128,9 +128,9 @@ class ConfigurationCommand(Command): def _determine_file(self, options, need_value): file_options = { - "user": options.user_file, - "global": options.global_file, - "venv": options.venv_file + kinds.USER: options.user_file, + kinds.GLOBAL: options.global_file, + kinds.VENV: options.venv_file } if sum(file_options.values()) == 0: @@ -138,9 +138,9 @@ class ConfigurationCommand(Command): return None # Default to user, unless there's a virtualenv file. elif os.path.exists(venv_config_file): - return "venv" + return kinds.VENV else: - return "user" + return kinds.USER elif sum(file_options.values()) == 1: # There's probably a better expression for this. return [key for key in file_options if file_options[key]][0] diff --git a/pip/configuration.py b/pip/configuration.py index 103224f93..a16e06f30 100644 --- a/pip/configuration.py +++ b/pip/configuration.py @@ -22,7 +22,7 @@ from pip.locations import ( legacy_config_file, new_config_file, running_under_virtualenv, site_config_files, venv_config_file ) -from pip.utils import ensure_dir +from pip.utils import ensure_dir, enum _need_file_err_msg = "Needed a specific file to be modifying." @@ -48,6 +48,10 @@ def _make_key(variant, name): return ".".join((variant, name)) +# The kinds of configurations there are. +kinds = enum(USER="user", GLOBAL="global", VENV="venv", ENV="environement") + + class Configuration(object): """Handles management of configuration. @@ -65,16 +69,20 @@ class Configuration(object): def __init__(self, isolated, load_only=None): super(Configuration, self).__init__() - if load_only not in ["user", "global", "venv", None]: + _valid_load_only = [kinds.USER, kinds.GLOBAL, kinds.VENV, None] + if load_only not in _valid_load_only: raise ConfigurationError( - "Got invalid value for load_only - should be one of 'user', " - "'global', 'venv'" + "Got invalid value for load_only - should be one of {}".format( + ", ".join(map(repr, _valid_load_only[:-1])) + ) ) self.isolated = isolated self.load_only = load_only # The order here determines the override order. - self._override_order = ["global", "user", "venv", "environment"] + self._override_order = [ + kinds.GLOBAL, kinds.USER, kinds.VENV, kinds.ENV + ] # Because we keep track of where we got the data from self._parsers = {variant: [] for variant in self._override_order} @@ -197,10 +205,10 @@ class Configuration(object): """Loads configuration from configuration files """ config_files = dict(self._get_config_files()) - if config_files["environment"][0:1] == [os.devnull]: + if config_files[kinds.ENV][0:1] == [os.devnull]: logger.debug( "Skipping loading configuration files due to " - "environment's PIP_CONFIG_FILE being os.devnull" + "environment's kinds.PIP_CONFIG being os.devnull" ) return @@ -241,7 +249,7 @@ class Configuration(object): def _load_environment_vars(self): """Loads configuration from environment variables """ - self._config["environment"].update( + self._config[kinds.ENV].update( self._normalized_keys(":env:", self._get_environ_vars()) ) @@ -272,26 +280,26 @@ class Configuration(object): # SMELL: Move the conditions out of this function # environment variables have the lowest priority - config_file = os.environ.get('PIP_CONFIG_FILE', None) + config_file = os.environ.get('kinds.PIP_CONFIG', None) if config_file is not None: - yield "environment", [config_file] + yield kinds.ENV, [config_file] else: - yield "environment", [] + yield kinds.ENV, [] - # at the base we have any global configuration - yield "global", list(site_config_files) + # at the base we have any kinds.global configuration + yield kinds.GLOBAL, list(site_config_files) - # per-user configuration next - should_load_user_config = not self.isolated and not ( + # per-kinds.user configuration next + kinds.should_load_user_config = not self.isolated and not ( config_file and os.path.exists(config_file) ) - if should_load_user_config: + if kinds.should_load_user_config: # The legacy config file is overridden by the new config file - yield "user", [legacy_config_file, new_config_file] + yield kinds.USER, [legacy_config_file, new_config_file] # finally virtualenv configuration first trumping others if running_under_virtualenv(): - yield "venv", [venv_config_file] + yield kinds.VENV, [venv_config_file] def _get_parser_to_modify(self): # Determine which parser to modify diff --git a/pip/utils/__init__.py b/pip/utils/__init__.py index 4e86e54f8..e0091ebec 100644 --- a/pip/utils/__init__.py +++ b/pip/utils/__init__.py @@ -854,3 +854,11 @@ def get_installed_version(dist_name, lookup_dirs=None): def consume(iterator): """Consume an iterable at C speed.""" deque(iterator, maxlen=0) + + +# Simulates an enum +def enum(*sequential, **named): + enums = dict(zip(sequential, range(len(sequential))), **named) + reverse = dict((value, key) for key, value in enums.items()) + enums['reverse_mapping'] = reverse + return type('Enum', (), enums) |