Lint: some parts of the code
This commit is contained in:
parent
2707e3ae76
commit
ffdbfc442b
@ -45,7 +45,6 @@ CONF_SCHEMA = {
|
|||||||
"default": [],
|
"default": [],
|
||||||
# "arrayItem": { "$ref": "#/$defs/rules_items" },
|
# "arrayItem": { "$ref": "#/$defs/rules_items" },
|
||||||
},
|
},
|
||||||
|
|
||||||
"config": {
|
"config": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"default": {},
|
"default": {},
|
||||||
@ -109,6 +108,7 @@ class KheopsNamespace(GenericInstance, QueryProcessor):
|
|||||||
Kheops Namespace Class
|
Kheops Namespace Class
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, app, name, config=None):
|
def __init__(self, app, name, config=None):
|
||||||
"""
|
"""
|
||||||
Kheops Namespace Instance
|
Kheops Namespace Instance
|
||||||
@ -130,11 +130,11 @@ class KheopsNamespace(GenericInstance, QueryProcessor):
|
|||||||
self.cache = app.cache
|
self.cache = app.cache
|
||||||
|
|
||||||
# Init config (from cache)
|
# Init config (from cache)
|
||||||
config_hash = 'conf_ns_' + dict_hash(config)
|
config_hash = "conf_ns_" + dict_hash(config)
|
||||||
try:
|
try:
|
||||||
config = self.cache[config_hash]
|
config = self.cache[config_hash]
|
||||||
log.debug("Loading namespace '%s' configuration from cache", self.name)
|
log.debug("Loading namespace '%s' configuration from cache", self.name)
|
||||||
except KeyError as err:
|
except KeyError:
|
||||||
config = schema_validate(config, CONF_SCHEMA)
|
config = schema_validate(config, CONF_SCHEMA)
|
||||||
self.cache.set(config_hash, config, expire=CACHE_CONFIG_EXPIRE)
|
self.cache.set(config_hash, config, expire=CACHE_CONFIG_EXPIRE)
|
||||||
super().__init__(config)
|
super().__init__(config)
|
||||||
@ -186,7 +186,6 @@ class Kheops(GenericInstance):
|
|||||||
# self.raw_config = self.parse_conf(config)
|
# self.raw_config = self.parse_conf(config)
|
||||||
# self.cache.set(needle, config, expire=CACHE_CONFIG_EXPIRE)
|
# self.cache.set(needle, config, expire=CACHE_CONFIG_EXPIRE)
|
||||||
|
|
||||||
|
|
||||||
def parse_conf(self, config="kheops.yml"):
|
def parse_conf(self, config="kheops.yml"):
|
||||||
"""
|
"""
|
||||||
Parse Kheops configuration
|
Parse Kheops configuration
|
||||||
@ -209,12 +208,13 @@ class Kheops(GenericInstance):
|
|||||||
elif isinstance(config, dict):
|
elif isinstance(config, dict):
|
||||||
dict_conf = config
|
dict_conf = config
|
||||||
source = "dict"
|
source = "dict"
|
||||||
|
|
||||||
|
self.run["conf_source"] = source
|
||||||
return dict_conf
|
return dict_conf
|
||||||
|
|
||||||
def lookup(
|
def lookup(
|
||||||
self,
|
self,
|
||||||
keys=None,
|
keys=None,
|
||||||
policy=None,
|
|
||||||
scope=None,
|
scope=None,
|
||||||
trace=False,
|
trace=False,
|
||||||
explain=False,
|
explain=False,
|
||||||
@ -240,7 +240,9 @@ class Kheops(GenericInstance):
|
|||||||
for key_def in keys:
|
for key_def in keys:
|
||||||
|
|
||||||
key_def = key_def or ""
|
key_def = key_def or ""
|
||||||
assert isinstance(key_def, str), f"Expected string as key, got {type(key_def)}: {key_def}"
|
assert isinstance(
|
||||||
|
key_def, str
|
||||||
|
), f"Expected string as key, got {type(key_def)}: {key_def}"
|
||||||
|
|
||||||
# Identify namespace and key
|
# Identify namespace and key
|
||||||
parts = key_def.split("/")
|
parts = key_def.split("/")
|
||||||
@ -267,7 +269,7 @@ class Kheops(GenericInstance):
|
|||||||
|
|
||||||
# Prepare output
|
# Prepare output
|
||||||
_key = key_name
|
_key = key_name
|
||||||
if namespace_prefix == True:
|
if namespace_prefix is True:
|
||||||
_key = key_def
|
_key = key_def
|
||||||
ret[_key] = result
|
ret[_key] = result
|
||||||
|
|
||||||
@ -285,12 +287,6 @@ class Kheops(GenericInstance):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# To clean/implement
|
# To clean/implement
|
||||||
|
|
||||||
# def DEPRECATED_dump_schema(self):
|
# def DEPRECATED_dump_schema(self):
|
||||||
|
|||||||
@ -24,6 +24,7 @@ CACHE_QUERY_EXPIRE = 10
|
|||||||
# Helper classes
|
# Helper classes
|
||||||
# ------------------------
|
# ------------------------
|
||||||
|
|
||||||
|
|
||||||
class LoadPlugin:
|
class LoadPlugin:
|
||||||
"""Kheops plugins loader
|
"""Kheops plugins loader
|
||||||
|
|
||||||
@ -60,7 +61,8 @@ class LoadPlugin:
|
|||||||
# Return plugin Classe
|
# Return plugin Classe
|
||||||
return plugin_cls.Plugin
|
return plugin_cls.Plugin
|
||||||
|
|
||||||
class BackendCandidate():
|
|
||||||
|
class BackendCandidate:
|
||||||
"""Backend Candidate
|
"""Backend Candidate
|
||||||
|
|
||||||
This object represents a backend candidate. It holds the value of the
|
This object represents a backend candidate. It holds the value of the
|
||||||
@ -97,6 +99,7 @@ class Query:
|
|||||||
# Query Processor class
|
# Query Processor class
|
||||||
# ------------------------
|
# ------------------------
|
||||||
|
|
||||||
|
|
||||||
class QueryProcessor:
|
class QueryProcessor:
|
||||||
"""QueryProcessor
|
"""QueryProcessor
|
||||||
|
|
||||||
@ -134,9 +137,7 @@ class QueryProcessor:
|
|||||||
# ------------------------
|
# ------------------------
|
||||||
|
|
||||||
def query(self, key=None, scope=None, explain=False):
|
def query(self, key=None, scope=None, explain=False):
|
||||||
"""Query key with scope
|
"""Query key with scope"""
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Look into cache
|
# Look into cache
|
||||||
query_hash = dict_hash([self.name, key, scope])
|
query_hash = dict_hash([self.name, key, scope])
|
||||||
@ -195,7 +196,6 @@ class QueryProcessor:
|
|||||||
self.cache.set(query_hash, result, expire=CACHE_QUERY_EXPIRE)
|
self.cache.set(query_hash, result, expire=CACHE_QUERY_EXPIRE)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
# Query parts methods
|
# Query parts methods
|
||||||
# ------------------------
|
# ------------------------
|
||||||
|
|
||||||
@ -218,7 +218,6 @@ class QueryProcessor:
|
|||||||
|
|
||||||
return rule
|
return rule
|
||||||
|
|
||||||
|
|
||||||
def _exec_assemble_lookups(self, lookups, query):
|
def _exec_assemble_lookups(self, lookups, query):
|
||||||
|
|
||||||
assert isinstance(lookups, list)
|
assert isinstance(lookups, list)
|
||||||
@ -258,7 +257,9 @@ class QueryProcessor:
|
|||||||
plugin_name = plugin_def.get("module", None)
|
plugin_name = plugin_def.get("module", None)
|
||||||
|
|
||||||
if plugin_name:
|
if plugin_name:
|
||||||
plugin = self.plugin_loader.load("scope", plugin_name)(namespace=self)
|
plugin = self.plugin_loader.load("scope", plugin_name)(
|
||||||
|
namespace=self
|
||||||
|
)
|
||||||
ret = plugin.process_items(ret, plugin_def)
|
ret = plugin.process_items(ret, plugin_def)
|
||||||
|
|
||||||
new_lookups2.extend(ret)
|
new_lookups2.extend(ret)
|
||||||
@ -274,11 +275,12 @@ class QueryProcessor:
|
|||||||
lookup["path"] = new_path
|
lookup["path"] = new_path
|
||||||
new_lookups3.append(lookup)
|
new_lookups3.append(lookup)
|
||||||
else:
|
else:
|
||||||
log.warning("Ignore lookup item because of missing scope vars: '%s'", path)
|
log.warning(
|
||||||
|
"Ignore lookup item because of missing scope vars: '%s'", path
|
||||||
|
)
|
||||||
|
|
||||||
return new_lookups3
|
return new_lookups3
|
||||||
|
|
||||||
|
|
||||||
def _exec_backend_plugins(self, lookups, selector="matched"):
|
def _exec_backend_plugins(self, lookups, selector="matched"):
|
||||||
selector = "matched"
|
selector = "matched"
|
||||||
assert selector in ["last", "first", "all", "matched"]
|
assert selector in ["last", "first", "all", "matched"]
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
# from pprint import pprint
|
# from pprint import pprint
|
||||||
|
|
||||||
import anyconfig
|
import anyconfig
|
||||||
@ -11,6 +12,7 @@ from kheops.plugin.common import BackendPlugin, BackendCandidate
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
CACHE_FILE_EXPIRE = 5
|
CACHE_FILE_EXPIRE = 5
|
||||||
|
|
||||||
|
|
||||||
class Plugin(BackendPlugin):
|
class Plugin(BackendPlugin):
|
||||||
"""File Backend Plugin
|
"""File Backend Plugin
|
||||||
|
|
||||||
@ -41,13 +43,12 @@ class Plugin(BackendPlugin):
|
|||||||
to find all of the specified format. It is better to keep this list as small
|
to find all of the specified format. It is better to keep this list as small
|
||||||
as possible.
|
as possible.
|
||||||
""",
|
""",
|
||||||
|
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"default": extensions,
|
"default": extensions,
|
||||||
"additionalProperties": {
|
"additionalProperties": {
|
||||||
"title": "Name of the extension with parser",
|
"title": "Name of the extension with parser",
|
||||||
"type": "string"
|
"type": "string",
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
"path_prefix": {
|
"path_prefix": {
|
||||||
"title": "Prefix string to append to final path",
|
"title": "Prefix string to append to final path",
|
||||||
@ -55,7 +56,7 @@ class Plugin(BackendPlugin):
|
|||||||
String to be added at the end of the resolved path. This is useful to change
|
String to be added at the end of the resolved path. This is useful to change
|
||||||
the place of the root hierarchy.
|
the place of the root hierarchy.
|
||||||
""",
|
""",
|
||||||
"type": "string"
|
"type": "string",
|
||||||
},
|
},
|
||||||
"path_suffix": {
|
"path_suffix": {
|
||||||
"title": "Suffix string to prepend to final path",
|
"title": "Suffix string to prepend to final path",
|
||||||
@ -65,12 +66,11 @@ class Plugin(BackendPlugin):
|
|||||||
"type": "string",
|
"type": "string",
|
||||||
"examples": [
|
"examples": [
|
||||||
{"path_suffix": "/ansible"},
|
{"path_suffix": "/ansible"},
|
||||||
]
|
],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
_schema_props_new = {
|
_schema_props_new = {
|
||||||
"path": {
|
"path": {
|
||||||
|
|||||||
@ -22,13 +22,16 @@ NoneType = type(None)
|
|||||||
# Generic Plugin classes
|
# Generic Plugin classes
|
||||||
# -------------------------
|
# -------------------------
|
||||||
|
|
||||||
|
|
||||||
class KheopsPlugin:
|
class KheopsPlugin:
|
||||||
plugin_name = None
|
plugin_name = None
|
||||||
plugin_type = None
|
plugin_type = None
|
||||||
plugin_kind = None
|
plugin_kind = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
assert isinstance(self.plugin_name, str), f"Missing name attribute in plugin: {self.__class__}"
|
assert isinstance(
|
||||||
|
self.plugin_name, str
|
||||||
|
), f"Missing name attribute in plugin: {self.__class__}"
|
||||||
assert isinstance(self.plugin_kind, str)
|
assert isinstance(self.plugin_kind, str)
|
||||||
|
|
||||||
config_key = f"{self.plugin_kind}_{self.plugin_name}"
|
config_key = f"{self.plugin_kind}_{self.plugin_name}"
|
||||||
@ -61,6 +64,7 @@ class KheopsItemPlugin(KheopsPlugin):
|
|||||||
# Plugin classes
|
# Plugin classes
|
||||||
# -------------------------
|
# -------------------------
|
||||||
|
|
||||||
|
|
||||||
class BackendPlugin(KheopsItemPlugin):
|
class BackendPlugin(KheopsItemPlugin):
|
||||||
plugin_kind = "backend"
|
plugin_kind = "backend"
|
||||||
|
|
||||||
@ -140,11 +144,14 @@ class ScopePlugin(KheopsListPlugin):
|
|||||||
self.ns = namespace
|
self.ns = namespace
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
|
||||||
# Helper classes
|
# Helper classes
|
||||||
# -------------------------
|
# -------------------------
|
||||||
|
|
||||||
class BackendCandidate():
|
|
||||||
|
class BackendCandidate:
|
||||||
"""Represent a backend candidate"""
|
"""Represent a backend candidate"""
|
||||||
|
|
||||||
def __init__(self, path=None, data=None, run=None, status=None):
|
def __init__(self, path=None, data=None, run=None, status=None):
|
||||||
assert isinstance(run, dict)
|
assert isinstance(run, dict)
|
||||||
self.path = path
|
self.path = path
|
||||||
@ -156,7 +163,6 @@ class BackendCandidate():
|
|||||||
return f"Status: {self.status}, Path: {self.path} => {self.data}"
|
return f"Status: {self.status}, Path: {self.path} => {self.data}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ScopeExtLoop:
|
class ScopeExtLoop:
|
||||||
"""This Scope Extension allow to loop over a lookup"""
|
"""This Scope Extension allow to loop over a lookup"""
|
||||||
|
|
||||||
@ -179,10 +185,15 @@ class ScopeExtLoop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
def loop_over(
|
def loop_over(
|
||||||
self, lookups, conf, module_name, var_name="item", callback_context=None, callback=None
|
self,
|
||||||
|
lookups,
|
||||||
|
conf,
|
||||||
|
module_name,
|
||||||
|
var_name="item",
|
||||||
|
callback_context=None,
|
||||||
|
callback=None,
|
||||||
):
|
):
|
||||||
|
|
||||||
|
|
||||||
var_name = conf.get("var", var_name)
|
var_name = conf.get("var", var_name)
|
||||||
var_data_ref = conf.get("data", None)
|
var_data_ref = conf.get("data", None)
|
||||||
|
|
||||||
@ -235,8 +246,6 @@ class ScopeExtLoop:
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# To clean/implement
|
# To clean/implement
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -77,6 +77,7 @@ class Default(dict):
|
|||||||
def __missing__(self, key):
|
def __missing__(self, key):
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
# Source: https://www.doc.ic.ac.uk/~nuric/coding/how-to-hash-a-dictionary-in-python.html
|
# Source: https://www.doc.ic.ac.uk/~nuric/coding/how-to-hash-a-dictionary-in-python.html
|
||||||
def dict_hash(dictionary: Dict[str, Any]) -> str:
|
def dict_hash(dictionary: Dict[str, Any]) -> str:
|
||||||
"""MD5 hash of a dictionary."""
|
"""MD5 hash of a dictionary."""
|
||||||
@ -104,6 +105,7 @@ def render_template_python(text, params, ignore_missing=True):
|
|||||||
# Schema Methods
|
# Schema Methods
|
||||||
# =====================
|
# =====================
|
||||||
|
|
||||||
|
|
||||||
def _extend_with_default(validator_class):
|
def _extend_with_default(validator_class):
|
||||||
validate_properties = validator_class.VALIDATORS["properties"]
|
validate_properties = validator_class.VALIDATORS["properties"]
|
||||||
|
|
||||||
@ -113,12 +115,16 @@ def _extend_with_default(validator_class):
|
|||||||
instance.setdefault(property, subschema["default"])
|
instance.setdefault(property, subschema["default"])
|
||||||
|
|
||||||
for error in validate_properties(
|
for error in validate_properties(
|
||||||
validator, properties, instance, schema,
|
validator,
|
||||||
|
properties,
|
||||||
|
instance,
|
||||||
|
schema,
|
||||||
):
|
):
|
||||||
yield error
|
yield error
|
||||||
|
|
||||||
return validators.extend(
|
return validators.extend(
|
||||||
validator_class, {"properties" : set_defaults},
|
validator_class,
|
||||||
|
{"properties": set_defaults},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user