Lint: managers.py
This commit is contained in:
parent
0af7ba8598
commit
b01baf44ad
@ -1,8 +1,8 @@
|
||||
import dpath.util
|
||||
"""Manager Classes"""
|
||||
|
||||
import logging
|
||||
from pprint import pprint
|
||||
|
||||
import dpath.util
|
||||
|
||||
from kheops.utils import schema_validate
|
||||
import kheops.plugin as KheopsPlugins
|
||||
@ -11,6 +11,8 @@ log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LoadPlugin:
|
||||
"""Generic class to load plugins"""
|
||||
|
||||
def __init__(self, plugins):
|
||||
self.plugins = plugins
|
||||
|
||||
@ -21,14 +23,14 @@ class LoadPlugin:
|
||||
# Get plugin kind
|
||||
try:
|
||||
plugins = getattr(self.plugins, kind)
|
||||
except Exception as e:
|
||||
raise Exception(f"Unknown module kind '{kind}': {e}")
|
||||
except Exception as err:
|
||||
raise Exception(f"Unknown module kind '{kind}': {err}")
|
||||
|
||||
# Get plugin class
|
||||
try:
|
||||
plugin_cls = getattr(plugins, name)
|
||||
except Exception as e:
|
||||
raise Exception(f"Unknown module '{kind}.{name}': {e}")
|
||||
except Exception as err:
|
||||
raise Exception(f"Unknown module '{kind}.{name}': {err}")
|
||||
|
||||
assert hasattr(
|
||||
plugin_cls, "Plugin"
|
||||
@ -41,24 +43,24 @@ class LoadPlugin:
|
||||
class Manager:
|
||||
"""Generic manager class"""
|
||||
|
||||
_app_kind = 'core'
|
||||
_app_kind = "core"
|
||||
plugins_kind = []
|
||||
_schema_props_default = None
|
||||
_schema_props_new = None
|
||||
_props_position = None
|
||||
|
||||
@classmethod
|
||||
def get_schema(cls, plugins_db, mode='full'):
|
||||
def get_schema(cls, plugins_db, mode="full"):
|
||||
"""Retrieve configuration schema"""
|
||||
|
||||
# Properties
|
||||
ret = {
|
||||
"core_schema": {},
|
||||
"plugin": {},
|
||||
}
|
||||
"core_schema": {},
|
||||
"plugin": {},
|
||||
}
|
||||
ret3 = {}
|
||||
for kind in cls.plugins_kind:
|
||||
ret['plugin'][kind] = {}
|
||||
ret["plugin"][kind] = {}
|
||||
plugin_kind = getattr(plugins_db, kind)
|
||||
|
||||
for plugin_name in [i for i in dir(plugin_kind) if not i.startswith("_")]:
|
||||
@ -69,7 +71,7 @@ class Manager:
|
||||
plugin_cls, "_schema_props_new", "MISSING ITEM"
|
||||
)
|
||||
if schema_props:
|
||||
ret['plugin'][kind][plugin_name + '_schema' ] = schema_props
|
||||
ret["plugin"][kind][plugin_name + "_schema"] = schema_props
|
||||
ret3.update(schema_props)
|
||||
ret3.update(cls._schema_props_new)
|
||||
|
||||
@ -77,15 +79,15 @@ class Manager:
|
||||
ret1 = cls._schema_props_default
|
||||
position = cls._props_position
|
||||
dpath.util.set(ret1, position, ret3)
|
||||
ret['core_schema'] = cls._schema_props_new
|
||||
ret["core_schema"] = cls._schema_props_new
|
||||
|
||||
if mode == 'full':
|
||||
if mode == "full":
|
||||
return ret1
|
||||
|
||||
ret4 = {
|
||||
"config_schema": {},
|
||||
"items": ret,
|
||||
}
|
||||
"config_schema": {},
|
||||
"items": ret,
|
||||
}
|
||||
|
||||
return ret4
|
||||
|
||||
@ -93,7 +95,7 @@ class Manager:
|
||||
class BackendsManager(Manager):
|
||||
"""Backend Manager"""
|
||||
|
||||
_app_kind = 'manager'
|
||||
_app_kind = "manager"
|
||||
plugins_kind = ["engine", "backend"]
|
||||
|
||||
_schema_props_new = {
|
||||
@ -168,7 +170,7 @@ class BackendsManager(Manager):
|
||||
return ret
|
||||
|
||||
def get_backends(self, key=None, scope=None, trace=False):
|
||||
log.debug(f"Look for candidates for key '{key}' in backend: {self.backends}")
|
||||
log.debug("Look for candidates for key '%s' in backend: %s", key, self.backends)
|
||||
|
||||
# Prepare plugins
|
||||
plugin_loader = LoadPlugin(KheopsPlugins)
|
||||
@ -179,12 +181,12 @@ class BackendsManager(Manager):
|
||||
|
||||
# Preprocess backends plugins
|
||||
backends = self.config_items
|
||||
log.debug(f"Backend preprocessing of {len(backends)} elements")
|
||||
log.debug("Backend preprocessing of %s elements", len(backends))
|
||||
for plugin in self.plugins:
|
||||
# backend_cls = plugin_loader.load('backend', plugin)
|
||||
plugin = plugin_loader.load("backend", plugin)()
|
||||
|
||||
log.debug(f"Run {plugin}")
|
||||
log.debug("Run %s", plugin)
|
||||
new_backend, _run = plugin.process(backends, _run)
|
||||
|
||||
assert isinstance(new_backend, list), f"Got: {new_backend}"
|
||||
@ -195,10 +197,11 @@ class BackendsManager(Manager):
|
||||
for i in backends:
|
||||
assert i.get("engine"), f"Got: {i}"
|
||||
|
||||
log.debug(f"Backend preprocessing made {len(backends)} elements")
|
||||
log.debug("Backend preprocessing made %s elements", len(backends))
|
||||
return backends
|
||||
|
||||
def get_results(self, backends, trace=False):
|
||||
"""Return results"""
|
||||
|
||||
# Prepare plugins
|
||||
plugin_loader = LoadPlugin(KheopsPlugins)
|
||||
@ -213,7 +216,7 @@ class BackendsManager(Manager):
|
||||
backend, parent=self, app=self.app
|
||||
)
|
||||
|
||||
log.debug(f"Run engine: {engine}")
|
||||
log.debug("Run engine: %s", engine)
|
||||
new_result = engine.process()
|
||||
|
||||
assert isinstance(new_result, list), f"Got: {new_result}"
|
||||
@ -226,8 +229,9 @@ class BackendsManager(Manager):
|
||||
|
||||
|
||||
class RulesManager(Manager):
|
||||
"""Rule Manager Class"""
|
||||
|
||||
_app_kind = 'rules'
|
||||
_app_kind = "rules"
|
||||
plugins_kind = ["strategy"]
|
||||
|
||||
_schema_props_new = {
|
||||
@ -313,13 +317,15 @@ class RulesManager(Manager):
|
||||
self.config_items = list(app.conf2["rules"])
|
||||
|
||||
def get_result(self, candidates, key=None, scope=None, trace=False, explain=False):
|
||||
"""Return query results"""
|
||||
|
||||
# trace=False
|
||||
|
||||
rules = self.config_items
|
||||
key = key or ""
|
||||
|
||||
# Filter out invalid candidates
|
||||
matched_candidates = [i for i in candidates if i["found"] == True]
|
||||
matched_candidates = [i for i in candidates if i["found"] is True]
|
||||
if len(matched_candidates) == 0:
|
||||
log.debug("No matched candidates")
|
||||
return None
|
||||
@ -332,10 +338,10 @@ class RulesManager(Manager):
|
||||
|
||||
rule = [i for i in rules if i.get("rule") == key]
|
||||
if len(rule) == 0:
|
||||
log.debug(f"No matched rule for %s, applying defaults", key)
|
||||
log.debug("No matched rule for %s, applying defaults", key)
|
||||
else:
|
||||
matched_rule = rule[0]
|
||||
log.debug(f"Matcher rule for {key}: {matched_rule}")
|
||||
log.debug("Matcher rule for %s: %s", key, matched_rule)
|
||||
|
||||
matched_rule["trace"] = trace
|
||||
matched_rule["explain"] = explain
|
||||
@ -345,10 +351,10 @@ class RulesManager(Manager):
|
||||
assert isinstance(matched_candidates, list), f"Got: {matched_candidates}"
|
||||
assert isinstance(matched_rule, dict), f"Got: {matched_rule}"
|
||||
strategy = matched_rule.get("strategy", "schema")
|
||||
log.debug(f"Key '{key}' matched rule '{rule}' with '{strategy}' strategy")
|
||||
log.debug("Key '%s' matched rule '%s' with '%s' strategy", key, rule, strategy)
|
||||
|
||||
# Load plugin
|
||||
log.debug(f"Run strategy: {strategy}")
|
||||
log.debug("Run strategy: %s", strategy)
|
||||
plugin_loader = LoadPlugin(KheopsPlugins)
|
||||
strategy = plugin_loader.load(
|
||||
"strategy",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user