Skip to content

Commit

Permalink
First attempt for a provenence yaml representer.
Browse files Browse the repository at this point in the history
  • Loading branch information
nwieters committed Aug 14, 2023
1 parent f7b7821 commit e23248e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/esm_parser/yaml_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
26 changes: 19 additions & 7 deletions tests/test_esm_parser/test_provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit e23248e

Please sign in to comment.