1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-21 12:28:41 +00:00

#N/A Add get_closest utility function

This commit is contained in:
nvbn 2015-07-08 21:30:24 +03:00
parent fbf7b91005
commit c0eae8b85c
5 changed files with 33 additions and 19 deletions

View File

@ -1,6 +1,6 @@
import pytest import pytest
from mock import Mock from mock import Mock
from thefuck.utils import sudo_support, wrap_settings, memoize from thefuck.utils import sudo_support, wrap_settings, memoize, get_closest
from thefuck.types import Settings from thefuck.types import Settings
from tests.utils import Command from tests.utils import Command
@ -32,3 +32,12 @@ def test_memoize():
memoized() memoized()
memoized() memoized()
fn.assert_called_once_with() fn.assert_called_once_with()
class TestGetClosest(object):
def test_when_can_match(self):
assert 'branch' == get_closest('brnch', ['branch', 'status'])
def test_when_cant_match(self):
assert 'status' == get_closest('st', ['status', 'reset'])

View File

@ -1,9 +1,10 @@
import difflib
import os import os
import re import re
from subprocess import check_output from subprocess import check_output
from thefuck.utils import get_closest
# Formulars are base on each local system's status # Formulars are base on each local system's status
brew_formulas = [] brew_formulas = []
try: try:
brew_path_prefix = check_output(['brew', '--prefix'], brew_path_prefix = check_output(['brew', '--prefix'],
@ -17,8 +18,8 @@ except:
pass pass
def _get_similar_formulars(formula_name): def _get_similar_formula(formula_name):
return difflib.get_close_matches(formula_name, brew_formulas, 1, 0.85) return get_closest(formula_name, brew_formulas, 1, 0.85)
def match(command, settings): def match(command, settings):
@ -29,7 +30,7 @@ def match(command, settings):
if is_proper_command: if is_proper_command:
formula = re.findall(r'Error: No available formula for ([a-z]+)', formula = re.findall(r'Error: No available formula for ([a-z]+)',
command.stderr)[0] command.stderr)[0]
has_possible_formulas = len(_get_similar_formulars(formula)) > 0 has_possible_formulas = bool(_get_similar_formula(formula))
return has_possible_formulas return has_possible_formulas
@ -37,6 +38,6 @@ def match(command, settings):
def get_new_command(command, settings): def get_new_command(command, settings):
not_exist_formula = re.findall(r'Error: No available formula for ([a-z]+)', not_exist_formula = re.findall(r'Error: No available formula for ([a-z]+)',
command.stderr)[0] command.stderr)[0]
exist_formula = _get_similar_formulars(not_exist_formula)[0] exist_formula = _get_similar_formula(not_exist_formula)
return command.script.replace(not_exist_formula, exist_formula, 1) return command.script.replace(not_exist_formula, exist_formula, 1)

View File

@ -1,8 +1,7 @@
import difflib
import os import os
import re import re
import subprocess import subprocess
from thefuck.utils import get_closest
BREW_CMD_PATH = '/Library/Homebrew/cmd' BREW_CMD_PATH = '/Library/Homebrew/cmd'
TAP_PATH = '/Library/Taps' TAP_PATH = '/Library/Taps'
@ -78,8 +77,8 @@ if brew_path_prefix:
pass pass
def _get_similar_commands(command): def _get_similar_command(command):
return difflib.get_close_matches(command, brew_commands) return get_closest(command, brew_commands)
def match(command, settings): def match(command, settings):
@ -90,7 +89,7 @@ def match(command, settings):
if is_proper_command: if is_proper_command:
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)', broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
command.stderr)[0] command.stderr)[0]
has_possible_commands = len(_get_similar_commands(broken_cmd)) > 0 has_possible_commands = bool(_get_similar_command(broken_cmd))
return has_possible_commands return has_possible_commands
@ -98,6 +97,6 @@ def match(command, settings):
def get_new_command(command, settings): def get_new_command(command, settings):
broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)', broken_cmd = re.findall(r'Error: Unknown command: ([a-z]+)',
command.stderr)[0] command.stderr)[0]
new_cmd = _get_similar_commands(broken_cmd)[0] new_cmd = _get_similar_command(broken_cmd)
return command.script.replace(broken_cmd, new_cmd, 1) return command.script.replace(broken_cmd, new_cmd, 1)

View File

@ -1,6 +1,5 @@
import re import re
from thefuck.utils import get_closest
from difflib import get_close_matches
def extract_possisiblities(command): def extract_possisiblities(command):
@ -26,9 +25,5 @@ def match(command, settings):
def get_new_command(command, settings): def get_new_command(command, settings):
script = command.script.split(' ') script = command.script.split(' ')
possisiblities = extract_possisiblities(command) possisiblities = extract_possisiblities(command)
matches = get_close_matches(script[1], possisiblities) script[1] = get_closest(script[1], possisiblities)
if matches:
script[1] = matches[0]
else:
script[1] = possisiblities[0]
return ' '.join(script) return ' '.join(script)

View File

@ -1,3 +1,4 @@
from difflib import get_close_matches
from functools import wraps from functools import wraps
import os import os
import pickle import pickle
@ -85,3 +86,12 @@ def memoize(fn):
return memo[key] return memo[key]
return wrapper return wrapper
def get_closest(word, possibilities, n=3, cutoff=0.6):
"""Returns closest match or just first from possibilities."""
possibilities = list(possibilities)
try:
return get_close_matches(word, possibilities, n, cutoff)[0]
except IndexError:
return possibilities[0]