1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-01-31 10:11:14 +00:00

#392: Little refactoring

This commit is contained in:
nvbn 2015-10-29 00:13:59 +08:00
parent a4c391096a
commit f20311fa89
20 changed files with 52 additions and 54 deletions

View File

@ -9,7 +9,7 @@ def match(command):
def get_new_command(command): def get_new_command(command):
broken = command.split_script[1] broken = command.script_parts[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.split_script[1].split(os.sep) dest = command.script_parts[1].split(os.sep)
if dest[-1] == '': if dest[-1] == '':
dest = dest[:-1] dest = dest[:-1]
cwd = os.getcwd() cwd = os.getcwd()

View File

@ -30,17 +30,17 @@ def _tar_file(cmd):
def match(command): def match(command):
return ('-C' not in command.script return ('-C' not in command.script
and _is_tar_extract(command.script) and _is_tar_extract(command.script)
and _tar_file(command.split_script) is not None) and _tar_file(command.script_parts) is not None)
def get_new_command(command): def get_new_command(command):
dir = shells.quote(_tar_file(command.split_script)[1]) dir = shells.quote(_tar_file(command.script_parts)[1])
return shells.and_('mkdir -p {dir}', '{cmd} -C {dir}') \ return shells.and_('mkdir -p {dir}', '{cmd} -C {dir}') \
.format(dir=dir, cmd=command.script) .format(dir=dir, cmd=command.script)
def side_effect(old_cmd, command): def side_effect(old_cmd, command):
with tarfile.TarFile(_tar_file(old_cmd.split_script)[0]) as archive: with tarfile.TarFile(_tar_file(old_cmd.script_parts)[0]) as archive:
for file in archive.getnames(): for file in archive.getnames():
try: try:
os.remove(file) os.remove(file)

View File

@ -14,7 +14,7 @@ def _zip_file(command):
# unzip [-flags] file[.zip] [file(s) ...] [-x file(s) ...] # unzip [-flags] file[.zip] [file(s) ...] [-x file(s) ...]
# ^ ^ files to unzip from the archive # ^ ^ files to unzip from the archive
# archive to unzip # archive to unzip
for c in command.split_script[1:]: for c in command.script_parts[1:]:
if not c.startswith('-'): if not c.startswith('-'):
if c.endswith('.zip'): if c.endswith('.zip'):
return c return c

View File

@ -1,5 +1,5 @@
def match(command): def match(command):
split_command = command.split_script split_command = command.script_parts
return (split_command return (split_command
and len(split_command) >= 2 and len(split_command) >= 2
@ -7,7 +7,7 @@ def match(command):
def get_new_command(command): def get_new_command(command):
return ' '.join(command.split_script[1:]) return ' '.join(command.script_parts[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,8 +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.split_script return (command.script_parts
and command.split_script[1:] == 'branch list'.split()) and command.script_parts[1:] == 'branch list'.split())
@git_support @git_support

View File

@ -5,8 +5,8 @@ from thefuck.specific.git import git_support
@git_support @git_support
def match(command): def match(command):
if command.split_script and len(command.split_script) > 1: if command.script_parts and len(command.script_parts) > 1:
return (command.split_script[1] == 'stash' return (command.script_parts[1] == 'stash'
and 'usage:' in command.stderr) and 'usage:' in command.stderr)
else: else:
return False return False
@ -25,12 +25,12 @@ stash_commands = (
@git_support @git_support
def get_new_command(command): def get_new_command(command):
stash_cmd = command.split_script[2] stash_cmd = command.script_parts[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.split_script[:] cmd = command.script_parts[:]
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 command.split_script and os.path.exists(command.split_script[0]) \ return command.script_parts and os.path.exists(command.script_parts[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 command.split_script and 'ls -' not in command.script return command.script_parts and 'ls -' not in command.script
def get_new_command(command): def get_new_command(command):
command = command.split_script[:] command = command.script_parts[:]
command[0] = 'ls -lah' command[0] = 'ls -lah'
return ' '.join(command) return ' '.join(command)

View File

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

View File

@ -11,14 +11,14 @@ from thefuck.specific.archlinux import get_pkgfile, archlinux_env
def match(command): def match(command):
return (command.split_script return (command.script_parts
and (command.split_script[0] in ('pacman', 'yaourt') and (command.script_parts[0] in ('pacman', 'yaourt')
or command.split_script[0:2] == ['sudo', 'pacman']) or command.script_parts[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.split_script[-1] pgr = command.script_parts[-1]
return replace_command(command, pgr, get_pkgfile(pgr)) return replace_command(command, pgr, get_pkgfile(pgr))

View File

@ -6,7 +6,7 @@ from thefuck.specific.sudo import sudo_support
@sudo_support @sudo_support
def match(command): def match(command):
toks = command.split_script toks = command.script_parts
return (toks 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

View File

@ -5,8 +5,8 @@ enabled_by_default = False
@sudo_support @sudo_support
def match(command): def match(command):
return (command.split_script return (command.script_parts
and {'rm', '/'}.issubset(command.split_script) and {'rm', '/'}.issubset(command.script_parts)
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

@ -10,13 +10,13 @@ from thefuck.utils import for_app
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.split_script cmd = command.script_parts
return (cmd and '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.split_script cmd = command.script_parts
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.split_script cmds = command.script_parts
machine = None machine = None
if len(cmds) >= 3: if len(cmds) >= 3:
machine = cmds[2] machine = cmds[2]

View File

@ -25,7 +25,7 @@ def match(command):
def get_new_command(command): def get_new_command(command):
url = command.split_script[1] url = command.script_parts[1]
if '/' in command.script: if '/' in command.script:
return 'whois ' + urlparse(url).netloc return 'whois ' + urlparse(url).netloc

View File

@ -1,13 +1,13 @@
from imp import load_source
from subprocess import Popen, PIPE
import os
import sys
import six
from psutil import Process, TimeoutExpired
from . import logs, shells from . import logs, shells
from .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED from .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED
from .exceptions import EmptyCommand from .exceptions import EmptyCommand
from .utils import compatibility_call from .utils import compatibility_call
from imp import load_source
from psutil import Process, TimeoutExpired
from subprocess import Popen, PIPE
import os
import six
import sys
class Command(object): class Command(object):
@ -21,22 +21,20 @@ class Command(object):
:type stderr: basestring :type stderr: basestring
""" """
self._script = script self.script = script
self.stdout = stdout self.stdout = stdout
self.stderr = stderr self.stderr = stderr
try:
self._split_script = shells.split_command(script)
except:
self._split_script = None
@property @property
def script(self): def script_parts(self):
return self._script if not hasattr(self, '_script_parts'):
try:
@property self._script_parts = shells.split_command(self.script)
def split_script(self): except Exception as e:
return self._split_script logs.exception("Can't split command script {}".format(self),
sys.exc_info())
self._script_parts = None
return self._script_parts
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, Command): if isinstance(other, Command):

View File

@ -145,8 +145,8 @@ def is_app(command, *app_names, **kwargs):
if kwargs: if kwargs:
raise TypeError("got an unexpected keyword argument '{}'".format(kwargs.keys())) raise TypeError("got an unexpected keyword argument '{}'".format(kwargs.keys()))
if command.split_script is not None and len(command.split_script) > at_least: if command.script_parts is not None and len(command.script_parts) > at_least:
return command.split_script[0] in app_names return command.script_parts[0] in app_names
return False return False