test_yaml.py 1.3 KB
# -*- coding: utf-8 -*-

from pykwalify.core import Core
from pykwalify.errors import SchemaError

""" 
Read yaml file : 
import yaml
with open("C:/srv/develop/pyros/config/config.yml", 'r') as stream:
    try:
        a = yaml.safe_load(stream)
        print(a)
    except yaml.YAMLError as exc:
        print(exc)

# Write YAML file
with open('C:/srv/develop/pyros/config/config2.yml', 'w', encoding='utf8') as outfile:
    yaml.dump(a, outfile, default_flow_style=False, allow_unicode=True)
"""

"""
import yaml
with open("config.yml", 'r') as stream:
    try:
        a = yaml.safe_load(stream)
        print(a)
    except yaml.YAMLError as exc:
        print(exc)

 """

def check_and_return_config(yaml_file:str,schema_file:str)->dict:
    """
    Check if yaml_file is valid for the schema_file

    Args:
        yaml_file (str): Path to the config_file to be validated
        schema_file (str): Path to the schema file 

    Returns:
        dict: Dictionnary of the config file (with values)
    """
    try:

        c = Core(source_file=yaml_file, schema_files=[schema_file])
        return c.validate(raise_exception=True)
    except SchemaError as error:
        #TODO : find a way to retrieve message with path to error
        print(error.path)
        

print(check_and_return_config("config.yml","schema.yaml"))