1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-07 13:41:21 +00:00

Allow rules to correct commands that time out

This commit is contained in:
mcarton 2015-07-25 23:04:08 +02:00
parent bfa3c905a3
commit 71bb1994c3
4 changed files with 33 additions and 20 deletions

View File

@ -14,7 +14,8 @@ def test_load_rule(mocker):
return_value=Mock(match=match, 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)) priority=900,
requires_output=True))
assert main.load_rule(Path('/rules/bash.py')) \ assert main.load_rule(Path('/rules/bash.py')) \
== Rule('bash', match, get_new_command, priority=900) == 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')

View File

@ -10,7 +10,8 @@ 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): priority=DEFAULT_PRIORITY,
requires_output=True):
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) priority, requires_output)

View File

@ -28,7 +28,8 @@ def load_rule(rule):
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)) getattr(rule_module, 'priority', conf.DEFAULT_PRIORITY),
getattr(rule_module, 'requires_output', True))
def _get_loaded_rules(rules, settings): def _get_loaded_rules(rules, settings):
@ -87,13 +88,26 @@ def get_command(settings, args):
settings): settings):
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE, env=env) result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE, env=env)
if wait_output(settings, result): if wait_output(settings, result):
return types.Command(script, result.stdout.read().decode('utf-8'), stdout = result.stdout.read().decode('utf-8')
result.stderr.read().decode('utf-8')) stderr = result.stderr.read().decode('utf-8')
logs.debug(u'Received stdout: {}'.format(stdout), settings)
logs.debug(u'Received stderr: {}'.format(stderr), settings)
return types.Command(script, stdout, stderr)
else:
logs.debug(u'Execution timed out!', settings)
return types.Script(script)
def get_matched_rule(command, rules, settings): def get_matched_rule(command, rules, settings):
"""Returns first matched rule for command.""" """Returns first matched rule for command."""
script_only = isinstance(command, types.Script)
for rule in rules: for rule in rules:
if script_only and rule.requires_output:
continue
try: try:
with logs.debug_time(u'Trying rule: {};'.format(rule.name), with logs.debug_time(u'Trying rule: {};'.format(rule.name),
settings): settings):
@ -138,10 +152,6 @@ def main():
logs.debug(u'Run with settings: {}'.format(pformat(settings)), settings) logs.debug(u'Run with settings: {}'.format(pformat(settings)), settings)
command = get_command(settings, sys.argv) command = get_command(settings, sys.argv)
if command:
logs.debug(u'Received stdout: {}'.format(command.stdout), settings)
logs.debug(u'Received stderr: {}'.format(command.stderr), settings)
rules = get_rules(user_dir, settings) rules = get_rules(user_dir, settings)
logs.debug( logs.debug(
u'Loaded rules: {}'.format(', '.join(rule.name for rule in rules)), u'Loaded rules: {}'.format(', '.join(rule.name for rule in rules)),

View File

@ -2,10 +2,11 @@ from collections import namedtuple
Command = namedtuple('Command', ('script', 'stdout', 'stderr')) Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
Script = namedtuple('Script', ('script'))
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')) 'priority', 'requires_output'))
class RulesNamesList(list): class RulesNamesList(list):