Lint: app.py

This commit is contained in:
mrjk 2022-01-26 15:32:17 -05:00
parent ac0e4e8237
commit 4b04ef19a0

View File

@ -1,27 +1,22 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Kheops App interface"""
# import sys
# sys.path.append("/home/jez/prj/bell/training/tiger-ansible/ext/ansible-tree")
import sys import sys
import yaml import logging
import anyconfig import json
from pprint import pprint
from kheops.query import Query
from kheops.utils import schema_validate
import anyconfig
# from box import Box
from pathlib import Path from pathlib import Path
import logging import anyconfig
from kheops.utils import schema_validate
from kheops.query import Query
import kheops.plugin as KheopsPlugins
from kheops.managers import BackendsManager, RulesManager
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class App: class App:
"""Main Kheops Application Instance"""
schema = { schema = {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
@ -47,21 +42,21 @@ class App:
"properties": { "properties": {
"app": { "app": {
"type": "object", "type": "object",
#"default": {}, # "default": {},
"additionalProperties": False, "additionalProperties": False,
"properties": { "properties": {
"root": { "root": {
"default": None, "default": None,
"oneOf": [ "oneOf": [
{ {
"type": "null", "type": "null",
"description": "Application current working directory is the `kheops.yml` directory", "description": "Application current working directory is the `kheops.yml` directory",
}, },
{ {
"type": "string", "type": "string",
"description": "Application working directory. If a relative path is used, it will be depending on `kheops.yml` directory", "description": "Application working directory. If a relative path is used, it will be depending on `kheops.yml` directory",
}, },
] ],
}, },
}, },
}, },
@ -74,16 +69,16 @@ class App:
"default": None, "default": None,
"oneOf": [ "oneOf": [
{ {
"type": "null", "type": "null",
"description": "Disable prefix, all files are lookup up from the app root dir.", "description": "Disable prefix, all files are lookup up from the app root dir.",
}, },
{ {
"type": "string", "type": "string",
"description": "Add a path prefix before all paths. This is quite useful to store your YAML data in a dedicated tree.", "description": "Add a path prefix before all paths. This is quite useful to store your YAML data in a dedicated tree.",
}, },
] ],
}, },
} },
}, },
"rules": { "rules": {
"type": "object", "type": "object",
@ -118,7 +113,7 @@ class App:
try: try:
conf2 = conf2[namespace] conf2 = conf2[namespace]
except KeyError: except KeyError:
log.error(f"Can't find namespace '{namespace}' in config '{config}'") log.error("Can't find namespace '%s' in config '%s'", namespace, config)
sys.exit(1) sys.exit(1)
# Get application paths # Get application paths
@ -126,16 +121,16 @@ class App:
# Fetch app root # Fetch app root
if conf2["config"]["app"]["root"]: if conf2["config"]["app"]["root"]:
path_root = Path(conf2["config"]["app"]["root"]) path_root = Path(conf2["config"]["app"]["root"])
log.debug ("Root path is hard coded.") log.debug("Root path is hard coded.")
else: else:
path_root = Path(config).parent path_root = Path(config).parent
log.debug ("Root path guessed from conf file.") log.debug("Root path guessed from conf file.")
#path_prefix = conf2["config"]["app"]["prefix"] # path_prefix = conf2["config"]["app"]["prefix"]
#if not path_prefix: # if not path_prefix:
# path_prefix = '' # path_prefix = ''
#p = Path(path_prefix) # p = Path(path_prefix)
#if not p.is_absolute(): # if not p.is_absolute():
# p = path_root / p # p = path_root / p
# try: # try:
# p = p.resolve().relative_to(Path.cwd().resolve()) # p = p.resolve().relative_to(Path.cwd().resolve())
@ -146,16 +141,15 @@ class App:
path_cwd = str(Path.cwd().resolve()) path_cwd = str(Path.cwd().resolve())
path_root = str(path_root.resolve()) path_root = str(path_root.resolve())
self.run['path_cwd'] = path_cwd self.run["path_cwd"] = path_cwd
self.run['path_root'] = path_root self.run["path_root"] = path_root
#self.run['path_prefix'] = str(p.resolve())
log.debug (f"Working directory is {path_root} while cwd is: {path_cwd}")
# self.run['path_prefix'] = str(p.resolve())
log.debug("Working directory is %s, cwd is: %s", path_root, path_cwd)
# path_root = path_root.resolve().relative_to(Path.cwd()) # path_root = path_root.resolve().relative_to(Path.cwd())
#conf2["config"]["app"]["root"] = str(path_root) # conf2["config"]["app"]["root"] = str(path_root)
# Finish # Finish
self.conf2 = dict(conf2) self.conf2 = dict(conf2)
@ -164,43 +158,34 @@ class App:
log.debug("Root directory is: %s", path_root) log.debug("Root directory is: %s", path_root)
def lookup(self, key=None, policy=None, scope=None, trace=False, explain=False): def lookup(self, key=None, policy=None, scope=None, trace=False, explain=False):
log.debug(f"Lookup key {key} with scope: {scope}") """Lookup a key in hierarchy"""
q = Query(app=self) log.debug("Lookup key %s with scope: %s", key, scope)
r = q.exec(key=key, scope=scope, policy=policy, trace=trace, explain=explain) query = Query(app=self)
ret = query.exec(key=key, scope=scope, policy=policy, trace=trace, explain=explain)
return r return ret
#print("=== Query Result ===")
print(anyconfig.dumps(r, ac_parser=fmt))
#print("=== Query Result ===")
def dump_schema(self): def dump_schema(self):
"""Dump configuration schema"""
import json ret1 = BackendsManager.get_schema(KheopsPlugins, mode="parts")
import kheops.plugin as KheopsPlugins ret2 = RulesManager.get_schema(KheopsPlugins)
from kheops.managers import BackendsManager, RulesManager print(json.dumps(ret1, indent=2))
r1 = BackendsManager.get_schema(KheopsPlugins, mode='parts')
r2 = RulesManager.get_schema(KheopsPlugins)
#pprint (r1)
print(json.dumps(r1, indent=2))
return return
d = self.schema # ret = self.schema
d["patternProperties"][".*"]["properties"]["tree"]["items"]["properties"] = r1 # ret["patternProperties"][".*"]["properties"]["tree"]["items"]["properties"] = ret1
d["patternProperties"][".*"]["properties"]["tree"]["items"] = r2 # ret["patternProperties"][".*"]["properties"]["tree"]["items"] = ret2
print(json.dumps(d, indent=2))
# print(json.dumps(ret, indent=2))
def gen_docs(self): def gen_docs(self):
""" Generate documentation"""
import json
import kheops.plugin as KheopsPlugins
from kheops.managers import BackendsManager, RulesManager
print ("WIP")
#src = { print("WIP")
return None
# src = {
# "app": { # "app": {
# "config_schema": None, # "config_schema": None,
# "plugin_managers": { # "plugin_managers": {
@ -208,25 +193,20 @@ class App:
# 'rules': None, # 'rules': None,
# } # }
# } # }
# #
#r1 = BackendsManager.get_schema(KheopsPlugins, mode='parts') # r1 = BackendsManager.get_schema(KheopsPlugins, mode='parts')
# print (json.dumps(r1, indent=2))
#print (json.dumps(r1, indent=2)) # ret = {
#
#ret = {
#
# } # }
#part_config = r1.get('config_schema', None) # part_config = r1.get('config_schema', None)
#part_item = r1['items']['core_schema'] # part_item = r1['items']['core_schema']
#part_item_plugins = r1['items']['plugin'] # part_item_plugins = r1['items']['plugin']
#for kind, plugins in part_item_plugins.items(): # for kind, plugins in part_item_plugins.items():
# for plugin_name, schema in plugins.items(): # for plugin_name, schema in plugins.items():
# part_item_ # part_item_