1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-12 07:59:01 +00:00

Replace (almost) all instance of script.split

This commit is contained in:
mcarton 2015-10-28 16:43:24 +01:00
parent 2d995d464f
commit e71a3e0cdb
19 changed files with 69 additions and 50 deletions

View File

@ -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 thefuck.rules.has_exists_script import match, get_new_command
from ..utils import Command
def test_match(): def test_match():
with patch('os.path.exists', return_value=True): with patch('os.path.exists', return_value=True):
assert match(Mock(script='main', stderr='main: command not found')) assert match(Command(script='main', stderr='main: command not found'))
assert match(Mock(script='main --help', assert match(Command(script='main --help',
stderr='main: command not found')) 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): 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(): 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'

View File

@ -2,14 +2,14 @@ import re
from thefuck.utils import replace_argument, for_app from thefuck.utils import replace_argument, for_app
@for_app('cargo') @for_app('cargo', at_least=1)
def match(command): def match(command):
return ('No such subcommand' in command.stderr return ('No such subcommand' in command.stderr
and 'Did you mean' in command.stderr) and 'Did you mean' in command.stderr)
def get_new_command(command): 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] fix = re.findall(r'Did you mean `([^`]*)`', command.stderr)[0]
return replace_argument(command.script, broken, fix) return replace_argument(command.script, broken, fix)

View File

@ -33,7 +33,7 @@ def get_new_command(command):
defaults to the rules of cd_mkdir. defaults to the rules of cd_mkdir.
Change sensitivity by changing MAX_ALLOWED_DIFF. Default value is 0.6 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] == '': if dest[-1] == '':
dest = dest[:-1] dest = dest[:-1]
cwd = os.getcwd() cwd = os.getcwd()

View File

@ -1,11 +1,13 @@
def match(command): 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): 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 # 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" # this rule can have a higher priority to come before things like "cd cd foo"

View File

@ -5,7 +5,8 @@ from thefuck.specific.git import git_support
@git_support @git_support
def match(command): def match(command):
# catches "git branch list" in place of "git branch" # 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 @git_support

View File

@ -5,9 +5,8 @@ from thefuck.specific.git import git_support
@git_support @git_support
def match(command): def match(command):
splited_script = command.script.split() if command.split_script and len(command.split_script) > 1:
if len(splited_script) > 1: return (command.split_script[1] == 'stash'
return (splited_script[1] == 'stash'
and 'usage:' in command.stderr) and 'usage:' in command.stderr)
else: else:
return False return False
@ -26,12 +25,12 @@ stash_commands = (
@git_support @git_support
def get_new_command(command): 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) fixed = utils.get_closest(stash_cmd, stash_commands, fallback_to_first=False)
if fixed is not None: if fixed is not None:
return replace_argument(command.script, stash_cmd, fixed) return replace_argument(command.script, stash_cmd, fixed)
else: else:
cmd = command.script.split() cmd = command.split_script[:]
cmd.insert(2, 'save') cmd.insert(2, 'save')
return ' '.join(cmd) return ' '.join(cmd)

View File

@ -4,7 +4,7 @@ from thefuck.specific.sudo import sudo_support
@sudo_support @sudo_support
def match(command): 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 and 'command not found' in command.stderr

View File

@ -3,10 +3,10 @@ from thefuck.utils import for_app
@for_app('ls') @for_app('ls')
def match(command): def match(command):
return 'ls -' not in command.script return command.split_script and 'ls -' not in command.script
def get_new_command(command): def get_new_command(command):
command = command.script.split(' ') command = command.split_script[:]
command[0] = 'ls -lah' command[0] = 'ls -lah'
return ' '.join(command) return ' '.join(command)

View File

@ -1,5 +1,9 @@
from thefuck.utils import for_app
@for_app('man', at_least=1)
def match(command): def match(command):
return command.script.strip().startswith('man ') return True
def get_new_command(command): def get_new_command(command):
@ -8,7 +12,7 @@ def get_new_command(command):
if '2' in command.script: if '2' in command.script:
return command.script.replace("2", "3") return command.script.replace("2", "3")
split_cmd2 = command.script.split() split_cmd2 = command.split_script
split_cmd3 = split_cmd2[:] split_cmd3 = split_cmd2[:]
split_cmd2.insert(1, ' 2 ') split_cmd2.insert(1, ' 2 ')

View File

@ -21,7 +21,7 @@ def match(command):
def get_new_command(command): def get_new_command(command):
script = command.script.split(' ') script = command.split_script[:]
possibilities = extract_possibilities(command) possibilities = extract_possibilities(command)
script[1] = get_closest(script[1], possibilities) script[1] = get_closest(script[1], possibilities)
return ' '.join(script) return ' '.join(script)

