mirror of
https://github.com/nvbn/thefuck.git
synced 2025-09-18 19:22:32 +01:00
#154 Add priority to rules
This commit is contained in:
@@ -199,6 +199,8 @@ enabled_by_default = True
|
|||||||
|
|
||||||
def side_effect(command, settings):
|
def side_effect(command, settings):
|
||||||
subprocess.call('chmod 777 .', shell=True)
|
subprocess.call('chmod 777 .', shell=True)
|
||||||
|
|
||||||
|
priority = 1000 # Lower first
|
||||||
```
|
```
|
||||||
|
|
||||||
[More examples of rules](https://github.com/nvbn/thefuck/tree/master/thefuck/rules),
|
[More examples of rules](https://github.com/nvbn/thefuck/tree/master/thefuck/rules),
|
||||||
|
@@ -12,28 +12,43 @@ def test_load_rule(monkeypatch):
|
|||||||
load_source = Mock()
|
load_source = Mock()
|
||||||
load_source.return_value = Mock(match=match,
|
load_source.return_value = Mock(match=match,
|
||||||
get_new_command=get_new_command,
|
get_new_command=get_new_command,
|
||||||
enabled_by_default=True)
|
enabled_by_default=True,
|
||||||
|
priority=900)
|
||||||
monkeypatch.setattr('thefuck.main.load_source', load_source)
|
monkeypatch.setattr('thefuck.main.load_source', load_source)
|
||||||
assert main.load_rule(Path('/rules/bash.py')) \
|
assert main.load_rule(Path('/rules/bash.py')) \
|
||||||
== Rule('bash', match, get_new_command)
|
== Rule('bash', match, get_new_command, priority=900)
|
||||||
load_source.assert_called_once_with('bash', '/rules/bash.py')
|
load_source.assert_called_once_with('bash', '/rules/bash.py')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('conf_rules, rules', [
|
class TestGetRules(object):
|
||||||
(conf.DEFAULT_RULES, [Rule('bash', 'bash', 'bash'),
|
@pytest.fixture(autouse=True)
|
||||||
Rule('lisp', 'lisp', 'lisp'),
|
def glob(self, monkeypatch):
|
||||||
Rule('bash', 'bash', 'bash'),
|
mock = Mock(return_value=[])
|
||||||
Rule('lisp', 'lisp', 'lisp')]),
|
monkeypatch.setattr('thefuck.main.Path.glob', mock)
|
||||||
(types.RulesNamesList(['bash']), [Rule('bash', 'bash', 'bash'),
|
return mock
|
||||||
Rule('bash', 'bash', 'bash')])])
|
|
||||||
def test_get_rules(monkeypatch, conf_rules, rules):
|
def _compare_names(self, rules, names):
|
||||||
monkeypatch.setattr(
|
return [r.name for r in rules] == names
|
||||||
'thefuck.main.Path.glob',
|
|
||||||
lambda *_: [PosixPath('bash.py'), PosixPath('lisp.py')])
|
@pytest.mark.parametrize('conf_rules, rules', [
|
||||||
monkeypatch.setattr('thefuck.main.load_source',
|
(conf.DEFAULT_RULES, ['bash', 'lisp', 'bash', 'lisp']),
|
||||||
lambda x, _: Mock(match=x, get_new_command=x,
|
(types.RulesNamesList(['bash']), ['bash', 'bash'])])
|
||||||
enabled_by_default=True))
|
def test_get(self, monkeypatch, glob, conf_rules, rules):
|
||||||
assert list(main.get_rules(Path('~'), Mock(rules=conf_rules))) == rules
|
glob.return_value = [PosixPath('bash.py'), PosixPath('lisp.py')]
|
||||||
|
monkeypatch.setattr('thefuck.main.load_source',
|
||||||
|
lambda x, _: Rule(x))
|
||||||
|
assert self._compare_names(
|
||||||
|
main.get_rules(Path('~'), Mock(rules=conf_rules)), rules)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('unordered, ordered', [
|
||||||
|
([Rule('bash', priority=100), Rule('python', priority=5)],
|
||||||
|
['python', 'bash']),
|
||||||
|
([Rule('lisp', priority=9999), Rule('c', priority=conf.DEFAULT_PRIORITY)],
|
||||||
|
['c', 'lisp'])])
|
||||||
|
def test_ordered_by_priority(self, monkeypatch, unordered, ordered):
|
||||||
|
monkeypatch.setattr('thefuck.main._get_loaded_rules',
|
||||||
|
lambda *_: unordered)
|
||||||
|
assert self._compare_names(main.get_rules(Path('~'), Mock()), ordered)
|
||||||
|
|
||||||
|
|
||||||
class TestGetCommand(object):
|
class TestGetCommand(object):
|
||||||
@@ -64,6 +79,7 @@ class TestGetCommand(object):
|
|||||||
stdout=PIPE,
|
stdout=PIPE,
|
||||||
stderr=PIPE,
|
stderr=PIPE,
|
||||||
env={'LANG': 'C'})
|
env={'LANG': 'C'})
|
||||||
|
|
||||||
@pytest.mark.parametrize('args, result', [
|
@pytest.mark.parametrize('args, result', [
|
||||||
(['thefuck', 'ls', '-la'], 'ls -la'),
|
(['thefuck', 'ls', '-la'], 'ls -la'),
|
||||||
(['thefuck', 'ls'], 'ls')])
|
(['thefuck', 'ls'], 'ls')])
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
from thefuck import types
|
from thefuck import types
|
||||||
|
from thefuck.conf import DEFAULT_PRIORITY
|
||||||
|
|
||||||
|
|
||||||
def Command(script='', stdout='', stderr=''):
|
def Command(script='', stdout='', stderr=''):
|
||||||
@@ -8,6 +9,8 @@ def Command(script='', stdout='', stderr=''):
|
|||||||
def Rule(name='', match=lambda *_: True,
|
def Rule(name='', match=lambda *_: True,
|
||||||
get_new_command=lambda *_: '',
|
get_new_command=lambda *_: '',
|
||||||
enabled_by_default=True,
|
enabled_by_default=True,
|
||||||
side_effect=None):
|
side_effect=None,
|
||||||
|
priority=DEFAULT_PRIORITY):
|
||||||
return types.Rule(name, match, get_new_command,
|
return types.Rule(name, match, get_new_command,
|
||||||
enabled_by_default, side_effect)
|
enabled_by_default, side_effect,
|
||||||
|
priority)
|
||||||
|
@@ -22,6 +22,7 @@ class _DefaultRulesNames(types.RulesNamesList):
|
|||||||
|
|
||||||
|
|
||||||
DEFAULT_RULES = _DefaultRulesNames([])
|
DEFAULT_RULES = _DefaultRulesNames([])
|
||||||
|
DEFAULT_PRIORITY = 1000
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
|
DEFAULT_SETTINGS = {'rules': DEFAULT_RULES,
|
||||||
|
@@ -26,7 +26,17 @@ def load_rule(rule):
|
|||||||
return types.Rule(rule.name[:-3], rule_module.match,
|
return types.Rule(rule.name[:-3], rule_module.match,
|
||||||
rule_module.get_new_command,
|
rule_module.get_new_command,
|
||||||
getattr(rule_module, 'enabled_by_default', True),
|
getattr(rule_module, 'enabled_by_default', True),
|
||||||
getattr(rule_module, 'side_effect', None))
|
getattr(rule_module, 'side_effect', None),
|
||||||
|
getattr(rule_module, 'priority', conf.DEFAULT_PRIORITY))
|
||||||
|
|
||||||
|
|
||||||
|
def _get_loaded_rules(rules, settings):
|
||||||
|
"""Yields all available rules."""
|
||||||
|
for rule in rules:
|
||||||
|
if rule.name != '__init__.py':
|
||||||
|
loaded_rule = load_rule(rule)
|
||||||
|
if loaded_rule in settings.rules:
|
||||||
|
yield loaded_rule
|
||||||
|
|
||||||
|
|
||||||
def get_rules(user_dir, settings):
|
def get_rules(user_dir, settings):
|
||||||
@@ -35,11 +45,8 @@ def get_rules(user_dir, settings):
|
|||||||
.joinpath('rules') \
|
.joinpath('rules') \
|
||||||
.glob('*.py')
|
.glob('*.py')
|
||||||
user = user_dir.joinpath('rules').glob('*.py')
|
user = user_dir.joinpath('rules').glob('*.py')
|
||||||
for rule in sorted(list(bundled)) + list(user):
|
rules = _get_loaded_rules(sorted(bundled) + sorted(user), settings)
|
||||||
if rule.name != '__init__.py':
|
return sorted(rules, key=lambda rule: rule.priority)
|
||||||
loaded_rule = load_rule(rule)
|
|
||||||
if loaded_rule in settings.rules:
|
|
||||||
yield loaded_rule
|
|
||||||
|
|
||||||
|
|
||||||
def wait_output(settings, popen):
|
def wait_output(settings, popen):
|
||||||
|
@@ -4,7 +4,8 @@ from collections import namedtuple
|
|||||||
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
||||||
|
|
||||||
Rule = namedtuple('Rule', ('name', 'match', 'get_new_command',
|
Rule = namedtuple('Rule', ('name', 'match', 'get_new_command',
|
||||||
'enabled_by_default', 'side_effect'))
|
'enabled_by_default', 'side_effect',
|
||||||
|
'priority'))
|
||||||
|
|
||||||
|
|
||||||
class RulesNamesList(list):
|
class RulesNamesList(list):
|
||||||
|
Reference in New Issue
Block a user