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