From e71a3e0cdb08d3b22ea2635d99b3065b190a0829 Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 28 Oct 2015 16:43:24 +0100 Subject: [PATCH] Replace (almost) all instance of script.split --- tests/rules/test_has_exists_script.py | 13 +++++++------ thefuck/rules/cargo_no_command.py | 4 ++-- thefuck/rules/cd_correction.py | 2 +- thefuck/rules/dry.py | 8 +++++--- thefuck/rules/git_branch_list.py | 3 ++- thefuck/rules/git_fix_stash.py | 9 ++++----- thefuck/rules/has_exists_script.py | 2 +- thefuck/rules/ls_lah.py | 4 ++-- thefuck/rules/man.py | 8 ++++++-- thefuck/rules/mercurial.py | 2 +- thefuck/rules/no_command.py | 11 ++++++----- thefuck/rules/pacman_not_found.py | 6 ++++-- thefuck/rules/python_command.py | 4 ++-- thefuck/rules/rm_root.py | 3 ++- thefuck/rules/switch_lang.py | 6 ++++-- thefuck/rules/systemctl.py | 8 ++++---- thefuck/rules/vagrant_up.py | 4 ++-- thefuck/rules/whois.py | 6 ++++-- thefuck/utils.py | 16 ++++++++++------ 19 files changed, 69 insertions(+), 50 deletions(-) diff --git a/tests/rules/test_has_exists_script.py b/tests/rules/test_has_exists_script.py index 6a94760c..c6b4b1ed 100644 --- a/tests/rules/test_has_exists_script.py +++ b/tests/rules/test_has_exists_script.py @@ -1,17 +1,18 @@ -from mock import Mock, patch +from mock import patch from thefuck.rules.has_exists_script import match, get_new_command +from ..utils import Command def test_match(): with patch('os.path.exists', return_value=True): - assert match(Mock(script='main', stderr='main: command not found')) - assert match(Mock(script='main --help', + assert match(Command(script='main', stderr='main: command not found')) + assert match(Command(script='main --help', stderr='main: command not found')) - assert not match(Mock(script='main', stderr='')) + assert not match(Command(script='main', stderr='')) with patch('os.path.exists', return_value=False): - assert not match(Mock(script='main', stderr='main: command not found')) + assert not match(Command(script='main', stderr='main: command not found')) def test_get_new_command(): - assert get_new_command(Mock(script='main --help')) == './main --help' + assert get_new_command(Command(script='main --help')) == './main --help' diff --git a/thefuck/rules/cargo_no_command.py b/thefuck/rules/cargo_no_command.py index 76eec3cb..f1816232 100644 --- a/thefuck/rules/cargo_no_command.py +++ b/thefuck/rules/cargo_no_command.py @@ -2,14 +2,14 @@ import re from thefuck.utils import replace_argument, for_app -@for_app('cargo') +@for_app('cargo', at_least=1) def match(command): return ('No such subcommand' in command.stderr and 'Did you mean' in command.stderr) def get_new_command(command): - broken = command.script.split()[1] + broken = command.split_script[1] fix = re.findall(r'Did you mean `([^`]*)`', command.stderr)[0] return replace_argument(command.script, broken, fix) diff --git a/thefuck/rules/cd_correction.py b/thefuck/rules/cd_correction.py index 24c70067..49886973 100644 --- a/thefuck/rules/cd_correction.py +++ b/thefuck/rules/cd_correction.py @@ -33,7 +33,7 @@ def get_new_command(command): defaults to the rules of cd_mkdir. Change sensitivity by changing MAX_ALLOWED_DIFF. Default value is 0.6 """ - dest = command.script.split()[1].split(os.sep) + dest = command.split_script[1].split(os.sep) if dest[-1] == '': dest = dest[:-1] cwd = os.getcwd() diff --git a/thefuck/rules/dry.py b/thefuck/rules/dry.py index 075df04f..d05fdafe 100644 --- a/thefuck/rules/dry.py +++ b/thefuck/rules/dry.py @@ -1,11 +1,13 @@ def match(command): - split_command = command.script.split() + split_command = command.split_script - return len(split_command) >= 2 and split_command[0] == split_command[1] + return (split_command + and len(split_command) >= 2 + and split_command[0] == split_command[1]) def get_new_command(command): - return command.script[command.script.find(' ')+1:] + return ' '.join(command.split_script[1:]) # it should be rare enough to actually have to type twice the same word, so # this rule can have a higher priority to come before things like "cd cd foo" diff --git a/thefuck/rules/git_branch_list.py b/thefuck/rules/git_branch_list.py index d40ef98d..5c055d33 100644 --- a/thefuck/rules/git_branch_list.py +++ b/thefuck/rules/git_branch_list.py @@ -5,7 +5,8 @@ from thefuck.specific.git import git_support @git_support def match(command): # catches "git branch list" in place of "git branch" - return command.script.split()[1:] == 'branch list'.split() + return (command.split_script + and command.split_script[1:] == 'branch list'.split()) @git_support diff --git a/thefuck/rules/git_fix_stash.py b/thefuck/rules/git_fix_stash.py index 944d37c5..4714f866 100644 --- a/thefuck/rules/git_fix_stash.py +++ b/thefuck/rules/git_fix_stash.py @@ -5,9 +5,8 @@ from thefuck.specific.git import git_support @git_support def match(command): - splited_script = command.script.split() - if len(splited_script) > 1: - return (splited_script[1] == 'stash' + if command.split_script and len(command.split_script) > 1: + return (command.split_script[1] == 'stash' and 'usage:' in command.stderr) else: return False @@ -26,12 +25,12 @@ stash_commands = ( @git_support def get_new_command(command): - stash_cmd = command.script.split()[2] + stash_cmd = command.split_script[2] fixed = utils.get_closest(stash_cmd, stash_commands, fallback_to_first=False) if fixed is not None: return replace_argument(command.script, stash_cmd, fixed) else: - cmd = command.script.split() + cmd = command.split_script[:] cmd.insert(2, 'save') return ' '.join(cmd) diff --git a/thefuck/rules/has_exists_script.py b/thefuck/rules/has_exists_script.py index 0ffc0860..c62da9e7 100644 --- a/thefuck/rules/has_exists_script.py +++ b/thefuck/rules/has_exists_script.py @@ -4,7 +4,7 @@ from thefuck.specific.sudo import sudo_support @sudo_support def match(command): - return os.path.exists(command.script.split()[0]) \ + return command.split_script and os.path.exists(command.split_script[0]) \ and 'command not found' in command.stderr diff --git a/thefuck/rules/ls_lah.py b/thefuck/rules/ls_lah.py index 54b6d651..bd1f16ba 100644 --- a/thefuck/rules/ls_lah.py +++ b/thefuck/rules/ls_lah.py @@ -3,10 +3,10 @@ from thefuck.utils import for_app @for_app('ls') def match(command): - return 'ls -' not in command.script + return command.split_script and 'ls -' not in command.script def get_new_command(command): - command = command.script.split(' ') + command = command.split_script[:] command[0] = 'ls -lah' return ' '.join(command) diff --git a/thefuck/rules/man.py b/thefuck/rules/man.py index 84427e79..18972be9 100644 --- a/thefuck/rules/man.py +++ b/thefuck/rules/man.py @@ -1,5 +1,9 @@ +from thefuck.utils import for_app + + +@for_app('man', at_least=1) def match(command): - return command.script.strip().startswith('man ') + return True def get_new_command(command): @@ -8,7 +12,7 @@ def get_new_command(command): if '2' in command.script: return command.script.replace("2", "3") - split_cmd2 = command.script.split() + split_cmd2 = command.split_script split_cmd3 = split_cmd2[:] split_cmd2.insert(1, ' 2 ') diff --git a/thefuck/rules/mercurial.py b/thefuck/rules/mercurial.py index c8290fb5..b1d93de6 100644 --- a/thefuck/rules/mercurial.py +++ b/thefuck/rules/mercurial.py @@ -21,7 +21,7 @@ def match(command): def get_new_command(command): - script = command.script.split(' ') + script = command.split_script[:] possibilities = extract_possibilities(command) script[1] = get_closest(script[1], possibilities) return ' '.join(script) diff --git a/thefuck/rules/no_command.py b/thefuck/rules/no_command.py index 5318568c..2f46bfc5 100644 --- a/thefuck/rules/no_command.py +++ b/thefuck/rules/no_command.py @@ -5,16 +5,17 @@ from thefuck.specific.sudo import sudo_support @sudo_support def match(command): - return 'not found' in command.stderr and \ - bool(get_close_matches(command.script.split(' ')[0], - get_all_executables())) + return (command.split_script + and 'not found' in command.stderr + and bool(get_close_matches(command.split_script[0], + get_all_executables()))) @sudo_support def get_new_command(command): - old_command = command.script.split(' ')[0] + old_command = command.split_script[0] new_cmds = get_close_matches(old_command, get_all_executables(), cutoff=0.1) - return [' '.join([new_command] + command.script.split(' ')[1:]) + return [' '.join([new_command] + command.split_script[1:]) for new_command in new_cmds] diff --git a/thefuck/rules/pacman_not_found.py b/thefuck/rules/pacman_not_found.py index 9875935b..129f80f4 100644 --- a/thefuck/rules/pacman_not_found.py +++ b/thefuck/rules/pacman_not_found.py @@ -11,12 +11,14 @@ from thefuck.specific.archlinux import get_pkgfile, archlinux_env def match(command): - return (command.script.startswith(('pacman', 'sudo pacman', 'yaourt')) + return (command.split_script + and (command.split_script[0] in ('pacman', 'yaourt') + or command.split_script[0:2] == ['sudo', 'pacman']) and 'error: target not found:' in command.stderr) def get_new_command(command): - pgr = command.script.split()[-1] + pgr = command.split_script[-1] return replace_command(command, pgr, get_pkgfile(pgr)) diff --git a/thefuck/rules/python_command.py b/thefuck/rules/python_command.py index 95bbf021..714d6d31 100644 --- a/thefuck/rules/python_command.py +++ b/thefuck/rules/python_command.py @@ -6,8 +6,8 @@ from thefuck.specific.sudo import sudo_support @sudo_support def match(command): - toks = command.script.split() - return (len(toks) > 0 + toks = command.split_script + return (toks and toks[0].endswith('.py') and ('Permission denied' in command.stderr or 'command not found' in command.stderr)) diff --git a/thefuck/rules/rm_root.py b/thefuck/rules/rm_root.py index 28e522f5..ad4aa7e4 100644 --- a/thefuck/rules/rm_root.py +++ b/thefuck/rules/rm_root.py @@ -5,7 +5,8 @@ enabled_by_default = False @sudo_support def match(command): - return ({'rm', '/'}.issubset(command.script.split()) + return (command.split_script + and {'rm', '/'}.issubset(command.split_script) and '--no-preserve-root' not in command.script and '--no-preserve-root' in command.stderr) diff --git a/thefuck/rules/switch_lang.py b/thefuck/rules/switch_lang.py index 8952eb05..23f1422d 100644 --- a/thefuck/rules/switch_lang.py +++ b/thefuck/rules/switch_lang.py @@ -11,9 +11,11 @@ source_layouts = [u'''йцукенгшщзхъфывапролджэячсмит @memoize def _get_matched_layout(command): + # don't use command.split_script here because a layout mismatch will likely + # result in a non-splitable sript as per shlex + cmd = command.script.split(' ') for source_layout in source_layouts: - if all([ch in source_layout or ch in '-_' - for ch in command.script.split(' ')[0]]): + if all([ch in source_layout or ch in '-_' for ch in cmd[0]]): return source_layout diff --git a/thefuck/rules/systemctl.py b/thefuck/rules/systemctl.py index 771206d7..2605b625 100644 --- a/thefuck/rules/systemctl.py +++ b/thefuck/rules/systemctl.py @@ -8,15 +8,15 @@ from thefuck.utils import for_app @sudo_support @for_app('systemctl') def match(command): - # Catches 'Unknown operation 'service'.' when executing systemctl with + # Catches "Unknown operation 'service'." when executing systemctl with # misordered arguments - cmd = command.script.split() - return ('Unknown operation \'' in command.stderr and + cmd = command.split_script + return (cmd and 'Unknown operation \'' in command.stderr and len(cmd) - cmd.index('systemctl') == 3) @sudo_support def get_new_command(command): - cmd = command.script.split() + cmd = command.split_script cmd[-1], cmd[-2] = cmd[-2], cmd[-1] return ' '.join(cmd) diff --git a/thefuck/rules/vagrant_up.py b/thefuck/rules/vagrant_up.py index 44093972..d6385273 100644 --- a/thefuck/rules/vagrant_up.py +++ b/thefuck/rules/vagrant_up.py @@ -8,13 +8,13 @@ def match(command): def get_new_command(command): - cmds = command.script.split(' ') + cmds = command.split_script machine = None if len(cmds) >= 3: machine = cmds[2] startAllInstances = shells.and_("vagrant up", command.script) - if machine is None: + if machine is None: return startAllInstances else: return [ shells.and_("vagrant up " + machine, command.script), startAllInstances] diff --git a/thefuck/rules/whois.py b/thefuck/rules/whois.py index 141c7332..61711de9 100644 --- a/thefuck/rules/whois.py +++ b/thefuck/rules/whois.py @@ -1,7 +1,9 @@ # -*- encoding: utf-8 -*- from six.moves.urllib.parse import urlparse +from thefuck.utils import for_app +@for_app('whois', at_least=1) def match(command): """ What the `whois` command returns depends on the 'Whois server' it contacted @@ -19,11 +21,11 @@ def match(command): - www.google.fr → subdomain: www, domain: 'google.fr'; - google.co.uk → subdomain: None, domain; 'google.co.uk'. """ - return 'whois ' in command.script.strip() + return True def get_new_command(command): - url = command.script.split()[1] + url = command.split_script[1] if '/' in command.script: return 'whois ' + urlparse(url).netloc diff --git a/thefuck/utils.py b/thefuck/utils.py index ac011ec7..bdcc25b8 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -138,19 +138,23 @@ def replace_command(command, broken, matched): @memoize -def is_app(command, *app_names): +def is_app(command, *app_names, **kwargs): """Returns `True` if command is call to one of passed app names.""" - if command.split_script is not None and len(command.split_script) > 0: - app = command.split_script[0] - return app in app_names + + at_least = kwargs.pop('at_least', 0) + if kwargs: + raise TypeError("got an unexpected keyword argument '{}'".format(kwargs.keys())) + + if command.split_script is not None and len(command.split_script) > at_least: + return command.split_script[0] in app_names return False -def for_app(*app_names): +def for_app(*app_names, **kwargs): """Specifies that matching script is for on of app names.""" def _for_app(fn, command): - if is_app(command, *app_names): + if is_app(command, *app_names, **kwargs): return fn(command) else: return False