Lint: utils.py
This commit is contained in:
parent
4b04ef19a0
commit
c97c171f41
@ -1,31 +1,29 @@
|
||||
from pathlib import Path
|
||||
from jinja2 import Template
|
||||
import yaml
|
||||
import json
|
||||
import glob
|
||||
"""Utils class"""
|
||||
|
||||
from jsonschema import validate, Draft7Validator, validators, exceptions
|
||||
import collections
|
||||
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from jinja2 import Template
|
||||
from jsonschema import Draft7Validator, validators
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Utils Methods
|
||||
# =====================
|
||||
|
||||
|
||||
def glob_files(path, pattern):
|
||||
log.debug(f"Search glob '{pattern}' in '{path}'")
|
||||
p = Path(path)
|
||||
ret = p.glob(pattern)
|
||||
"""Return a list of path that match a glob"""
|
||||
log.debug("Search glob '%s' in '%s'", pattern, path)
|
||||
path = Path(path)
|
||||
ret = path.glob(pattern)
|
||||
return [str(i) for i in ret]
|
||||
|
||||
|
||||
def path_assemble_hier(path, sep='/'):
|
||||
"""Append the previous
|
||||
|
||||
"""
|
||||
def path_assemble_hier(path, sep="/"):
|
||||
"""Append the previous"""
|
||||
|
||||
if isinstance(path, str):
|
||||
list_data = path.split(sep)
|
||||
@ -34,8 +32,7 @@ def path_assemble_hier(path, sep='/'):
|
||||
else:
|
||||
raise Exception(f"This function only accepts string or lists, got: {path}")
|
||||
|
||||
|
||||
assert isinstance(list_data, list), f'Got: {list_data}'
|
||||
assert isinstance(list_data, list), f"Got: {list_data}"
|
||||
ret = []
|
||||
for index, part in enumerate(list_data):
|
||||
try:
|
||||
@ -48,12 +45,11 @@ def path_assemble_hier(path, sep='/'):
|
||||
return ret
|
||||
|
||||
|
||||
|
||||
def render_template(path, params):
|
||||
"""Render template for a given string"""
|
||||
assert isinstance(params, dict), f"Got: {params}"
|
||||
t = Template(path)
|
||||
return t.render(**params)
|
||||
tpl = Template(path)
|
||||
return tpl.render(**params)
|
||||
|
||||
|
||||
# Schema Methods
|
||||
@ -65,9 +61,9 @@ def _extend_with_default(validator_class):
|
||||
|
||||
def set_defaults(validator, properties, instance, schema):
|
||||
|
||||
for property, subschema in properties.items():
|
||||
for prop, subschema in properties.items():
|
||||
if "default" in subschema:
|
||||
instance.setdefault(property, subschema["default"])
|
||||
instance.setdefault(prop, subschema["default"])
|
||||
|
||||
try:
|
||||
for error in validate_properties(
|
||||
@ -77,8 +73,8 @@ def _extend_with_default(validator_class):
|
||||
schema,
|
||||
):
|
||||
continue
|
||||
except Exception as e:
|
||||
print("CATCHED2222 ", e)
|
||||
except Exception as err:
|
||||
print("CATCHED2222 ", err)
|
||||
|
||||
return validators.extend(
|
||||
validator_class,
|
||||
@ -87,23 +83,25 @@ def _extend_with_default(validator_class):
|
||||
|
||||
|
||||
def schema_validate(config, schema):
|
||||
"""Validate a config against a jsonschema"""
|
||||
|
||||
# Validate the schema
|
||||
DefaultValidatingDraft7Validator = _extend_with_default(Draft7Validator)
|
||||
try:
|
||||
DefaultValidatingDraft7Validator(schema).validate(config)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
p = list(collections.deque(e.schema_path))
|
||||
p = "/".join([str(i) for i in p])
|
||||
p = f"schema/{p}"
|
||||
except Exception as err:
|
||||
print(err)
|
||||
path = list(collections.deque(err.schema_path))
|
||||
path = "/".join([str(i) for i in path])
|
||||
path = f"schema/{path}"
|
||||
raise Exception(
|
||||
f"Failed validating {p} for resource with content: {config} with !!!!!! schema: {schema}"
|
||||
f"Failed validating {path} for resource with content: {config}"
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
def str_ellipsis(txt, length=120):
|
||||
"""Truncate with ellipsis too wide texts"""
|
||||
txt = str(txt)
|
||||
ret = []
|
||||
for string in txt.splitlines():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user