Skip to content

Commit ffca676

Browse files
committed
Use configuration for autocorrect
1 parent 1c5bace commit ffca676

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

pip/__init__.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
2323
warnings.filterwarnings("ignore", category=DependencyWarning) # noqa
2424

25-
26-
from pip.exceptions import InstallationError, CommandError, PipError
2725
from pip.utils import get_installed_distributions, get_prog
2826
from pip.utils import deprecation, dist_is_editable
2927
from pip.vcs import git, mercurial, subversion, bazaar # noqa
3028
from pip.baseparser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
3129
from pip.commands import get_summaries, get_similar_command, commands_dict
30+
from pip.exceptions import (
31+
InstallationError, CommandError, PipError, ConfigurationError,
32+
)
3233
from pip._vendor.requests.packages.urllib3.exceptions import (
3334
InsecureRequestWarning,
3435
)
@@ -155,6 +156,39 @@ def create_main_parser():
155156
return parser
156157

157158

159+
def _get_autocorrect_configuration_values(config):
160+
di = dict(config.items())
161+
configuration_problems = []
162+
163+
def get_float(var_name, default):
164+
if var_name not in di:
165+
return default
166+
167+
val = di[var_name]
168+
try:
169+
return float(val)
170+
except Exception:
171+
configuration_problems.append(
172+
var_name + " should be a floating point value"
173+
)
174+
return None
175+
176+
wait_time = get_float("wait_time", 2.0)
177+
suggest_cut_off = get_float("suggest_cut_off", 0.6)
178+
replace_cut_off = get_float("replace_cut_off", 0.8)
179+
180+
if suggest_cut_off is not None and replace_cut_off is not None:
181+
if suggest_cut_off > replace_cut_off:
182+
configuration_problems.append(
183+
"suggest_cut_off should be less than that of "
184+
"replace_cut_off in configuration files"
185+
)
186+
187+
if configuration_problems:
188+
raise ConfigurationError(", ".join(configuration_problems))
189+
return wait_time, suggest_cut_off, replace_cut_off
190+
191+
158192
def parseopts(args):
159193
parser = create_main_parser()
160194

@@ -187,14 +221,8 @@ def parseopts(args):
187221

188222
# Autocorrect command name
189223
if cmd_name not in commands_dict:
190-
# MARK: The following should be loaded from the configuration file
191-
# in the future. For now, it can stay like this, I guess.
192-
wait_time = 2 # float
193-
suggest_cut_off = 0.6 # float, between 0-1
194-
replace_cut_off = 0.8 # float, between 0-1
195-
196-
assert suggest_cut_off <= replace_cut_off, \
197-
"autocorrect - suggestions cut off value invalid!"
224+
vals = _get_autocorrect_configuration_values(parser.config)
225+
wait_time, suggest_cut_off, replace_cut_off = vals
198226

199227
score, guess = get_similar_command(cmd_name, suggest_cut_off)
200228

pip/commands/help.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import
22

33
from pip.basecommand import Command, SUCCESS
4-
from pip.exceptions import CommandError
4+
from pip.exceptions import CommandError, ConfigurationError
55

66

77
class HelpCommand(Command):
@@ -14,8 +14,15 @@ class HelpCommand(Command):
1414
def run(self, options, args):
1515
from pip.commands import commands_dict, get_similar_command
1616

17-
suggest_cut_off = 0.6 # float, between 0-1
17+
di = dict(self.parser.config.items())
18+
suggest_cut_off = di.get("suggest_cut_off", 0.6) # float, between 0-1
1819

20+
try:
21+
suggest_cut_off = float(suggest_cut_off)
22+
except Exception:
23+
raise ConfigurationError(
24+
"suggest_cut_off should be a floating point value"
25+
)
1926
try:
2027
# 'pip help' with no args is handled by pip.__init__.parseopt()
2128
cmd_name = args[0] # the command we need help for

pip/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class PipError(Exception):
1010
"""Base pip exception"""
1111

1212

13+
class ConfigurationError(PipError):
14+
"""General exception during configuration"""
15+
16+
1317
class InstallationError(PipError):
1418
"""General exception during installation"""
1519

0 commit comments

Comments
 (0)