|
22 | 22 | from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
|
23 | 23 | warnings.filterwarnings("ignore", category=DependencyWarning) # noqa
|
24 | 24 |
|
25 |
| - |
26 |
| -from pip.exceptions import InstallationError, CommandError, PipError |
27 | 25 | from pip.utils import get_installed_distributions, get_prog
|
28 | 26 | from pip.utils import deprecation, dist_is_editable
|
29 | 27 | from pip.vcs import git, mercurial, subversion, bazaar # noqa
|
30 | 28 | from pip.baseparser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
|
31 | 29 | from pip.commands import get_summaries, get_similar_command, commands_dict
|
| 30 | +from pip.exceptions import ( |
| 31 | + InstallationError, CommandError, PipError, ConfigurationError, |
| 32 | +) |
32 | 33 | from pip._vendor.requests.packages.urllib3.exceptions import (
|
33 | 34 | InsecureRequestWarning,
|
34 | 35 | )
|
@@ -155,6 +156,39 @@ def create_main_parser():
|
155 | 156 | return parser
|
156 | 157 |
|
157 | 158 |
|
| 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 | + |
158 | 192 | def parseopts(args):
|
159 | 193 | parser = create_main_parser()
|
160 | 194 |
|
@@ -187,14 +221,8 @@ def parseopts(args):
|
187 | 221 |
|
188 | 222 | # Autocorrect command name
|
189 | 223 | 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 |
198 | 226 |
|
199 | 227 | score, guess = get_similar_command(cmd_name, suggest_cut_off)
|
200 | 228 |
|
|
0 commit comments