Add ConfigurationError and use it.

This commit is contained in:
Pradyun S. Gedam 2017-04-08 11:01:03 +05:30
parent 39edb2cfb2
commit 757d356d89
3 changed files with 25 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import subprocess
from pip import cmdoptions
from pip.basecommand import Command
from pip.configuration import Configuration
from pip.exceptions import ConfigurationError
from pip.status_codes import SUCCESS, ERROR
logger = logging.getLogger(__name__)
@ -203,18 +204,22 @@ class ConfigurationCommand(Command):
def set_name_value(self, options):
key, value = options.set_name_value.split("=", 1)
self.configuration.set_value(key, value) # Error here is propagated.
try:
self.configuration.set_value(key, value)
except ConfigurationError:
logger.error("Could not set value in configuration")
else:
return self._save_configuration()
def unset_name(self, options):
key = options.unset_name
val = self.configuration.unset_value(key) # Error here is propagated.
if val is False:
logger.warn("ERROR: No key %r in configuration", key)
return ERROR
try:
self.configuration.unset_value(key)
except ConfigurationError:
logger.error("Could not unset value in configuration")
else:
return self._save_configuration()
def _save_configuration(self):

View File

@ -18,6 +18,7 @@ import logging
from pip._vendor.six import next
from pip._vendor.six.moves import configparser
from pip.exceptions import ConfigurationError
from pip.locations import (
legacy_config_file, new_config_file, running_under_virtualenv,
site_config_files, venv_config_file
@ -67,7 +68,7 @@ class Configuration(object):
super(Configuration, self).__init__()
if load_only not in ["user", "site-wide", "venv", None]:
raise ValueError(
raise ConfigurationError(
"Got invalid value for load_only - should be one of 'user', "
"'site-wide', 'venv'"
)
@ -127,7 +128,7 @@ class Configuration(object):
assert self.load_only is not None, _need_file_err_msg
if key not in self._config[self.load_only]:
raise KeyError(key)
raise ConfigurationError(key)
file, parser = self._get_parser_to_modify()
@ -150,9 +151,9 @@ class Configuration(object):
if file_parser_tuple not in self._modified_parsers:
self._modified_parsers.append(file_parser_tuple)
else:
# If here, something is there in the dictionary but not in the
# parser. This should not happen.
pass
raise ConfigurationError(
"Internal error [id=1]. Please report as a bug."
)
del self._config[self.load_only][key]
@ -277,8 +278,10 @@ class Configuration(object):
# Determine which parser to modify
parsers = self._parsers[self.load_only]
if not parsers:
# This should not happen if we're doing it correctly.
raise Exception("Internal configuration error!?")
# This should not happen if everything works correctly.
raise ConfigurationError(
"Internal error [id=2]. Please report as a bug."
)
# Use the highest priority parser.
return parsers[-1]

View File

@ -10,6 +10,10 @@ class PipError(Exception):
"""Base pip exception"""
class ConfigurationError(PipError):
"""General exception in configuration"""
class InstallationError(PipError):
"""General exception during installation"""