Move rule-related code to Rule
This commit is contained in:
+10
-63
@@ -1,45 +1,16 @@
|
||||
import sys
|
||||
from imp import load_source
|
||||
from pathlib import Path
|
||||
from .conf import settings, DEFAULT_PRIORITY, ALL_ENABLED
|
||||
from .types import Rule, CorrectedCommand
|
||||
from .utils import compatibility_call
|
||||
from .conf import settings
|
||||
from .types import Rule
|
||||
from . import logs
|
||||
|
||||
|
||||
def load_rule(rule):
|
||||
"""Imports rule module and returns it."""
|
||||
name = rule.name[:-3]
|
||||
with logs.debug_time(u'Importing rule: {};'.format(name)):
|
||||
rule_module = load_source(name, str(rule))
|
||||
priority = getattr(rule_module, 'priority', DEFAULT_PRIORITY)
|
||||
return Rule(name, rule_module.match,
|
||||
rule_module.get_new_command,
|
||||
getattr(rule_module, 'enabled_by_default', True),
|
||||
getattr(rule_module, 'side_effect', None),
|
||||
settings.priority.get(name, priority),
|
||||
getattr(rule_module, 'requires_output', True))
|
||||
|
||||
|
||||
def is_rule_enabled(rule):
|
||||
"""Returns `True` when rule enabled."""
|
||||
if rule.name in settings.exclude_rules:
|
||||
return False
|
||||
elif rule.name in settings.rules:
|
||||
return True
|
||||
elif rule.enabled_by_default and ALL_ENABLED in settings.rules:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def get_loaded_rules(rules):
|
||||
def get_loaded_rules(rules_paths):
|
||||
"""Yields all available rules."""
|
||||
for rule in rules:
|
||||
if rule.name != '__init__.py':
|
||||
loaded_rule = load_rule(rule)
|
||||
if is_rule_enabled(loaded_rule):
|
||||
yield loaded_rule
|
||||
for path in rules_paths:
|
||||
if path.name != '__init__.py':
|
||||
rule = Rule.from_path(path)
|
||||
if rule.is_enabled:
|
||||
yield rule
|
||||
|
||||
|
||||
def get_rules():
|
||||
@@ -52,30 +23,6 @@ def get_rules():
|
||||
key=lambda rule: rule.priority)
|
||||
|
||||
|
||||
def is_rule_match(command, rule):
|
||||
"""Returns first matched rule for command."""
|
||||
script_only = command.stdout is None and command.stderr is None
|
||||
|
||||
if script_only and rule.requires_output:
|
||||
return False
|
||||
|
||||
try:
|
||||
with logs.debug_time(u'Trying rule: {};'.format(rule.name)):
|
||||
if compatibility_call(rule.match, command):
|
||||
return True
|
||||
except Exception:
|
||||
logs.rule_failed(rule, sys.exc_info())
|
||||
|
||||
|
||||
def make_corrected_commands(command, rule):
|
||||
new_commands = compatibility_call(rule.get_new_command, command)
|
||||
if not isinstance(new_commands, list):
|
||||
new_commands = (new_commands,)
|
||||
for n, new_command in enumerate(new_commands):
|
||||
yield CorrectedCommand(script=new_command,
|
||||
side_effect=rule.side_effect,
|
||||
priority=(n + 1) * rule.priority)
|
||||
|
||||
def organize_commands(corrected_commands):
|
||||
"""Yields sorted commands without duplicates."""
|
||||
try:
|
||||
@@ -103,6 +50,6 @@ def organize_commands(corrected_commands):
|
||||
def get_corrected_commands(command):
|
||||
corrected_commands = (
|
||||
corrected for rule in get_rules()
|
||||
if is_rule_match(command, rule)
|
||||
for corrected in make_corrected_commands(command, rule))
|
||||
if rule.is_match(command)
|
||||
for corrected in rule.get_corrected_commands(command))
|
||||
return organize_commands(corrected_commands)
|
||||
|
||||
Reference in New Issue
Block a user