From f20311fa89df2887d49395a8815cd3d6a00ef765 Mon Sep 17 00:00:00 2001 From: nvbn Date: Thu, 29 Oct 2015 00:13:59 +0800 Subject: [PATCH] #392: Little refactoring --- thefuck/rules/cargo_no_command.py | 2 +- thefuck/rules/cd_correction.py | 2 +- thefuck/rules/dirty_untar.py | 6 +++--- thefuck/rules/dirty_unzip.py | 2 +- thefuck/rules/dry.py | 4 ++-- thefuck/rules/git_branch_list.py | 4 ++-- thefuck/rules/git_fix_stash.py | 8 +++---- thefuck/rules/has_exists_script.py | 2 +- thefuck/rules/ls_lah.py | 4 ++-- thefuck/rules/man.py | 2 +- thefuck/rules/mercurial.py | 2 +- thefuck/rules/no_command.py | 8 +++---- thefuck/rules/pacman_not_found.py | 8 +++---- thefuck/rules/python_command.py | 2 +- thefuck/rules/rm_root.py | 4 ++-- thefuck/rules/systemctl.py | 4 ++-- thefuck/rules/vagrant_up.py | 2 +- thefuck/rules/whois.py | 2 +- thefuck/types.py | 34 ++++++++++++++---------------- thefuck/utils.py | 4 ++-- 20 files changed, 52 insertions(+), 54 deletions(-) diff --git a/thefuck/rules/cargo_no_command.py b/thefuck/rules/cargo_no_command.py index f181623..02e2a7f 100644 --- a/thefuck/rules/cargo_no_command.py +++ b/thefuck/rules/cargo_no_command.py @@ -9,7 +9,7 @@ def match(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] return replace_argument(command.script, broken, fix) diff --git a/thefuck/rules/cd_correction.py b/thefuck/rules/cd_correction.py index 4988697..5adab75 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.split_script[1].split(os.sep) + dest = command.script_parts[1].split(os.sep) if dest[-1] == '': dest = dest[:-1] cwd = os.getcwd() diff --git a/thefuck/rules/dirty_untar.py b/thefuck/rules/dirty_untar.py index 2b5090f..efbf1e3 100644 --- a/thefuck/rules/dirty_untar.py +++ b/thefuck/rules/dirty_untar.py @@ -30,17 +30,17 @@ def _tar_file(cmd): def match(command): return ('-C' not in 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): - 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}') \ .format(dir=dir, cmd=command.script) 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(): try: os.remove(file) diff --git a/thefuck/rules/dirty_unzip.py b/thefuck/rules/dirty_unzip.py index fced9b3..f15d6e9 100644 --- a/thefuck/rules/dirty_unzip.py +++ b/thefuck/rules/dirty_unzip.py @@ -14,7 +14,7 @@ def _zip_file(command): # unzip [-flags] file[.zip] [file(s) ...] [-x file(s) ...] # ^ ^ files to unzip from the archive # archive to unzip - for c in command.split_script[1:]: + for c in command.script_parts[1:]: if not c.startswith('-'): if c.endswith('.zip'): return c diff --git a/thefuck/rules/dry.py b/thefuck/rules/dry.py index d05fdaf..cfc91da 100644 --- a/thefuck/rules/dry.py +++ b/thefuck/rules/dry.py @@ -1,5 +1,5 @@ def match(command): - split_command = command.split_script + split_command = command.script_parts return (split_command and len(split_command) >= 2 @@ -7,7 +7,7 @@ def match(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 # 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 5c055d3..4c77b14 100644 --- a/thefuck/rules/git_branch_list.py +++ b/thefuck/rules/git_branch_list.py @@ -5,8 +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.split_script - and command.split_script[1:] == 'branch list'.split()) + return (command.script_parts + and command.script_parts[1:] == 'branch list'.split()) @git_support diff --git a/thefuck/rules/git_fix_stash.py b/thefuck/rules/git_fix_stash.py index 4714f86..19cef4c 100644 --- a/thefuck/rules/git_fix_stash.py +++ b/thefuck/rules/git_fix_stash.py @@ -5,8 +5,8 @@ from thefuck.specific.git import git_support @git_support def match(command): - if command.split_script and len(command.split_script) > 1: - return (command.split_script[1] == 'stash' + if command.script_parts and len(command.script_parts) > 1: + return (command.script_parts[1] == 'stash' and 'usage:' in command.stderr) else: return False @@ -25,12 +25,12 @@ stash_commands = ( @git_support 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) if fixed is not None: return replace_argument(command.script, stash_cmd, fixed) else: - cmd = command.split_script[:] + cmd = command.script_parts[:] cmd.insert(2, 'save') return ' '.join(cmd) diff --git a/thefuck/rules/has_exists_script.py b/thefuck/rules/has_exists_script.py index c62da9e..e71b3a1 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 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 diff --git a/thefuck/rules/ls_lah.py b/thefuck/rules/ls_lah.py index bd1f16b..93c0da5 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 command.split_script and 'ls -' not in command.script + return command.script_parts and 'ls -' not in command.script def get_new_command(command): - command = command.split_script[:] + command = command.script_parts[:] command[0] = 'ls -lah' return ' '.join(command) diff --git a/thefuck/rules/man.py b/thefuck/rules/man.py index 18972be..ead1361 100644 --- a/thefuck/rules/man.py +++ b/thefuck/rules/man.py @@ -12,7 +12,7 @@ def get_new_command(command): if '2' in command.script: return command.script.replace("2", "3") - split_cmd2 = command.split_script + split_cmd2 = command.script_parts split_cmd3 = split_cmd2[:] split_cmd2.insert(1, ' 2 ') diff --git a/thefuck/rules/mercurial.py b/thefuck/rules/mercurial.py index b1d93de..f2b6581 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.split_script[:] + script = command.script_parts[:] 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 2f46bfc..f67cda5 100644 --- a/thefuck/rules/no_command.py +++ b/thefuck/rules/no_command.py @@ -5,17 +5,17 @@ from thefuck.specific.sudo import sudo_support @sudo_support def match(command): - return (command.split_script + return (command.script_parts 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()))) @sudo_support 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) - return [' '.join([new_command] + command.split_script[1:]) + return [' '.join([new_command] + command.script_parts[1:]) for new_command in new_cmds] diff --git a/thefuck/rules/pacman_not_found.py b/thefuck/rules/pacman_not_found.py index 129f80f..2019a4f 100644 --- a/thefuck/rules/pacman_not_found.py +++ b/thefuck/rules/pacman_not_found.py @@ -11,14 +11,14 @@ from thefuck.specific.archlinux import get_pkgfile, archlinux_env def match(command): - return (command.split_script - and (command.split_script[0] in ('pacman', 'yaourt') - or command.split_script[0:2] == ['sudo', 'pacman']) + return (command.script_parts + and (command.script_parts[0] in ('pacman', 'yaourt') + or command.script_parts[0:2] == ['sudo', 'pacman']) and 'error: target not found:' in command.stderr) def get_new_command(command): - pgr = command.split_script[-1] + pgr = command.script_parts[-1] return replace_command(command, pgr, get_pkgfile(pgr)) diff --git a/thefuck/rules/python_command.py b/thefuck/rules/python_command.py index 714d6d3..b4c321b 100644 --- a/thefuck/rules/python_command.py +++ b/thefuck/rules/python_command.py @@ -6,7 +6,7 @@ from thefuck.specific.sudo import sudo_support @sudo_support def match(command): - toks = command.split_script + toks = command.script_parts return (toks and toks[0].endswith('.py') and ('Permission denied' in command.stderr or diff --git a/thefuck/rules/rm_root.py b/thefuck/rules/rm_root.py index ad4aa7e..3ac8d4a 100644 --- a/thefuck/rules/rm_root.py +++ b/thefuck/rules/rm_root.py @@ -5,8 +5,8 @@ enabled_by_default = False @sudo_support def match(command): - return (command.split_script - and {'rm', '/'}.issubset(command.split_script) + return (command.script_parts + and {'rm', '/'}.issubset(command.script_parts) and '--no-preserve-root' not in command.script and '--no-preserve-root' in command.stderr) diff --git a/thefuck/rules/systemctl.py b/thefuck/rules/systemctl.py index 2605b62..334c783 100644 --- a/thefuck/rules/systemctl.py +++ b/thefuck/rules/systemctl.py @@ -10,13 +10,13 @@ from thefuck.utils import for_app def match(command): # Catches "Unknown operation 'service'." when executing systemctl with # misordered arguments - cmd = command.split_script + cmd = command.script_parts return (cmd and 'Unknown operation \'' in command.stderr and len(cmd) - cmd.index('systemctl') == 3) @sudo_support def get_new_command(command): - cmd = command.split_script + cmd = command.script_parts 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 d638527..04610a0 100644 --- a/thefuck/rules/vagrant_up.py +++ b/thefuck/rules/vagrant_up.py @@ -8,7 +8,7 @@ def match(command): def get_new_command(command): - cmds = command.split_script + cmds = command.script_parts machine = None if len(cmds) >= 3: machine = cmds[2] diff --git a/thefuck/rules/whois.py b/thefuck/rules/whois.py index 61711de..c16bd70 100644 --- a/thefuck/rules/whois.py +++ b/thefuck/rules/whois.py @@ -25,7 +25,7 @@ def match(command): def get_new_command(command): - url = command.split_script[1] + url = command.script_parts[1] if '/' in command.script: return 'whois ' + urlparse(url).netloc diff --git a/thefuck/types.py b/thefuck/types.py index 779829b..442744c 100644 --- a/thefuck/types.py +++ b/thefuck/types.py @@ -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 .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED from .exceptions import EmptyCommand 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): @@ -21,22 +21,20 @@ class Command(object): :type stderr: basestring """ - self._script = script + self.script = script self.stdout = stdout self.stderr = stderr - try: - self._split_script = shells.split_command(script) - except: - self._split_script = None - @property - def script(self): - return self._script - - @property - def split_script(self): - return self._split_script + def script_parts(self): + if not hasattr(self, '_script_parts'): + try: + self._script_parts = shells.split_command(self.script) + except Exception as e: + logs.exception("Can't split command script {}".format(self), + sys.exc_info()) + self._script_parts = None + return self._script_parts def __eq__(self, other): if isinstance(other, Command): diff --git a/thefuck/utils.py b/thefuck/utils.py index bdcc25b..2d0266e 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -145,8 +145,8 @@ def is_app(command, *app_names, **kwargs): 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 + if command.script_parts is not None and len(command.script_parts) > at_least: + return command.script_parts[0] in app_names return False