Coverage for .nox/test-3-9/lib/python3.9/site-packages/nskit/common/io/yaml.py: 100%
13 statements
« prev ^ index » next coverage.py v7.4.2, created at 2024-02-25 17:38 +0000
« prev ^ index » next coverage.py v7.4.2, created at 2024-02-25 17:38 +0000
1"""Provide a YAML 1.2 Load/Dump API consistent with JSON."""
2from io import StringIO
3from typing import Any, TextIO
5from ruamel.yaml import YAML as _YAML
7# PyYAML only supports YAML 1.1 (so a: NO -> {"a": False})
8# Instead want to use YAML 1.2 (so a: NO -> {"a": "NO"})
9# StrictYAML is an alternative, but it is difficult as it casts everything to strings, so needs a schema.
12def loads(s: str, *, typ: str = 'rt', **kwargs):
13 """Load YAML from string."""
14 return load(StringIO(s), typ=typ, **kwargs)
17def dumps(data: Any, *, typ: str = 'rt', **kwargs):
18 """Dump YAML to string."""
19 s = StringIO()
20 dump(data, s, typ=typ, **kwargs)
21 return s.getvalue()
24def load(stream: TextIO, *, typ: str = 'rt', **kwargs):
25 """Load YAML from file/stream."""
26 return _YAML(typ=typ).load(stream, **kwargs)
29def dump(data: Any, stream: TextIO, *, typ: str = 'rt', **kwargs):
30 """Dump YAML to file/stream."""
31 return _YAML(typ=typ).dump(data, stream=stream, **kwargs)
33# TODO: Add !include and !env tag handling