#334: Don't wait for all rules before showing result

This commit is contained in:
nvbn
2015-09-01 12:51:41 +03:00
parent ebe53f0d18
commit 12394ca842
7 changed files with 132 additions and 46 deletions
+3 -19
View File
@@ -2,7 +2,6 @@ import sys
from imp import load_source
from pathlib import Path
from . import conf, types, logs
from .utils import eager
def load_rule(rule, settings):
@@ -27,17 +26,16 @@ def get_loaded_rules(rules, settings):
yield loaded_rule
@eager
def get_rules(user_dir, settings):
"""Returns all enabled rules."""
bundled = Path(__file__).parent \
.joinpath('rules') \
.glob('*.py')
user = user_dir.joinpath('rules').glob('*.py')
return get_loaded_rules(sorted(bundled) + sorted(user), settings)
return sorted(get_loaded_rules(sorted(bundled) + sorted(user), settings),
key=lambda rule: rule.priority)
@eager
def get_matched_rules(command, rules, settings):
"""Returns first matched rule for command."""
script_only = command.stdout is None and command.stderr is None
@@ -66,22 +64,8 @@ def make_corrected_commands(command, rules, settings):
priority=(n + 1) * rule.priority)
def remove_duplicates(corrected_commands):
commands = {(command.script, command.side_effect): command
for command in sorted(corrected_commands,
key=lambda command: -command.priority)}
return commands.values()
def get_corrected_commands(command, user_dir, settings):
rules = get_rules(user_dir, settings)
logs.debug(
u'Loaded rules: {}'.format(', '.join(rule.name for rule in rules)),
settings)
matched = get_matched_rules(command, rules, settings)
logs.debug(
u'Matched rules: {}'.format(', '.join(rule.name for rule in matched)),
settings)
corrected_commands = make_corrected_commands(command, matched, settings)
return sorted(remove_duplicates(corrected_commands),
key=lambda corrected_command: corrected_command.priority)
return types.SortedCorrectedCommandsSequence(corrected_commands, settings)