2019-06-25 00:52:17 -05:00
|
|
|
import yaml
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
def _yaml_constructor(loader, node):
|
|
|
|
return node.value
|
|
|
|
|
|
|
|
|
|
|
|
class Util(object):
|
|
|
|
def __init__(self, cfg):
|
|
|
|
self.cfg = cfg
|
|
|
|
yaml.SafeLoader.add_constructor("tag:yaml.org,2002:python/unicode", _yaml_constructor)
|
|
|
|
|
2019-08-04 23:59:28 -05:00
|
|
|
def load_yaml(self, data):
|
2019-06-25 00:52:17 -05:00
|
|
|
generator = yaml.safe_load_all(data)
|
|
|
|
data_list = list(generator)
|
|
|
|
return data_list
|
|
|
|
|
2019-09-25 23:14:11 -05:00
|
|
|
def verify_integrity(self, new_data, schema):
|
2019-06-25 00:52:17 -05:00
|
|
|
logger = logging.getLogger('util.verify_integrity')
|
|
|
|
try:
|
|
|
|
# Verify that updated fields exist
|
2019-09-25 23:14:11 -05:00
|
|
|
if type(schema) != list:
|
|
|
|
schemas = [schema]
|
|
|
|
else:
|
|
|
|
schemas = schema
|
|
|
|
|
|
|
|
keys_not_found = []
|
|
|
|
for current_schema in schemas:
|
|
|
|
schema_keys = current_schema['properties'].keys()
|
|
|
|
for prop in new_data.keys():
|
|
|
|
if prop not in schema_keys:
|
|
|
|
keys_not_found.append(prop)
|
|
|
|
print('{}'.format(keys_not_found))
|
|
|
|
if len(keys_not_found) > 0:
|
|
|
|
raise Exception('{} is not valid property'.format(keys_not_found.join(', ')))
|
2019-06-25 00:52:17 -05:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(e)
|
|
|
|
raise e
|