From e23248ee1193a8d739fee1690476e61b0327b03a Mon Sep 17 00:00:00 2001 From: Nadine Wieters Date: Mon, 14 Aug 2023 10:33:46 +0200 Subject: [PATCH] First attempt for a provenence yaml representer. --- src/esm_parser/yaml_to_dict.py | 9 +++++++- tests/test_esm_parser/test_provenance.py | 26 +++++++++++++++++------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/esm_parser/yaml_to_dict.py b/src/esm_parser/yaml_to_dict.py index 70296c672..b86945c79 100644 --- a/src/esm_parser/yaml_to_dict.py +++ b/src/esm_parser/yaml_to_dict.py @@ -18,9 +18,16 @@ CONFIG_PATH = esm_tools.get_config_filepath() -class CommentedYamlDumper(yaml.SafeDumper): +def provenance_representer(dumper, provenance): + return dumper.represent_str("provenance") + +#class CommentedYamlDumper(ruamel.yaml.SafeDumper): +class CommentedYamlDumper(yaml.Dumper): pass +CommentedYamlDumper.add_representer( + str,provenance_representer +) class EsmConfigFileError(Exception): """ diff --git a/tests/test_esm_parser/test_provenance.py b/tests/test_esm_parser/test_provenance.py index bcd4de6be..b11835815 100644 --- a/tests/test_esm_parser/test_provenance.py +++ b/tests/test_esm_parser/test_provenance.py @@ -7,15 +7,16 @@ import pytest from ruamel.yaml import YAML +import yaml as yamel import esm_parser import esm_parser.provenance as provenance from esm_parser import yaml_to_dict -#@pytest.fixture -#def yaml(): -# yaml = YAML() -# return yaml +@pytest.fixture +def yaml(): + yaml = YAML() + return yaml # # #@pytest.fixture @@ -258,8 +259,19 @@ def test_check_set_provenance_of_single_list_entry(): assert config["person"]["my_other_list"].get_provenance() == check_prov -def test_dump_config_with_provenance(config, yaml): +#def test_dump_config_with_provenance(config, yaml): +def test_dump_config_with_provenance(yaml): """Prints out the dictionary with comments""" - breakpoint() - print(yaml.dump(config.get_provenance(), sys.stdout)) + esm_tools_loader = yaml_to_dict.EsmToolsLoader() + with open(EXAMPLE_PATH1, "r") as file: + esm_tools_loader.set_filename(file) + data, data2 = esm_tools_loader.load(file) + + config = provenance.DictWithProvenance(data, data2) +# breakpoint() + yamel.dump(config, sys.stdout, Dumper=yaml_to_dict.CommentedYamlDumper, width=10000, indent=4) +# print(yaml.dump(config.get_provenance(), sys.stdout)) +# dumper = esm_parser.CommentedYamlDumper(config.get_provenance()) +# print(dumper.dump, sys.stdout) + assert 1 == 2