Skip to content

Commit

Permalink
consolidate representers and test
Browse files Browse the repository at this point in the history
  • Loading branch information
WeirAE committed Feb 28, 2024
1 parent 4886cd5 commit f4b1ac0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
18 changes: 3 additions & 15 deletions src/uwtools/config/formats/yaml.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
from collections import OrderedDict
from pathlib import Path
from types import SimpleNamespace as ns
from typing import Optional

import yaml
from f90nml import Namelist # type: ignore

from uwtools.config.formats.base import Config
from uwtools.config.support import (
INCLUDE_TAG,
TaggedString,
log_and_error,
represent_namelist,
represent_ordereddict,
)
from uwtools.config.support import INCLUDE_TAG, TaggedString, add_representers, log_and_error
from uwtools.utils.file import FORMAT, readable, writable

_MSGS = ns(
Expand Down Expand Up @@ -52,9 +44,7 @@ def __repr__(self) -> str:
"""
The string representation of a YAMLConfig object.
"""
yaml.add_representer(TaggedString, TaggedString.represent)
yaml.add_representer(Namelist, represent_namelist)
yaml.add_representer(OrderedDict, represent_ordereddict)
add_representers()
return yaml.dump(self.data, default_flow_style=False).strip()

# Private methods
Expand Down Expand Up @@ -125,9 +115,7 @@ def dump_dict(cfg: dict, path: Optional[Path] = None) -> None:
:param cfg: The in-memory config object to dump.
:param path: Path to dump config to.
"""
yaml.add_representer(TaggedString, TaggedString.represent)
yaml.add_representer(Namelist, represent_namelist)
yaml.add_representer(OrderedDict, represent_ordereddict)
add_representers()
with writable(path) as f:
yaml.dump(cfg, f, sort_keys=False)

Expand Down
18 changes: 16 additions & 2 deletions src/uwtools/config/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
INCLUDE_TAG = "!INCLUDE"


# Public functions
def add_representers():
"""
Add representers to the YAML dumper for custom types.
This function is called by the YAMLConfig class to add representers for the TaggedString,
Namelist, and OrderedDict types to the YAML dumper. The representers are used to serialize these
types to YAML.
"""
yaml.add_representer(TaggedString, TaggedString.represent)
yaml.add_representer(Namelist, _represent_namelist)
yaml.add_representer(OrderedDict, _represent_ordereddict)


def depth(d: dict) -> int:
"""
The depth of a dictionary.
Expand Down Expand Up @@ -55,7 +69,7 @@ def log_and_error(msg: str) -> Exception:
return UWConfigError(msg)


def represent_namelist(dumper: yaml.Dumper, data: Namelist) -> yaml.nodes.MappingNode:
def _represent_namelist(dumper: yaml.Dumper, data: Namelist) -> yaml.nodes.MappingNode:
"""
Convert f90nml Namelist to OrderedDict and serialize.
Expand All @@ -69,7 +83,7 @@ def represent_namelist(dumper: yaml.Dumper, data: Namelist) -> yaml.nodes.Mappin
return dumper.represent_mapping("tag:yaml.org,2002:map", namelist_dict)


def represent_ordereddict(dumper: yaml.Dumper, data: OrderedDict) -> yaml.nodes.MappingNode:
def _represent_ordereddict(dumper: yaml.Dumper, data: OrderedDict) -> yaml.nodes.MappingNode:
"""
Convert OrderedDict to dict and serialize.
Expand Down
10 changes: 9 additions & 1 deletion src/uwtools/tests/config/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import pytest
import yaml
from f90nml import reads # type: ignore
from f90nml import Namelist, reads # type: ignore
from pytest import fixture, raises

from uwtools.config import support
Expand All @@ -23,6 +23,14 @@
from uwtools.utils.file import FORMAT


def test_add_representers():
support.add_representers()
representers = yaml.Dumper.yaml_representers
assert support.TaggedString in representers
assert OrderedDict in representers
assert Namelist in representers


@pytest.mark.parametrize(
"d,n", [({1: 88}, 1), ({1: {2: 88}}, 2), ({1: {2: {3: 88}}}, 3), ({1: {}}, 2)]
)
Expand Down

0 comments on commit f4b1ac0

Please sign in to comment.