View File

@ -5,16 +5,17 @@ from thefuck.specific.sudo import sudo_support
@sudo_support @sudo_support
def match(command): def match(command):
return 'not found' in command.stderr and \ return (command.split_script
bool(get_close_matches(command.script.split(' ')[0], and 'not found' in command.stderr
get_all_executables())) and bool(get_close_matches(command.split_script[0],
get_all_executables())))
@sudo_support @sudo_support
def get_new_command(command): 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) 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] for new_command in new_cmds]

View File

@ -11,12 +11,14 @@ from thefuck.specific.archlinux import get_pkgfile, archlinux_env
def match(command): 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) and 'error: target not found:' in command.stderr)
def get_new_command(command): def get_new_command(command):
pgr = command.script.split()[-1] pgr = command.split_script[-1]
return replace_command(command, pgr, get_pkgfile(pgr)) return replace_command(command, pgr, get_pkgfile(pgr))

View File

@ -6,8 +6,8 @@ from thefuck.specific.sudo import sudo_support
@sudo_support @sudo_support
def match(command): def match(command):
toks = command.script.split() toks = command.split_script
return (len(toks) > 0 return (toks
and toks[0].endswith('.py') and toks[0].endswith('.py')
and ('Permission denied' in command.stderr or and ('Permission denied' in command.stderr or
'command not found' in command.stderr)) 'command not found' in command.stderr))

View File

@ -5,7 +5,8 @@ enabled_by_default = False
@sudo_support @sudo_support
def match(command): 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' not in command.script
and '--no-preserve-root' in command.stderr) and '--no-preserve-root' in command.stderr)

View File

@ -11,9 +11,11 @@ source_layouts = [u'''йцукенгшщзхъфывапролджэячсмит
@memoize @memoize
def _get_matched_layout(command): 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: for source_layout in source_layouts:
if all([ch in source_layout or ch in '-_' if all([ch in source_layout or ch in '-_' for ch in cmd[0]]):
for ch in command.script.split(' ')[0]]):
return source_layout return source_layout

View File

@ -8,15 +8,15 @@ from thefuck.utils import for_app
@sudo_support @sudo_support
@for_app('systemctl') @for_app('systemctl')
def match(command): def match(command):
# Catches 'Unknown operation 'service'.' when executing systemctl with # Catches "Unknown operation 'service'." when executing systemctl with
# misordered arguments # misordered arguments
cmd = command.script.split() cmd = command.split_script
return ('Unknown operation \'' in command.stderr and return (cmd and 'Unknown operation \'' in command.stderr and
len(cmd) - cmd.index('systemctl') == 3) len(cmd) - cmd.index('systemctl') == 3)
@sudo_support @sudo_support
def get_new_command(command): def get_new_command(command):
cmd = command.script.split() cmd = command.split_script
cmd[-1], cmd[-2] = cmd[-2], cmd[-1] cmd[-1], cmd[-2] = cmd[-2], cmd[-1]
return ' '.join(cmd) return ' '.join(cmd)

View File

@ -8,7 +8,7 @@ def match(command):
def get_new_command(command): def get_new_command(command):
cmds = command.script.split(' ') cmds = command.split_script
machine = None machine = None
if len(cmds) >= 3: if len(cmds) >= 3:
machine = cmds[2] machine = cmds[2]

View File

@ -1,7 +1,9 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
from six.moves.urllib.parse import urlparse from six.moves.urllib.parse import urlparse
from thefuck.utils import for_app
@for_app('whois', at_least=1)
def match(command): def match(command):
""" """
What the `whois` command returns depends on the 'Whois server' it contacted 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'; - www.google.fr subdomain: www, domain: 'google.fr';
- google.co.uk subdomain: None, domain; 'google.co.uk'. - google.co.uk subdomain: None, domain; 'google.co.uk'.
""" """
return 'whois ' in command.script.strip() return True
def get_new_command(command): def get_new_command(command):
url = command.script.split()[1] url = command.split_script[1]
if '/' in command.script: if '/' in command.script:
return 'whois ' + urlparse(url).netloc return 'whois ' + urlparse(url).netloc

View File

@ -138,19 +138,23 @@ def replace_command(command, broken, matched):
@memoize @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.""" """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] at_least = kwargs.pop('at_least', 0)
return app in app_names 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 return False
def for_app(*app_names): def for_app(*app_names, **kwargs):
"""Specifies that matching script is for on of app names.""" """Specifies that matching script is for on of app names."""
def _for_app(fn, command): def _for_app(fn, command):
if is_app(command, *app_names): if is_app(command, *app_names, **kwargs):
return fn(command) return fn(command)
else: else:
return False return False