grocy-cli/grocy/util.py

40 lines
1.2 KiB
Python
Raw Normal View History

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)
def load_yaml(self, data):
generator = yaml.safe_load_all(data)
data_list = list(generator)
return data_list
def verify_integrity(self, new_data, schema):
logger = logging.getLogger('util.verify_integrity')
try:
# Verify that updated fields exist
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(', ')))
except Exception as e:
logger.error(e)
raise e