From 5b535077bf1e5f5d592ff9968f1fd7fae26c75e9 Mon Sep 17 00:00:00 2001 From: Vladimir Iakovlev Date: Sat, 8 Oct 2016 12:18:33 +0200 Subject: [PATCH] #N/A: Stop changing `Command` inside rules --- README.md | 4 +++- thefuck/rules/brew_link.py | 9 +++++---- thefuck/rules/brew_uninstall.py | 7 ++++--- thefuck/rules/git_push.py | 12 +++++++----- thefuck/rules/git_rm_recursive.py | 7 ++++--- thefuck/rules/python_command.py | 5 ++--- thefuck/rules/systemctl.py | 2 +- thefuck/types.py | 3 ++- thefuck/utils.py | 2 +- 9 files changed, 29 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 2604b62b..b158c807 100644 --- a/README.md +++ b/README.md @@ -273,7 +273,9 @@ side_effect(old_command: Command, fixed_command: str) -> None ``` and optional `enabled_by_default`, `requires_output` and `priority` variables. -`Command` has three attributes: `script`, `stdout` and `stderr`. +`Command` has three attributes: `script`, `stdout`, `stderr` and `script_parts`. +Rule shouldn't change `Command`. + *Rules api changed in 3.0:* For accessing settings in rule you need to import it with `from thefuck.conf import settings`. `settings` is a special object filled with `~/.config/thefuck/settings.py` and values from env ([see more below](#settings)). diff --git a/thefuck/rules/brew_link.py b/thefuck/rules/brew_link.py index 23919a47..1c4d251e 100644 --- a/thefuck/rules/brew_link.py +++ b/thefuck/rules/brew_link.py @@ -8,7 +8,8 @@ def match(command): def get_new_command(command): - command.script_parts[1] = 'link' - command.script_parts.insert(2, '--overwrite') - command.script_parts.insert(3, '--dry-run') - return ' '.join(command.script_parts) + command_parts = command.script_parts[:] + command_parts[1] = 'link' + command_parts.insert(2, '--overwrite') + command_parts.insert(3, '--dry-run') + return ' '.join(command_parts) diff --git a/thefuck/rules/brew_uninstall.py b/thefuck/rules/brew_uninstall.py index d2306abd..f72062f7 100644 --- a/thefuck/rules/brew_uninstall.py +++ b/thefuck/rules/brew_uninstall.py @@ -8,6 +8,7 @@ def match(command): def get_new_command(command): - command.script_parts[1] = 'uninstall' - command.script_parts.insert(2, '--force') - return ' '.join(command.script_parts) + command_parts = command.script_parts[:] + command_parts[1] = 'uninstall' + command_parts.insert(2, '--force') + return ' '.join(command_parts) diff --git a/thefuck/rules/git_push.py b/thefuck/rules/git_push.py index 0a624eb9..af5d45fa 100644 --- a/thefuck/rules/git_push.py +++ b/thefuck/rules/git_push.py @@ -14,17 +14,19 @@ def get_new_command(command): # because the remaining arguments are concatenated onto the command suggested # by git, which includes --set-upstream and its argument upstream_option_index = -1 + command_parts = command.script_parts[:] + try: - upstream_option_index = command.script_parts.index('--set-upstream') + upstream_option_index = command_parts.index('--set-upstream') except ValueError: pass try: - upstream_option_index = command.script_parts.index('-u') + upstream_option_index = command_parts.index('-u') except ValueError: pass if upstream_option_index is not -1: - command.script_parts.pop(upstream_option_index) - command.script_parts.pop(upstream_option_index) + command_parts.pop(upstream_option_index) + command_parts.pop(upstream_option_index) push_upstream = command.stderr.split('\n')[-3].strip().partition('git ')[2] - return replace_argument(" ".join(command.script_parts), 'push', push_upstream) + return replace_argument(" ".join(command_parts), 'push', push_upstream) diff --git a/thefuck/rules/git_rm_recursive.py b/thefuck/rules/git_rm_recursive.py index b9cad274..9053aa6c 100644 --- a/thefuck/rules/git_rm_recursive.py +++ b/thefuck/rules/git_rm_recursive.py @@ -10,6 +10,7 @@ def match(command): @git_support def get_new_command(command): - index = command.script_parts.index('rm') + 1 - command.script_parts.insert(index, '-r') - return u' '.join(command.script_parts) + command_parts = command.script_parts[:] + index = command_parts.index('rm') + 1 + command_parts.insert(index, '-r') + return u' '.join(command_parts) diff --git a/thefuck/rules/python_command.py b/thefuck/rules/python_command.py index b4c321b7..98c56f72 100644 --- a/thefuck/rules/python_command.py +++ b/thefuck/rules/python_command.py @@ -6,9 +6,8 @@ from thefuck.specific.sudo import sudo_support @sudo_support def match(command): - toks = command.script_parts - return (toks - and toks[0].endswith('.py') + return (command.script_parts + and command.script_parts[0].endswith('.py') and ('Permission denied' in command.stderr or 'command not found' in command.stderr)) diff --git a/thefuck/rules/systemctl.py b/thefuck/rules/systemctl.py index 334c783e..499235c5 100644 --- a/thefuck/rules/systemctl.py +++ b/thefuck/rules/systemctl.py @@ -17,6 +17,6 @@ def match(command): @sudo_support def get_new_command(command): - cmd = command.script_parts + cmd = command.script_parts[:] cmd[-1], cmd[-2] = cmd[-2], cmd[-1] return ' '.join(cmd) diff --git a/thefuck/types.py b/thefuck/types.py index 527cf651..c03cb932 100644 --- a/thefuck/types.py +++ b/thefuck/types.py @@ -34,7 +34,8 @@ class Command(object): except Exception: logs.debug(u"Can't split command script {} because:\n {}".format( self, sys.exc_info())) - self._script_parts = None + self._script_parts = [] + return self._script_parts def __eq__(self, other): diff --git a/thefuck/utils.py b/thefuck/utils.py index b5680351..f6c0bb71 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -159,7 +159,7 @@ def is_app(command, *app_names, **kwargs): if kwargs: raise TypeError("got an unexpected keyword argument '{}'".format(kwargs.keys())) - if command.script_parts is not None and len(command.script_parts) > at_least: + if len(command.script_parts) > at_least: return command.script_parts[0] in app_names return False