kheops/albero/app.py

107 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
# import sys
# sys.path.append("/home/jez/prj/bell/training/tiger-ansible/ext/ansible-tree")
import sys
import yaml
import anyconfig
from pprint import pprint
from albero.query import Query
from albero.utils import schema_validate
import anyconfig
# from box import Box
from pathlib import Path
import logging
log = logging.getLogger(__name__)
class App():
schema = {
"$schema": 'http://json-schema.org/draft-04/schema#',
"type": "object",
"additionalProperties": False,
"default": {},
"patternProperties": {
".*": {
"type": "object",
"optional": True,
"additionalProperties": False,
"properties": {
"config": {
"type": "object",
"default": {},
"additionalProperties": False,
"properties": {
"app": {
"type": "object",
"default": {},
"additionalProperties": False,
"properties": {
"root": {
"type": "string",
"default": None,
},
},
},
"tree": {
#"additionalProperties": False,
"type": "object",
"default": {},
},
"rules": {
"type": "object",
"default": {},
},
},
},
"tree": {
"type": "array",
"default": [],
},
"rules": {
"type": "array",
"default": [],
},
},
},
}
}
def __init__(self, config="albero.yml", namespace='default'):
conf2 = anyconfig.load(config)
# Validate configuration
schema_validate(conf2, self.schema)
try:
conf2 = conf2[namespace]
except KeyError:
log.error (f"Can't find namespace '{namespace}' in config '{config}'")
sys.exit(1)
# Init
if not conf2['config']['app']['root']:
conf2['config']['app']['root'] = Path(config).parent
else:
conf2['config']['app']['root'] = Path(conf2['config']['app']['root'])
# Finish
self.conf2 = dict(conf2)
def lookup(self, key=None, policy=None, scope=None, trace=False, explain=False):
log.debug(f"Lookup key {key} with scope: {scope}")
q = Query(app = self)
r = q.exec(key=key, scope=scope , policy=policy, trace=trace, explain=explain)
print ("=== Query Result ===")
print(anyconfig.dumps(r, ac_parser='yaml'))
print ("=== Query Result ===")