#334: Don't wait for all rules before showing result
This commit is contained in:
+3
-19
@@ -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)
|
||||
|
||||
+72
-2
@@ -1,5 +1,6 @@
|
||||
from collections import namedtuple
|
||||
|
||||
from traceback import format_stack
|
||||
from .logs import debug
|
||||
|
||||
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
||||
|
||||
@@ -18,7 +19,6 @@ class RulesNamesList(list):
|
||||
|
||||
|
||||
class Settings(dict):
|
||||
|
||||
def __getattr__(self, item):
|
||||
return self.get(item)
|
||||
|
||||
@@ -29,3 +29,73 @@ class Settings(dict):
|
||||
conf = dict(kwargs)
|
||||
conf.update(self)
|
||||
return Settings(conf)
|
||||
|
||||
|
||||
class SortedCorrectedCommandsSequence(object):
|
||||
"""List-like collection/wrapper around generator, that:
|
||||
|
||||
- immediately gives access to the first commands through [];
|
||||
- realises generator and sorts commands on first access to other
|
||||
commands through [], or when len called.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, commands, settings):
|
||||
self._settings = settings
|
||||
self._commands = commands
|
||||
self._cached = self._get_first_two_unique()
|
||||
self._realised = False
|
||||
|
||||
def _get_first_two_unique(self):
|
||||
"""Returns first two unique commands."""
|
||||
try:
|
||||
first = next(self._commands)
|
||||
except StopIteration:
|
||||
return []
|
||||
|
||||
for command in self._commands:
|
||||
if command.script != first.script or \
|
||||
command.side_effect != first.side_effect:
|
||||
return [first, command]
|
||||
return [first]
|
||||
|
||||
def _remove_duplicates(self, corrected_commands):
|
||||
"""Removes low-priority duplicates."""
|
||||
commands = {(command.script, command.side_effect): command
|
||||
for command in sorted(corrected_commands,
|
||||
key=lambda command: -command.priority)
|
||||
if command.script != self._cached[0].script
|
||||
or command.side_effect != self._cached[0].side_effect}
|
||||
return commands.values()
|
||||
|
||||
def _realise(self):
|
||||
"""Realises generator, removes duplicates and sorts commands."""
|
||||
commands = self._cached[1:] + list(self._commands)
|
||||
commands = self._remove_duplicates(commands)
|
||||
self._cached = [self._cached[0]] + sorted(
|
||||
commands, key=lambda corrected_command: corrected_command.priority)
|
||||
self._realised = True
|
||||
debug('SortedCommandsSequence was realised with: {}, after: {}'.format(
|
||||
self._cached, '\n'.join(format_stack())), self._settings)
|
||||
|
||||
def __getitem__(self, item):
|
||||
if item != 0 and not self._realised:
|
||||
self._realise()
|
||||
return self._cached[item]
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self._cached)
|
||||
|
||||
def __len__(self):
|
||||
if not self._realised:
|
||||
self._realise()
|
||||
return len(self._cached)
|
||||
|
||||
def __iter__(self):
|
||||
if not self._realised:
|
||||
self._realise()
|
||||
return iter(self._cached)
|
||||
|
||||
@property
|
||||
def is_multiple(self):
|
||||
return len(self._cached) > 1
|
||||
|
||||
+3
-3
@@ -88,9 +88,9 @@ def select_command(corrected_commands, settings):
|
||||
logs.show_corrected_command(selector.value, settings)
|
||||
return selector.value
|
||||
|
||||
multiple_cmds = len(corrected_commands) > 1
|
||||
|
||||
selector.on_change(lambda val: logs.confirm_text(val, multiple_cmds, settings))
|
||||
selector.on_change(
|
||||
lambda val: logs.confirm_text(val, corrected_commands.is_multiple,
|
||||
settings))
|
||||
for action in read_actions():
|
||||
if action == SELECT:
|
||||
sys.stderr.write('\n')
|
||||
|
||||
Reference in New Issue
Block a user