# -*- coding: utf-8 -*- import pykwalify.core from pykwalify.errors import SchemaError """ Read yaml file : import yaml filename = "observatory_guitalens.yml" with open(filename, 'r') as stream: try: a = yaml.safe_load(stream) print(a) except yaml.YAMLError as exc: print(exc) # Write YAML file with open('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 = pykwalify.core.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("observatory_v1.yml","schema_observatory-1.0.yml"))